Changes in uspace/lib/ui/src/pbutton.c [3583ffb:b769ca0] in mainline
- File:
-
- 1 edited
-
uspace/lib/ui/src/pbutton.c (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/ui/src/pbutton.c
r3583ffb rb769ca0 1 1 /* 2 * Copyright (c) 202 0Jiri Svoboda2 * Copyright (c) 2026 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 32 32 /** 33 33 * @file Push button 34 * 35 * Push button either uses text as decoration, or it can use a caller-provided 36 * function to paint the decoration. 34 37 */ 35 38 … … 51 54 enum { 52 55 ui_pb_press_dx = 1, 53 ui_pb_press_dy = 1 56 ui_pb_press_dy = 1, 57 ui_pb_pad_x = 2, 58 ui_pb_pad_x_text = 1 54 59 }; 55 60 … … 111 116 112 117 ui_control_delete(pbutton->control); 118 free(pbutton->caption); 113 119 free(pbutton); 114 120 } … … 136 142 } 137 143 144 /** Set push button ops. 145 * 146 * These allow overriding the function for painting the button or 147 * painting the button decoration. 148 * 149 * @param pbutton Push button 150 * @param ops Push button ops 151 * @param arg Decoration ops argument 152 */ 153 void ui_pbutton_set_ops(ui_pbutton_t *pbutton, ui_pbutton_ops_t *ops, void *arg) 154 { 155 pbutton->ops = ops; 156 pbutton->ops_arg = arg; 157 } 158 159 /** Set push button flag.s 160 * 161 * @param pbutton Push button 162 * @param flags Flags 163 */ 164 void ui_pbutton_set_flags(ui_pbutton_t *pbutton, ui_pbutton_flags_t flags) 165 { 166 pbutton->flags = flags; 167 } 168 138 169 /** Set button rectangle. 139 170 * … … 157 188 { 158 189 pbutton->isdefault = isdefault; 190 } 191 192 /** Get button light status. 193 * 194 * @param pbutton Button 195 * @return @c true iff light is on 196 */ 197 bool ui_pbutton_get_light(ui_pbutton_t *pbutton) 198 { 199 return pbutton->light; 200 } 201 202 /** Turn button light on or off. 203 * 204 * @param pbutton Button 205 * @param light @c true iff button should be lit 206 */ 207 void ui_pbutton_set_light(ui_pbutton_t *pbutton, bool light) 208 { 209 pbutton->light = light; 210 } 211 212 /** Set push button caption. 213 * 214 * @param pbutton Button 215 * @param caption New caption 216 * @return EOK on success, ENOMEM if out of memory 217 */ 218 errno_t ui_pbutton_set_caption(ui_pbutton_t *pbutton, const char *caption) 219 { 220 char *dcaption; 221 222 dcaption = str_dup(caption); 223 if (dcaption == NULL) 224 return ENOMEM; 225 226 free(pbutton->caption); 227 pbutton->caption = dcaption; 228 return EOK; 159 229 } 160 230 … … 239 309 } 240 310 241 /** Paint push button. 242 * 243 * @param pbutton Push button 244 * @return EOK on success or an error code 245 */ 246 errno_t ui_pbutton_paint(ui_pbutton_t *pbutton) 311 /** Paint button text shadow. 312 * 313 * @param pbutton Push button 314 * @return EOK on success or an error code 315 */ 316 static errno_t ui_pbutton_paint_text_shadow(ui_pbutton_t *pbutton) 317 { 318 gfx_rect_t rect; 319 errno_t rc; 320 321 rect.p0.x = pbutton->rect.p0.x + 1; 322 rect.p0.y = pbutton->rect.p0.y + 1; 323 rect.p1.x = pbutton->rect.p1.x; 324 rect.p1.y = pbutton->rect.p1.y; 325 326 rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_shadow_color); 327 if (rc != EOK) 328 goto error; 329 330 rc = gfx_fill_rect(pbutton->res->gc, &rect); 331 if (rc != EOK) 332 goto error; 333 334 return EOK; 335 error: 336 return rc; 337 } 338 339 /** Paint push button in graphic mode. 340 * 341 * @param pbutton Push button 342 * @return EOK on success or an error code 343 */ 344 static errno_t ui_pbutton_paint_gfx(ui_pbutton_t *pbutton) 247 345 { 248 346 gfx_coord2_t pos; 249 347 gfx_text_fmt_t fmt; 250 348 gfx_rect_t rect; 349 gfx_rect_t irect; 251 350 gfx_coord_t thickness; 351 gfx_color_t *color; 252 352 bool depressed; 253 353 errno_t rc; … … 261 361 rect.p1.y = pbutton->rect.p1.y - thickness; 262 362 263 rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color); 363 color = pbutton->light ? pbutton->res->btn_face_lit_color : 364 pbutton->res->btn_face_color; 365 366 rc = gfx_set_color(pbutton->res->gc, color); 264 367 if (rc != EOK) 265 368 goto error; 266 369 267 370 rc = gfx_fill_rect(pbutton->res->gc, &rect); 268 if (rc != EOK)269 goto error;270 271 rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_text_color);272 371 if (rc != EOK) 273 372 goto error; … … 282 381 } 283 382 284 gfx_text_fmt_init(&fmt); 285 fmt.halign = gfx_halign_center; 286 fmt.valign = gfx_valign_center; 287 288 rc = gfx_puttext(pbutton->res->font, &pos, &fmt, pbutton->caption); 289 if (rc != EOK) 290 goto error; 383 if (pbutton->ops != NULL && pbutton->ops->decor_paint != NULL) { 384 /* Custom decoration */ 385 rc = pbutton->ops->decor_paint(pbutton, pbutton->ops_arg, 386 &pos); 387 if (rc != EOK) 388 goto error; 389 } else { 390 /* Text decoration */ 391 ui_paint_get_inset_frame_inside(pbutton->res, &rect, &irect); 392 gfx_text_fmt_init(&fmt); 393 fmt.font = pbutton->res->font; 394 fmt.color = pbutton->res->btn_text_color; 395 fmt.halign = gfx_halign_center; 396 fmt.valign = gfx_valign_center; 397 fmt.abbreviate = true; 398 fmt.width = irect.p1.x - irect.p0.x - 2 * ui_pb_pad_x; 399 400 rc = gfx_puttext(&pos, &fmt, pbutton->caption); 401 if (rc != EOK) 402 goto error; 403 } 291 404 292 405 rc = ui_pbutton_paint_frame(pbutton); … … 304 417 } 305 418 419 rc = gfx_update(pbutton->res->gc); 420 if (rc != EOK) 421 goto error; 422 306 423 return EOK; 307 424 error: … … 309 426 } 310 427 428 /** Paint push button in text mode. 429 * 430 * @param pbutton Push button 431 * @return EOK on success or an error code 432 */ 433 static errno_t ui_pbutton_paint_text(ui_pbutton_t *pbutton) 434 { 435 gfx_coord2_t pos; 436 gfx_text_fmt_t fmt; 437 gfx_rect_t rect; 438 bool depressed; 439 errno_t rc; 440 441 if ((pbutton->flags & ui_pbf_no_text_depress) == 0) 442 depressed = pbutton->held && pbutton->inside; 443 else 444 depressed = false; 445 446 rc = gfx_set_color(pbutton->res->gc, pbutton->res->wnd_face_color); 447 if (rc != EOK) 448 goto error; 449 450 rc = gfx_fill_rect(pbutton->res->gc, &pbutton->rect); 451 if (rc != EOK) 452 goto error; 453 454 rect.p0.x = pbutton->rect.p0.x + (depressed ? 1 : 0); 455 rect.p0.y = pbutton->rect.p0.y; 456 rect.p1.x = pbutton->rect.p1.x - 1 + (depressed ? 1 : 0); 457 rect.p1.y = pbutton->rect.p0.y + 1; 458 459 rc = gfx_set_color(pbutton->res->gc, pbutton->res->btn_face_color); 460 if (rc != EOK) 461 goto error; 462 463 rc = gfx_fill_rect(pbutton->res->gc, &rect); 464 if (rc != EOK) 465 goto error; 466 467 /* Center of button rectangle */ 468 pos.x = (rect.p0.x + rect.p1.x) / 2; 469 pos.y = (rect.p0.y + rect.p1.y) / 2; 470 471 gfx_text_fmt_init(&fmt); 472 fmt.font = pbutton->res->font; 473 fmt.color = pbutton->res->btn_text_color; 474 fmt.halign = gfx_halign_center; 475 fmt.valign = gfx_valign_center; 476 fmt.abbreviate = true; 477 fmt.width = rect.p1.x - rect.p0.x - 2 * ui_pb_pad_x_text; 478 if (fmt.width < 1) 479 fmt.width = 1; 480 481 rc = gfx_puttext(&pos, &fmt, pbutton->caption); 482 if (rc != EOK) 483 goto error; 484 485 if (!depressed) { 486 rc = ui_pbutton_paint_text_shadow(pbutton); 487 if (rc != EOK) 488 goto error; 489 } 490 491 rc = gfx_update(pbutton->res->gc); 492 if (rc != EOK) 493 goto error; 494 495 return EOK; 496 error: 497 return rc; 498 } 499 500 /** Paint push button. 501 * 502 * @param pbutton Push button 503 * @return EOK on success or an error code 504 */ 505 errno_t ui_pbutton_paint(ui_pbutton_t *pbutton) 506 { 507 if (pbutton->ops != NULL && pbutton->ops->paint != NULL) { 508 /* Custom paint routine */ 509 return pbutton->ops->paint(pbutton, pbutton->ops_arg); 510 } else if (pbutton->res->textmode) { 511 return ui_pbutton_paint_text(pbutton); 512 } else { 513 return ui_pbutton_paint_gfx(pbutton); 514 } 515 } 516 311 517 /** Press down button. 312 518 * … … 321 527 pbutton->held = true; 322 528 (void) ui_pbutton_paint(pbutton); 529 ui_pbutton_down(pbutton); 323 530 } 324 531 … … 333 540 334 541 pbutton->held = false; 542 ui_pbutton_up(pbutton); 335 543 336 544 if (pbutton->inside) { … … 368 576 } 369 577 370 /** Button was clicked.578 /** Send button clicked event. 371 579 * 372 580 * @param pbutton Push button … … 376 584 if (pbutton->cb != NULL && pbutton->cb->clicked != NULL) 377 585 pbutton->cb->clicked(pbutton, pbutton->arg); 586 } 587 588 /** Send button down event. 589 * 590 * @param pbutton Push button 591 */ 592 void ui_pbutton_down(ui_pbutton_t *pbutton) 593 { 594 if (pbutton->cb != NULL && pbutton->cb->down != NULL) 595 pbutton->cb->down(pbutton, pbutton->arg); 596 } 597 598 /** Send button up event. 599 * 600 * @param pbutton Push button 601 */ 602 void ui_pbutton_up(ui_pbutton_t *pbutton) 603 { 604 if (pbutton->cb != NULL && pbutton->cb->up != NULL) 605 pbutton->cb->up(pbutton, pbutton->arg); 378 606 } 379 607 … … 415 643 } 416 644 break; 645 case POS_DCLICK: 646 break; 417 647 } 418 648
Note:
See TracChangeset
for help on using the changeset viewer.
