Changeset 47728678 in mainline


Ignore:
Timestamp:
2020-10-13T09:24:56Z (4 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f6df5a3
Parents:
f80690a
git-author:
Jiri Svoboda <jiri@…> (2020-10-12 21:24:39)
git-committer:
Jiri Svoboda <jiri@…> (2020-10-13 09:24:56)
Message:

Push button - first light

Introduced ui_resource_t class to hold common UI resources such as font,
colors, etc. We don't want every button to load its own copy of the font.

Location:
uspace
Files:
5 added
7 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/uidemo/uidemo.c

    rf80690a r47728678  
    3636#include <stdio.h>
    3737#include <str.h>
     38#include <task.h>
    3839#include <ui/pbutton.h>
     40#include <ui/resource.h>
     41
     42static void wnd_close_event(void *);
     43static void wnd_kbd_event(void *, kbd_event_t *);
     44
     45static display_wnd_cb_t wnd_cb = {
     46        .close_event = wnd_close_event,
     47        .kbd_event = wnd_kbd_event
     48};
     49
     50static bool quit = false;
    3951
    4052static void print_syntax(void)
     
    4355}
    4456
     57static void wnd_close_event(void *arg)
     58{
     59        printf("Close event\n");
     60        quit = true;
     61}
     62
     63static void wnd_kbd_event(void *arg, kbd_event_t *event)
     64{
     65        printf("Keyboard event type=%d key=%d\n", event->type, event->key);
     66        if (event->type == KEY_PRESS)
     67                quit = true;
     68}
     69
     70/** Run UI demo on display server. */
     71static errno_t ui_demo_display(const char *display_svc)
     72{
     73        display_t *display = NULL;
     74        gfx_context_t *gc;
     75        display_wnd_params_t params;
     76        display_window_t *window = NULL;
     77        ui_resource_t *ui_res;
     78        ui_pbutton_t *pb1;
     79        ui_pbutton_t *pb2;
     80        gfx_rect_t rect;
     81        errno_t rc;
     82
     83        printf("Init display..\n");
     84
     85        rc = display_open(display_svc, &display);
     86        if (rc != EOK) {
     87                printf("Error opening display.\n");
     88                return rc;
     89        }
     90
     91        display_wnd_params_init(&params);
     92        params.rect.p0.x = 0;
     93        params.rect.p0.y = 0;
     94        params.rect.p1.x = 220;
     95        params.rect.p1.y = 100;
     96
     97        rc = display_window_create(display, &params, &wnd_cb, NULL, &window);
     98        if (rc != EOK) {
     99                printf("Error creating window.\n");
     100                return rc;
     101        }
     102
     103        rc = display_window_get_gc(window, &gc);
     104        if (rc != EOK) {
     105                printf("Error getting graphics context.\n");
     106                return rc;
     107        }
     108
     109        task_retval(0);
     110
     111        rc = ui_resource_create(gc, &ui_res);
     112        if (rc != EOK) {
     113                printf("Error creating UI.\n");
     114                return 1;
     115        }
     116
     117        rc = ui_pbutton_create(ui_res, "Confirm", &pb1);
     118        if (rc != EOK) {
     119                printf("Error creating button.\n");
     120                return 1;
     121        }
     122
     123        rect.p0.x = 20;
     124        rect.p0.y = 50;
     125        rect.p1.x = 100;
     126        rect.p1.y = 80;
     127        ui_pbutton_set_rect(pb1, &rect);
     128
     129        rc = ui_pbutton_create(ui_res, "Cancel", &pb2);
     130        if (rc != EOK) {
     131                printf("Error creating button.\n");
     132                return 1;
     133        }
     134
     135        rect.p0.x = 120;
     136        rect.p0.y = 50;
     137        rect.p1.x = 200;
     138        rect.p1.y = 80;
     139        ui_pbutton_set_rect(pb2, &rect);
     140
     141        rc = ui_pbutton_paint(pb1);
     142        if (rc != EOK) {
     143                printf("Error painting button.\n");
     144                return 1;
     145        }
     146
     147        rc = ui_pbutton_paint(pb2);
     148        if (rc != EOK) {
     149                printf("Error painting button.\n");
     150                return 1;
     151        }
     152
     153        while (!quit) {
     154                fibril_usleep(100 * 1000);
     155        }
     156
     157        ui_pbutton_destroy(pb1);
     158        ui_pbutton_destroy(pb2);
     159
     160        rc = gfx_context_delete(gc);
     161        if (rc != EOK)
     162                return rc;
     163
     164        display_window_destroy(window);
     165        display_close(display);
     166
     167        return EOK;
     168}
     169
    45170int main(int argc, char *argv[])
    46171{
    47172        const char *display_svc = DISPLAY_DEFAULT;
    48         ui_pbutton_t *pbutton;
    49173        errno_t rc;
    50174        int i;
     
    73197        }
    74198
    75         printf("Display service: %s\n", display_svc);
    76 
    77         rc = ui_pbutton_create("Hello", &pbutton);
    78         if (rc != EOK) {
    79                 printf("Error creating button.\n");
    80                 return 1;
    81         }
    82 
    83         ui_pbutton_destroy(pbutton);
     199        rc = ui_demo_display(display_svc);
     200        if (rc != EOK)
     201                return 1;
     202
    84203        return 0;
    85204}
  • uspace/lib/ui/include/ui/pbutton.h

    rf80690a r47728678  
    3838
    3939#include <errno.h>
     40#include <gfx/coord.h>
    4041#include <types/ui/pbutton.h>
     42#include <types/ui/resource.h>
    4143
    42 extern errno_t ui_pbutton_create(const char *, ui_pbutton_t **);
     44extern errno_t ui_pbutton_create(ui_resource_t *, const char *,
     45    ui_pbutton_t **);
    4346extern void ui_pbutton_destroy(ui_pbutton_t *);
     47extern void ui_pbutton_set_rect(ui_pbutton_t *, gfx_rect_t *);
     48extern errno_t ui_pbutton_paint(ui_pbutton_t *);
    4449
    4550#endif
  • uspace/lib/ui/meson.build

    rf80690a r47728678  
    2727#
    2828
    29 deps = [ 'gfx' ]
     29deps = [ 'gfx', 'gfxfont' ]
    3030src = files(
    3131        'src/pbutton.c',
     32        'src/resource.c',
    3233)
    3334
     
    3536        'test/main.c',
    3637        'test/pbutton.c',
     38        'test/resource.c',
    3739)
  • uspace/lib/ui/private/pbutton.h

    rf80690a r47728678  
    3838#define _UI_PRIVATE_PBUTTON_H
    3939
     40#include <gfx/context.h>
     41#include <gfx/coord.h>
     42
    4043/** Actual structure of push button.
    4144 *
     
    4346 */
    4447struct ui_pbutton {
     48        /** UI resource */
     49        struct ui_resource *res;
     50        /** Push button rectangle */
     51        gfx_rect_t rect;
     52        /** Caption */
    4553        const char *caption;
    4654};
  • uspace/lib/ui/src/pbutton.c

    rf80690a r47728678  
    3535
    3636#include <errno.h>
     37#include <gfx/color.h>
     38#include <gfx/context.h>
     39#include <gfx/render.h>
     40#include <gfx/text.h>
    3741#include <stdlib.h>
     42#include <str.h>
    3843#include <ui/pbutton.h>
    3944#include "../private/pbutton.h"
     45#include "../private/resource.h"
    4046
    4147/** Create new push button.
    4248 *
     49 * @param resource UI resource
    4350 * @param caption Caption
    4451 * @param rpbutton Place to store pointer to new push button
    4552 * @return EOK on success, ENOMEM if out of memory
    4653 */
    47 errno_t ui_pbutton_create(const char *caption, ui_pbutton_t **rpbutton)
     54errno_t ui_pbutton_create(ui_resource_t *resource, const char *caption,
     55    ui_pbutton_t **rpbutton)
    4856{
    4957        ui_pbutton_t *pbutton;
     
    5361                return ENOMEM;
    5462
    55         (void) caption;
     63        pbutton->caption = str_dup(caption);
     64        if (pbutton->caption == NULL) {
     65                free(pbutton);
     66                return ENOMEM;
     67        }
     68
     69        pbutton->res = resource;
    5670        *rpbutton = pbutton;
    5771        return EOK;
     
    7084}
    7185
     86/** Set button rectangle.
     87 *
     88 * @param pbutton Button
     89 * @param rect New button rectanle
     90 */
     91void ui_pbutton_set_rect(ui_pbutton_t *pbutton, gfx_rect_t *rect)
     92{
     93        pbutton->rect = *rect;
     94}
     95
     96/** Paint push button.
     97 *
     98 * @param pbutton Push button
     99 * @return EOK on success or an error code
     100 */
     101errno_t ui_pbutton_paint(ui_pbutton_t *pbutton)
     102{
     103        gfx_color_t *color = NULL;
     104        gfx_coord2_t pos;
     105        gfx_text_fmt_t fmt;
     106        errno_t rc;
     107
     108        rc = gfx_color_new_rgb_i16(0xc8c8, 0xc8c8, 0xc8c8, &color);
     109        if (rc != EOK)
     110                goto error;
     111
     112        rc = gfx_set_color(pbutton->res->gc, color);
     113        if (rc != EOK)
     114                goto error;
     115
     116        rc = gfx_fill_rect(pbutton->res->gc, &pbutton->rect);
     117        if (rc != EOK)
     118                goto error;
     119
     120        gfx_color_delete(color);
     121
     122        rc = gfx_color_new_rgb_i16(0, 0, 0, &color);
     123        if (rc != EOK)
     124                goto error;
     125
     126        rc = gfx_set_color(pbutton->res->gc, color);
     127        if (rc != EOK)
     128                goto error;
     129
     130        /* Center of button rectangle */
     131        pos.x = (pbutton->rect.p0.x + pbutton->rect.p1.x) / 2;
     132        pos.y = (pbutton->rect.p0.y + pbutton->rect.p1.y) / 2;
     133
     134        gfx_text_fmt_init(&fmt);
     135        fmt.halign = gfx_halign_center;
     136        fmt.valign = gfx_valign_center;
     137
     138        rc = gfx_puttext(pbutton->res->font, &pos, &fmt, pbutton->caption);
     139        if (rc != EOK)
     140                goto error;
     141
     142        gfx_color_delete(color);
     143
     144        return EOK;
     145error:
     146        if (color != NULL)
     147                gfx_color_delete(color);
     148        return rc;
     149}
     150
    72151/** @}
    73152 */
  • uspace/lib/ui/test/main.c

    rf80690a r47728678  
    3232
    3333PCUT_IMPORT(pbutton);
     34PCUT_IMPORT(resource);
    3435
    3536PCUT_MAIN();
  • uspace/lib/ui/test/pbutton.c

    rf80690a r47728678  
    4040        errno_t rc;
    4141
    42         rc = ui_pbutton_create("Hello", &pbutton);
     42        rc = ui_pbutton_create(NULL, "Hello", &pbutton);
    4343        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    4444
Note: See TracChangeset for help on using the changeset viewer.