Changeset c9a7adc in mainline
- Timestamp:
- 2020-10-14T17:32:49Z (4 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 4ed00d3
- Parents:
- f6df5a3
- Location:
- uspace
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/uidemo/uidemo.c
rf6df5a3 rc9a7adc 34 34 35 35 #include <display.h> 36 #include <gfx/color.h> 37 #include <gfx/coord.h> 38 #include <gfx/render.h> 36 39 #include <stdio.h> 37 40 #include <str.h> … … 135 138 ui_demo_t demo; 136 139 gfx_rect_t rect; 140 gfx_color_t *color = NULL; 137 141 errno_t rc; 138 142 … … 184 188 ui_pbutton_set_rect(demo.pb1, &rect); 185 189 190 ui_pbutton_set_default(demo.pb1, true); 191 186 192 rc = ui_pbutton_create(ui_res, "Cancel", &demo.pb2); 187 193 if (rc != EOK) { … … 195 201 rect.p1.y = 80; 196 202 ui_pbutton_set_rect(demo.pb2, &rect); 203 204 rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &color); 205 if (rc != EOK) { 206 printf("Error allocating color.\n"); 207 return rc; 208 } 209 210 rc = gfx_set_color(gc, color); 211 if (rc != EOK) { 212 printf("Error setting color.\n"); 213 return rc; 214 } 215 216 rc = gfx_fill_rect(gc, ¶ms.rect); 217 if (rc != EOK) { 218 printf("Error filling background.\n"); 219 return rc; 220 } 221 222 gfx_color_delete(color); 223 color = NULL; 197 224 198 225 rc = ui_pbutton_paint(demo.pb1); -
uspace/lib/ui/include/ui/pbutton.h
rf6df5a3 rc9a7adc 41 41 #include <types/ui/pbutton.h> 42 42 #include <types/ui/resource.h> 43 #include <stdbool.h> 43 44 44 45 extern errno_t ui_pbutton_create(ui_resource_t *, const char *, … … 46 47 extern void ui_pbutton_destroy(ui_pbutton_t *); 47 48 extern void ui_pbutton_set_rect(ui_pbutton_t *, gfx_rect_t *); 49 extern void ui_pbutton_set_default(ui_pbutton_t *, bool); 48 50 extern errno_t ui_pbutton_paint(ui_pbutton_t *); 49 51 extern void ui_pbutton_press(ui_pbutton_t *); -
uspace/lib/ui/private/pbutton.h
rf6df5a3 rc9a7adc 53 53 /** Caption */ 54 54 const char *caption; 55 /** Button is selected as default */ 56 bool isdefault; 55 57 /** Button is currently held down */ 56 58 bool held; -
uspace/lib/ui/src/pbutton.c
rf6df5a3 rc9a7adc 47 47 /** Caption movement when button is pressed down */ 48 48 enum { 49 ui_pb_press_dx = 2,50 ui_pb_press_dy = 249 ui_pb_press_dx = 1, 50 ui_pb_press_dy = 1 51 51 }; 52 52 … … 100 100 } 101 101 102 /** Paint push button. 102 /** Set default flag. 103 * 104 * Default button is the one activated by Enter key, it is marked 105 * by thicker frame. 106 * 107 * @param pbutton Button 108 * @param isdefault @c true iff button is default 109 */ 110 void ui_pbutton_set_default(ui_pbutton_t *pbutton, bool isdefault) 111 { 112 pbutton->isdefault = isdefault; 113 } 114 115 /** Paint outer button frame. 103 116 * 104 117 * @param pbutton Push button 105 118 * @return EOK on success or an error code 106 119 */ 107 errno_t ui_pbutton_paint(ui_pbutton_t *pbutton)120 static errno_t ui_pbutton_paint_frame(ui_pbutton_t *pbutton) 108 121 { 109 122 gfx_color_t *color = NULL; 110 gfx_ coord2_t pos;111 gfx_ text_fmt_t fmt;123 gfx_rect_t rect; 124 gfx_coord_t thickness; 112 125 errno_t rc; 113 126 114 if (pbutton->held) { 115 rc = gfx_color_new_rgb_i16(0x8000, 0, 0, &color); 116 if (rc != EOK) 117 goto error; 118 } else { 119 rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &color); 120 if (rc != EOK) 121 goto error; 122 } 123 124 rc = gfx_set_color(pbutton->res->gc, color); 125 if (rc != EOK) 126 goto error; 127 128 rc = gfx_fill_rect(pbutton->res->gc, &pbutton->rect); 129 if (rc != EOK) 130 goto error; 131 132 gfx_color_delete(color); 127 thickness = pbutton->isdefault ? 2 : 1; 133 128 134 129 rc = gfx_color_new_rgb_i16(0, 0, 0, &color); … … 140 135 goto error; 141 136 142 /* Center of button rectangle */ 143 pos.x = (pbutton->rect.p0.x + pbutton->rect.p1.x) / 2; 144 pos.y = (pbutton->rect.p0.y + pbutton->rect.p1.y) / 2; 145 146 if (pbutton->held) { 147 pos.x += ui_pb_press_dx; 148 pos.y += ui_pb_press_dy; 149 } 150 151 gfx_text_fmt_init(&fmt); 152 fmt.halign = gfx_halign_center; 153 fmt.valign = gfx_valign_center; 154 155 rc = gfx_puttext(pbutton->res->font, &pos, &fmt, pbutton->caption); 156 if (rc != EOK) 157 goto error; 158 159 gfx_color_delete(color); 160 137 rect.p0.x = pbutton->rect.p0.x + 1; 138 rect.p0.y = pbutton->rect.p0.y; 139 rect.p1.x = pbutton->rect.p1.x - 1; 140 rect.p1.y = pbutton->rect.p0.y + thickness; 141 rc = gfx_fill_rect(pbutton->res->gc, &rect); 142 if (rc != EOK) 143 goto error; 144 145 rect.p0.x = pbutton->rect.p0.x + 1; 146 rect.p0.y = pbutton->rect.p1.y - thickness; 147 rect.p1.x = pbutton->rect.p1.x - 1; 148 rect.p1.y = pbutton->rect.p1.y; 149 rc = gfx_fill_rect(pbutton->res->gc, &rect); 150 if (rc != EOK) 151 goto error; 152 153 rect.p0.x = pbutton->rect.p0.x; 154 rect.p0.y = pbutton->rect.p0.y + 1; 155 rect.p1.x = pbutton->rect.p0.x + thickness; 156 rect.p1.y = pbutton->rect.p1.y - 1; 157 rc = gfx_fill_rect(pbutton->res->gc, &rect); 158 if (rc != EOK) 159 goto error; 160 161 rect.p0.x = pbutton->rect.p1.x - thickness; 162 rect.p0.y = pbutton->rect.p0.y + 1; 163 rect.p1.x = pbutton->rect.p1.x; 164 rect.p1.y = pbutton->rect.p1.y - 1; 165 rc = gfx_fill_rect(pbutton->res->gc, &rect); 166 if (rc != EOK) 167 goto error; 168 169 gfx_color_delete(color); 161 170 return EOK; 162 171 error: … … 166 175 } 167 176 177 /** Paint outset button bevel. 178 * 179 * @param pbutton Push button 180 * @return EOK on success or an error code 181 */ 182 static errno_t ui_pbutton_paint_outset(ui_pbutton_t *pbutton, 183 gfx_rect_t *rect) 184 { 185 gfx_color_t *color = NULL; 186 gfx_rect_t frect; 187 gfx_coord_t i; 188 errno_t rc; 189 190 /* Highlight */ 191 192 rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color); 193 if (rc != EOK) 194 goto error; 195 196 rc = gfx_set_color(pbutton->res->gc, color); 197 if (rc != EOK) 198 goto error; 199 200 for (i = 0; i < 2; i++) { 201 frect.p0.x = rect->p0.x + i; 202 frect.p0.y = rect->p0.y + i; 203 frect.p1.x = rect->p1.x - i - 1; 204 frect.p1.y = rect->p0.y + i + 1; 205 rc = gfx_fill_rect(pbutton->res->gc, &frect); 206 if (rc != EOK) 207 goto error; 208 209 frect.p0.x = rect->p0.x + i; 210 frect.p0.y = rect->p0.y + i + 1; 211 frect.p1.x = rect->p0.x + i + 1; 212 frect.p1.y = rect->p1.y - i - 1; 213 rc = gfx_fill_rect(pbutton->res->gc, &frect); 214 if (rc != EOK) 215 goto error; 216 } 217 218 gfx_color_delete(color); 219 color = NULL; 220 221 /* Shadow */ 222 223 rc = gfx_color_new_rgb_i16(0x8888, 0x8888, 0x8888, &color); 224 if (rc != EOK) 225 goto error; 226 227 rc = gfx_set_color(pbutton->res->gc, color); 228 if (rc != EOK) 229 goto error; 230 231 for (i = 0; i < 2; i++) { 232 frect.p0.x = rect->p0.x + i; 233 frect.p0.y = rect->p1.y - i - 1; 234 frect.p1.x = rect->p1.x - i - 1; 235 frect.p1.y = rect->p1.y - i; 236 rc = gfx_fill_rect(pbutton->res->gc, &frect); 237 if (rc != EOK) 238 goto error; 239 240 frect.p0.x = rect->p1.x - i - 1; 241 frect.p0.y = rect->p0.y + i; 242 frect.p1.x = rect->p1.x - i; 243 frect.p1.y = rect->p1.y - i; 244 rc = gfx_fill_rect(pbutton->res->gc, &frect); 245 if (rc != EOK) 246 goto error; 247 } 248 249 gfx_color_delete(color); 250 251 return EOK; 252 error: 253 if (color != NULL) 254 gfx_color_delete(color); 255 return rc; 256 } 257 258 /** Paint inset button bevel. 259 * 260 * @param pbutton Push button 261 * @return EOK on success or an error code 262 */ 263 static errno_t ui_pbutton_paint_inset(ui_pbutton_t *pbutton, 264 gfx_rect_t *rect) 265 { 266 gfx_color_t *color = NULL; 267 gfx_rect_t frect; 268 errno_t rc; 269 270 rc = gfx_color_new_rgb_i16(0x8888, 0x8888, 0x8888, &color); 271 if (rc != EOK) 272 goto error; 273 274 rc = gfx_set_color(pbutton->res->gc, color); 275 if (rc != EOK) 276 goto error; 277 278 frect.p0.x = rect->p0.x; 279 frect.p0.y = rect->p0.y; 280 frect.p1.x = rect->p1.x; 281 frect.p1.y = rect->p0.y + 2; 282 rc = gfx_fill_rect(pbutton->res->gc, &frect); 283 if (rc != EOK) 284 goto error; 285 286 frect.p0.x = rect->p0.x; 287 frect.p0.y = rect->p0.y + 2; 288 frect.p1.x = rect->p0.x + 2; 289 frect.p1.y = rect->p1.y; 290 rc = gfx_fill_rect(pbutton->res->gc, &frect); 291 if (rc != EOK) 292 goto error; 293 294 gfx_color_delete(color); 295 296 return EOK; 297 error: 298 if (color != NULL) 299 gfx_color_delete(color); 300 return rc; 301 } 302 303 /** Paint push button. 304 * 305 * @param pbutton Push button 306 * @return EOK on success or an error code 307 */ 308 errno_t ui_pbutton_paint(ui_pbutton_t *pbutton) 309 { 310 gfx_color_t *color = NULL; 311 gfx_coord2_t pos; 312 gfx_text_fmt_t fmt; 313 gfx_rect_t rect; 314 gfx_coord_t thickness; 315 errno_t rc; 316 317 thickness = pbutton->isdefault ? 2 : 1; 318 319 rect.p0.x = pbutton->rect.p0.x + thickness; 320 rect.p0.y = pbutton->rect.p0.y + thickness; 321 rect.p1.x = pbutton->rect.p1.x - thickness; 322 rect.p1.y = pbutton->rect.p1.y - thickness; 323 324 rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &color); 325 if (rc != EOK) 326 goto error; 327 328 rc = gfx_set_color(pbutton->res->gc, color); 329 if (rc != EOK) 330 goto error; 331 332 rc = gfx_fill_rect(pbutton->res->gc, &rect); 333 if (rc != EOK) 334 goto error; 335 336 gfx_color_delete(color); 337 color = NULL; 338 339 rc = gfx_color_new_rgb_i16(0, 0, 0, &color); 340 if (rc != EOK) 341 goto error; 342 343 rc = gfx_set_color(pbutton->res->gc, color); 344 if (rc != EOK) 345 goto error; 346 347 /* Center of button rectangle */ 348 pos.x = (rect.p0.x + rect.p1.x) / 2; 349 pos.y = (rect.p0.y + rect.p1.y) / 2; 350 351 if (pbutton->held) { 352 pos.x += ui_pb_press_dx; 353 pos.y += ui_pb_press_dy; 354 } 355 356 gfx_text_fmt_init(&fmt); 357 fmt.halign = gfx_halign_center; 358 fmt.valign = gfx_valign_center; 359 360 rc = gfx_puttext(pbutton->res->font, &pos, &fmt, pbutton->caption); 361 if (rc != EOK) 362 goto error; 363 364 gfx_color_delete(color); 365 color = NULL; 366 367 rc = ui_pbutton_paint_frame(pbutton); 368 if (rc != EOK) 369 goto error; 370 371 if (pbutton->held) { 372 rc = ui_pbutton_paint_inset(pbutton, &rect); 373 if (rc != EOK) 374 goto error; 375 } else { 376 rc = ui_pbutton_paint_outset(pbutton, &rect); 377 if (rc != EOK) 378 goto error; 379 } 380 381 return EOK; 382 error: 383 if (color != NULL) 384 gfx_color_delete(color); 385 return rc; 386 } 387 168 388 /** Press down button. 169 389 *
Note:
See TracChangeset
for help on using the changeset viewer.