Changeset 7481ee19 in mainline
- Timestamp:
- 2021-06-23T08:15:00Z (3 years ago)
- Branches:
- master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- a977e37
- Parents:
- 034ce6bb
- git-author:
- Jiri Svoboda <jiri@…> (2021-06-22 17:14:40)
- git-committer:
- Jiri Svoboda <jiri@…> (2021-06-23 08:15:00)
- Location:
- uspace
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/calculator/calculator.c
r034ce6bb r7481ee19 945 945 ui_entry_set_rect(display, &calc.geom.entry_rect); 946 946 ui_entry_set_halign(display, gfx_halign_right); 947 ui_entry_set_read_only(display, true); 947 948 948 949 rc = ui_fixed_add(fixed, ui_entry_ctl(display)); -
uspace/lib/ui/include/types/ui/control.h
r034ce6bb r7481ee19 1 1 /* 2 * Copyright (c) 202 0Jiri Svoboda2 * Copyright (c) 2021 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 38 38 39 39 #include <errno.h> 40 #include <io/kbd_event.h> 40 41 #include <io/pos_event.h> 41 42 #include <types/ui/event.h> … … 50 51 /** Paint */ 51 52 errno_t (*paint)(void *); 53 /** Keyboard event */ 54 ui_evclaim_t (*kbd_event)(void *, kbd_event_t *); 52 55 /** Position event */ 53 56 ui_evclaim_t (*pos_event)(void *, pos_event_t *); -
uspace/lib/ui/include/ui/control.h
r034ce6bb r7481ee19 1 1 /* 2 * Copyright (c) 202 0Jiri Svoboda2 * Copyright (c) 2021 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 38 38 39 39 #include <errno.h> 40 #include <io/kbd_event.h> 40 41 #include <io/pos_event.h> 41 42 #include <types/ui/control.h> … … 46 47 extern void ui_control_destroy(ui_control_t *); 47 48 extern errno_t ui_control_paint(ui_control_t *); 49 extern ui_evclaim_t ui_control_kbd_event(ui_control_t *, kbd_event_t *); 48 50 extern ui_evclaim_t ui_control_pos_event(ui_control_t *, pos_event_t *); 49 51 extern void ui_control_unfocus(ui_control_t *); -
uspace/lib/ui/include/ui/entry.h
r034ce6bb r7481ee19 50 50 extern void ui_entry_set_rect(ui_entry_t *, gfx_rect_t *); 51 51 extern void ui_entry_set_halign(ui_entry_t *, gfx_halign_t); 52 extern void ui_entry_set_read_only(ui_entry_t *, bool); 52 53 extern errno_t ui_entry_set_text(ui_entry_t *, const char *); 53 54 extern errno_t ui_entry_paint(ui_entry_t *); 55 extern ui_evclaim_t ui_entry_kbd_event(ui_entry_t *, kbd_event_t *); 56 extern ui_evclaim_t ui_entry_pos_event(ui_entry_t *, pos_event_t *); 54 57 55 58 #endif -
uspace/lib/ui/include/ui/fixed.h
r034ce6bb r7481ee19 1 1 /* 2 * Copyright (c) 202 0Jiri Svoboda2 * Copyright (c) 2021 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 49 49 extern void ui_fixed_remove(ui_fixed_t *, ui_control_t *); 50 50 extern errno_t ui_fixed_paint(ui_fixed_t *); 51 extern ui_evclaim_t ui_fixed_kbd_event(ui_fixed_t *, kbd_event_t *); 51 52 extern ui_evclaim_t ui_fixed_pos_event(ui_fixed_t *, pos_event_t *); 52 53 extern void ui_fixed_unfocus(ui_fixed_t *); -
uspace/lib/ui/include/ui/window.h
r034ce6bb r7481ee19 40 40 #include <gfx/context.h> 41 41 #include <gfx/coord.h> 42 #include <io/kbd_event.h> 42 43 #include <io/pos_event.h> 43 44 #include <types/ui/control.h> … … 62 63 extern void ui_window_set_ctl_cursor(ui_window_t *, ui_stock_cursor_t); 63 64 extern errno_t ui_window_paint(ui_window_t *); 65 extern void ui_window_def_kbd(ui_window_t *, kbd_event_t *); 64 66 extern errno_t ui_window_def_paint(ui_window_t *); 65 67 extern void ui_window_def_pos(ui_window_t *, pos_event_t *); -
uspace/lib/ui/private/entry.h
r034ce6bb r7481ee19 54 54 /** Horizontal alignment */ 55 55 gfx_halign_t halign; 56 /** Text entry is read-only */ 57 bool read_only; 56 58 /** Text */ 57 59 char *text; 58 60 /** Pointer is currently inside */ 59 61 bool pointer_inside; 62 /** Entry is activated */ 63 bool active; 60 64 }; 65 66 extern errno_t ui_entry_insert_str(ui_entry_t *, const char *); 67 extern void ui_entry_backspace(ui_entry_t *); 68 extern ui_evclaim_t ui_entry_key_press_unmod(ui_entry_t *, kbd_event_t *); 61 69 62 70 #endif -
uspace/lib/ui/src/control.c
r034ce6bb r7481ee19 1 1 /* 2 * Copyright (c) 202 0Jiri Svoboda2 * Copyright (c) 2021 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 35 35 36 36 #include <errno.h> 37 #include <io/kbd_event.h> 37 38 #include <io/pos_event.h> 38 39 #include <stdlib.h> … … 91 92 } 92 93 94 /** Deliver keyboard event to UI control. 95 * 96 * @param control Control 97 * @param kbd_event Keyboard event 98 * @return @c ui_claimed iff the event is claimed 99 */ 100 ui_evclaim_t ui_control_kbd_event(ui_control_t *control, kbd_event_t *event) 101 { 102 if (control->ops->kbd_event != NULL) 103 return control->ops->kbd_event(control->ext, event); 104 else 105 return ui_unclaimed; 106 } 107 93 108 /** Paint UI control. 94 109 * -
uspace/lib/ui/src/entry.c
r034ce6bb r7481ee19 53 53 static void ui_entry_ctl_destroy(void *); 54 54 static errno_t ui_entry_ctl_paint(void *); 55 static ui_evclaim_t ui_entry_ctl_kbd_event(void *, kbd_event_t *); 55 56 static ui_evclaim_t ui_entry_ctl_pos_event(void *, pos_event_t *); 56 57 … … 66 67 .destroy = ui_entry_ctl_destroy, 67 68 .paint = ui_entry_ctl_paint, 69 .kbd_event = ui_entry_ctl_kbd_event, 68 70 .pos_event = ui_entry_ctl_pos_event 69 71 }; … … 148 150 } 149 151 152 /** Set text entry read-only flag. 153 * 154 * @param entry Text entry 155 * @param read_only True iff entry is to be read-only. 156 */ 157 void ui_entry_set_read_only(ui_entry_t *entry, bool read_only) 158 { 159 entry->read_only = read_only; 160 } 161 150 162 /** Set entry text. 151 163 * … … 180 192 gfx_coord_t hpad; 181 193 gfx_coord_t vpad; 194 gfx_coord_t width; 182 195 gfx_rect_t inside; 183 196 errno_t rc; … … 212 225 goto error; 213 226 227 width = gfx_text_width(res->font, entry->text); 228 214 229 switch (entry->halign) { 215 230 case gfx_halign_left: … … 218 233 break; 219 234 case gfx_halign_center: 220 pos.x = (inside.p0.x + inside.p1.x) / 2 ;235 pos.x = (inside.p0.x + inside.p1.x) / 2 - width / 2; 221 236 break; 222 237 case gfx_halign_right: 223 pos.x = inside.p1.x - hpad - 1 ;238 pos.x = inside.p1.x - hpad - 1 - width; 224 239 break; 225 240 } … … 229 244 gfx_text_fmt_init(&fmt); 230 245 fmt.color = res->entry_fg_color; 231 fmt.halign = entry->halign;246 fmt.halign = gfx_halign_left; 232 247 fmt.valign = gfx_valign_top; 233 248 249 rc = gfx_set_clip_rect(res->gc, &inside); 250 if (rc != EOK) 251 goto error; 252 234 253 rc = gfx_puttext(res->font, &pos, &fmt, entry->text); 254 if (rc != EOK) { 255 (void) gfx_set_clip_rect(res->gc, NULL); 256 goto error; 257 } 258 259 if (entry->active) { 260 /* Cursor */ 261 pos.x += width; 262 263 rc = gfx_puttext(res->font, &pos, &fmt, "_"); 264 if (rc != EOK) { 265 (void) gfx_set_clip_rect(res->gc, NULL); 266 goto error; 267 } 268 } 269 270 rc = gfx_set_clip_rect(res->gc, NULL); 235 271 if (rc != EOK) 236 272 goto error; … … 268 304 } 269 305 270 /** Handle text entry control position event. 271 * 272 * @param arg Argument (ui_entry_t *) 306 /** Insert string at cursor position. 307 * 308 * @param entry Text entry 309 * @param str String 310 * @return EOK on success, ENOMEM if out of memory 311 */ 312 errno_t ui_entry_insert_str(ui_entry_t *entry, const char *str) 313 { 314 char *newtext; 315 char *oldtext; 316 int rc; 317 318 rc = asprintf(&newtext, "%s%s", entry->text, str); 319 if (rc < 0) 320 return ENOMEM; 321 322 oldtext = entry->text; 323 entry->text = newtext; 324 free(oldtext); 325 ui_entry_paint(entry); 326 327 return EOK; 328 } 329 330 /** Delete character before cursor. 331 * 332 * @param entry Text entry 333 */ 334 void ui_entry_backspace(ui_entry_t *entry) 335 { 336 size_t off; 337 338 off = str_size(entry->text); 339 (void) str_decode_reverse(entry->text, &off, 340 str_size(entry->text)); 341 entry->text[off] = '\0'; 342 ui_entry_paint(entry); 343 } 344 345 /** Handle text entry key press without modifiers. 346 * 347 * @param entry Text entry 348 * @param kbd_event Keyboard event 349 * @return @c ui_claimed iff the event is claimed 350 */ 351 ui_evclaim_t ui_entry_key_press_unmod(ui_entry_t *entry, kbd_event_t *event) 352 { 353 assert(event->type == KEY_PRESS); 354 355 if (event->key == KC_BACKSPACE) 356 ui_entry_backspace(entry); 357 358 if (event->key == KC_ESCAPE) { 359 entry->active = false; 360 (void) ui_entry_paint(entry); 361 } 362 363 return ui_claimed; 364 } 365 366 /** Handle text entry keyboard event. 367 * 368 * @param entry Text entry 369 * @param kbd_event Keyboard event 370 * @return @c ui_claimed iff the event is claimed 371 */ 372 ui_evclaim_t ui_entry_kbd_event(ui_entry_t *entry, kbd_event_t *event) 373 { 374 char buf[STR_BOUNDS(1) + 1]; 375 size_t off; 376 errno_t rc; 377 378 if (!entry->active) 379 return ui_unclaimed; 380 381 if (event->type == KEY_PRESS && event->c >= ' ') { 382 off = 0; 383 rc = chr_encode(event->c, buf, &off, sizeof(buf)); 384 if (rc == EOK) { 385 buf[off] = '\0'; 386 (void) ui_entry_insert_str(entry, buf); 387 } 388 } 389 390 if (event->type == KEY_PRESS && 391 (event->mods & (KM_CTRL | KM_ALT | KM_SHIFT)) == 0) 392 return ui_entry_key_press_unmod(entry, event); 393 394 return ui_claimed; 395 } 396 397 /** Handle text entry position event. 398 * 399 * @param entry Text entry 273 400 * @param pos_event Position event 274 401 * @return @c ui_claimed iff the event is claimed 275 402 */ 276 ui_evclaim_t ui_entry_ctl_pos_event(void *arg, pos_event_t *event) 277 { 278 ui_entry_t *entry = (ui_entry_t *) arg; 403 ui_evclaim_t ui_entry_pos_event(ui_entry_t *entry, pos_event_t *event) 404 { 279 405 gfx_coord2_t pos; 406 407 if (entry->read_only) 408 return ui_unclaimed; 280 409 281 410 if (event->type == POS_UPDATE) { … … 298 427 } 299 428 429 if (event->type == POS_PRESS) { 430 pos.x = event->hpos; 431 pos.y = event->vpos; 432 433 if (gfx_pix_inside_rect(&pos, &entry->rect)) { 434 if (!entry->active) { 435 entry->active = true; 436 (void) ui_entry_paint(entry); 437 } 438 439 return ui_claimed; 440 } else { 441 if (entry->active) { 442 entry->active = false; 443 (void) ui_entry_paint(entry); 444 } 445 } 446 } 447 300 448 return ui_unclaimed; 301 449 } 302 450 451 /** Handle text entry control keyboard event. 452 * 453 * @param arg Argument (ui_entry_t *) 454 * @param kbd_event Keyboard event 455 * @return @c ui_claimed iff the event is claimed 456 */ 457 static ui_evclaim_t ui_entry_ctl_kbd_event(void *arg, kbd_event_t *event) 458 { 459 ui_entry_t *entry = (ui_entry_t *) arg; 460 461 return ui_entry_kbd_event(entry, event); 462 } 463 464 /** Handle text entry control position event. 465 * 466 * @param arg Argument (ui_entry_t *) 467 * @param pos_event Position event 468 * @return @c ui_claimed iff the event is claimed 469 */ 470 static ui_evclaim_t ui_entry_ctl_pos_event(void *arg, pos_event_t *event) 471 { 472 ui_entry_t *entry = (ui_entry_t *) arg; 473 474 return ui_entry_pos_event(entry, event); 475 } 476 303 477 /** @} 304 478 */ -
uspace/lib/ui/src/fixed.c
r034ce6bb r7481ee19 1 1 /* 2 * Copyright (c) 202 0Jiri Svoboda2 * Copyright (c) 2021 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 46 46 static void ui_fixed_ctl_destroy(void *); 47 47 static errno_t ui_fixed_ctl_paint(void *); 48 static ui_evclaim_t ui_fixed_ctl_kbd_event(void *, kbd_event_t *); 48 49 static ui_evclaim_t ui_fixed_ctl_pos_event(void *, pos_event_t *); 49 50 static void ui_fixed_ctl_unfocus(void *); … … 53 54 .destroy = ui_fixed_ctl_destroy, 54 55 .paint = ui_fixed_ctl_paint, 56 .kbd_event = ui_fixed_ctl_kbd_event, 55 57 .pos_event = ui_fixed_ctl_pos_event, 56 58 .unfocus = ui_fixed_ctl_unfocus … … 211 213 } 212 214 215 /** Handle fixed layout keyboard event. 216 * 217 * @param fixed Fixed layout 218 * @param kbd_event Keyboard event 219 * @return @c ui_claimed iff the event is claimed 220 */ 221 ui_evclaim_t ui_fixed_kbd_event(ui_fixed_t *fixed, kbd_event_t *event) 222 { 223 ui_fixed_elem_t *elem; 224 ui_evclaim_t claimed; 225 226 elem = ui_fixed_first(fixed); 227 while (elem != NULL) { 228 claimed = ui_control_kbd_event(elem->control, event); 229 if (claimed == ui_claimed) 230 return ui_claimed; 231 232 elem = ui_fixed_next(elem); 233 } 234 235 return ui_unclaimed; 236 } 237 213 238 /** Handle fixed layout position event. 214 239 * … … 273 298 } 274 299 300 /** Handle fixed layout control keyboard event. 301 * 302 * @param arg Argument (ui_fixed_t *) 303 * @param kbd_event Keyboard event 304 * @return @c ui_claimed iff the event is claimed 305 */ 306 ui_evclaim_t ui_fixed_ctl_kbd_event(void *arg, kbd_event_t *event) 307 { 308 ui_fixed_t *fixed = (ui_fixed_t *) arg; 309 310 return ui_fixed_kbd_event(fixed, event); 311 } 312 275 313 /** Handle fixed layout control position event. 276 314 * -
uspace/lib/ui/src/window.c
r034ce6bb r7481ee19 871 871 if (window->cb != NULL && window->cb->kbd != NULL) 872 872 window->cb->kbd(window, window->arg, kbd); 873 else 874 return ui_window_def_kbd(window, kbd); 873 875 } 874 876 … … 907 909 else 908 910 return ui_window_def_unfocus(window); 911 } 912 913 /** Default window keyboard event routine. 914 * 915 * @param window Window 916 */ 917 void ui_window_def_kbd(ui_window_t *window, kbd_event_t *kbd) 918 { 919 if (window->control != NULL) 920 ui_control_kbd_event(window->control, kbd); 909 921 } 910 922 -
uspace/lib/ui/test/control.c
r034ce6bb r7481ee19 1 1 /* 2 * Copyright (c) 202 0Jiri Svoboda2 * Copyright (c) 2021 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 29 29 #include <errno.h> 30 30 #include <mem.h> 31 #include <io/kbd_event.h> 31 32 #include <io/pos_event.h> 32 33 #include <pcut/pcut.h> … … 41 42 static void test_ctl_destroy(void *); 42 43 static errno_t test_ctl_paint(void *); 44 static ui_evclaim_t test_ctl_kbd_event(void *, kbd_event_t *); 43 45 static ui_evclaim_t test_ctl_pos_event(void *, pos_event_t *); 44 46 static void test_ctl_unfocus(void *); … … 47 49 .destroy = test_ctl_destroy, 48 50 .paint = test_ctl_paint, 51 .kbd_event = test_ctl_kbd_event, 49 52 .pos_event = test_ctl_pos_event, 50 53 .unfocus = test_ctl_unfocus … … 64 67 bool paint; 65 68 69 /** @c true iff kbd_event was called */ 70 bool kbd; 71 /** Keyboard event that was sent */ 72 kbd_event_t kevent; 73 66 74 /** @c true iff pos_event was called */ 67 75 bool pos; … … 134 142 PCUT_ASSERT_ERRNO_VAL(resp.rc, rc); 135 143 PCUT_ASSERT_TRUE(resp.paint); 144 145 ui_control_delete(control); 146 } 147 148 /** Test sending keyboard event to control */ 149 PCUT_TEST(kbd_event) 150 { 151 ui_control_t *control = NULL; 152 test_resp_t resp; 153 kbd_event_t event; 154 ui_evclaim_t claim; 155 errno_t rc; 156 157 rc = ui_control_new(&test_ctl_ops, &resp, &control); 158 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 159 PCUT_ASSERT_NOT_NULL(control); 160 161 resp.claim = ui_claimed; 162 resp.kbd = false; 163 event.type = KEY_PRESS; 164 event.key = KC_2; 165 event.mods = KM_LSHIFT; 166 event.c = '@'; 167 168 claim = ui_control_kbd_event(control, &event); 169 PCUT_ASSERT_EQUALS(resp.claim, claim); 170 PCUT_ASSERT_TRUE(resp.kbd); 171 PCUT_ASSERT_EQUALS(resp.kevent.type, event.type); 172 PCUT_ASSERT_INT_EQUALS(resp.kevent.key, event.key); 173 PCUT_ASSERT_INT_EQUALS(resp.kevent.mods, event.mods); 174 PCUT_ASSERT_INT_EQUALS(resp.kevent.c, event.c); 136 175 137 176 ui_control_delete(control); … … 205 244 } 206 245 246 static ui_evclaim_t test_ctl_kbd_event(void *arg, kbd_event_t *event) 247 { 248 test_resp_t *resp = (test_resp_t *) arg; 249 250 resp->kbd = true; 251 resp->kevent = *event; 252 253 return resp->claim; 254 } 255 207 256 static ui_evclaim_t test_ctl_pos_event(void *arg, pos_event_t *event) 208 257 { -
uspace/lib/ui/test/entry.c
r034ce6bb r7481ee19 119 119 } 120 120 121 /** Set entry read only flag sets internal field */ 122 PCUT_TEST(set_read_only) 123 { 124 ui_entry_t *entry; 125 errno_t rc; 126 127 rc = ui_entry_create(NULL, "Hello", &entry); 128 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 129 130 ui_entry_set_read_only(entry, true); 131 PCUT_ASSERT_TRUE(entry->read_only); 132 ui_entry_set_read_only(entry, false); 133 PCUT_ASSERT_FALSE(entry->read_only); 134 135 ui_entry_destroy(entry); 136 } 137 121 138 /** Set text entry rectangle sets internal field */ 122 139 PCUT_TEST(set_text) … … 173 190 } 174 191 192 /** ui_entry_insert_str() inserts string at cursor. */ 193 PCUT_TEST(insert_str) 194 { 195 errno_t rc; 196 ui_t *ui = NULL; 197 ui_window_t *window = NULL; 198 ui_wnd_params_t params; 199 ui_entry_t *entry; 200 201 rc = ui_create_disp(NULL, &ui); 202 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 203 204 ui_wnd_params_init(¶ms); 205 params.caption = "Hello"; 206 207 rc = ui_window_create(ui, ¶ms, &window); 208 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 209 PCUT_ASSERT_NOT_NULL(window); 210 211 rc = ui_entry_create(window, "A", &entry); 212 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 213 214 PCUT_ASSERT_STR_EQUALS("A", entry->text); 215 216 rc = ui_entry_insert_str(entry, "B"); 217 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 218 219 PCUT_ASSERT_STR_EQUALS("AB", entry->text); 220 221 rc = ui_entry_insert_str(entry, "CD"); 222 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 223 224 PCUT_ASSERT_STR_EQUALS("ABCD", entry->text); 225 226 ui_entry_destroy(entry); 227 ui_window_destroy(window); 228 ui_destroy(ui); 229 } 230 231 /** ui_entry_backspace() deletes character before cursor. */ 232 PCUT_TEST(backspace) 233 { 234 errno_t rc; 235 ui_t *ui = NULL; 236 ui_window_t *window = NULL; 237 ui_wnd_params_t params; 238 ui_entry_t *entry; 239 240 rc = ui_create_disp(NULL, &ui); 241 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 242 243 ui_wnd_params_init(¶ms); 244 params.caption = "Hello"; 245 246 rc = ui_window_create(ui, ¶ms, &window); 247 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 248 PCUT_ASSERT_NOT_NULL(window); 249 250 rc = ui_entry_create(window, "ABC", &entry); 251 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 252 253 PCUT_ASSERT_STR_EQUALS("ABC", entry->text); 254 255 ui_entry_backspace(entry); 256 PCUT_ASSERT_STR_EQUALS("AB", entry->text); 257 258 ui_entry_backspace(entry); 259 PCUT_ASSERT_STR_EQUALS("A", entry->text); 260 261 ui_entry_backspace(entry); 262 PCUT_ASSERT_STR_EQUALS("", entry->text); 263 264 ui_entry_backspace(entry); 265 PCUT_ASSERT_STR_EQUALS("", entry->text); 266 267 ui_entry_destroy(entry); 268 ui_window_destroy(window); 269 ui_destroy(ui); 270 } 271 175 272 PCUT_EXPORT(entry);
Note:
See TracChangeset
for help on using the changeset viewer.