Changeset d8ddf7a in mainline for uspace/app/uidemo/uidemo.c


Ignore:
Timestamp:
2020-11-22T17:52:37Z (4 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
2d879f7
Parents:
4f64b7b8
Message:

UI demo should demonstrate image and entry controls

We also add the ability to draw a frame around image control, use
in UI demo and in launcher.

File:
1 edited

Legend:

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

    r4f64b7b8 rd8ddf7a  
    3333 */
    3434
     35#include <gfx/bitmap.h>
    3536#include <gfx/coord.h>
     37#include <io/pixelmap.h>
    3638#include <stdio.h>
    3739#include <str.h>
     40#include <ui/entry.h>
    3841#include <ui/fixed.h>
     42#include <ui/image.h>
    3943#include <ui/label.h>
    4044#include <ui/pbutton.h>
     
    4448#include "uidemo.h"
    4549
     50static errno_t bitmap_moire(gfx_bitmap_t *, gfx_coord_t, gfx_coord_t);
     51
    4652static void wnd_close(ui_window_t *, void *);
    4753
     
    7985
    8086        if (pbutton == demo->pb1) {
    81                 rc = ui_label_set_text(demo->label, "Confirmed");
     87                rc = ui_entry_set_text(demo->entry, "OK pressed");
    8288                if (rc != EOK)
    83                         printf("Error changing label text.\n");
    84                 (void) ui_label_paint(demo->label);
     89                        printf("Error changing entry text.\n");
     90                (void) ui_entry_paint(demo->entry);
    8591        } else {
    86                 rc = ui_label_set_text(demo->label, "Cancelled");
     92                rc = ui_entry_set_text(demo->entry, "Cancel pressed");
    8793                if (rc != EOK)
    88                         printf("Error changing label text.\n");
    89                 (void) ui_label_paint(demo->label);
     94                        printf("Error changing entry text.\n");
     95                (void) ui_entry_paint(demo->entry);
    9096        }
    9197}
     
    99105        ui_demo_t demo;
    100106        gfx_rect_t rect;
     107        gfx_context_t *gc;
    101108        ui_resource_t *ui_res;
     109        gfx_bitmap_params_t bparams;
     110        gfx_bitmap_t *bitmap;
     111        gfx_coord2_t off;
    102112        errno_t rc;
    103113
     
    113123        params.rect.p0.y = 0;
    114124        params.rect.p1.x = 220;
    115         params.rect.p1.y = 100;
     125        params.rect.p1.y = 180;
    116126
    117127        memset((void *) &demo, 0, sizeof(demo));
     
    128138
    129139        ui_res = ui_window_get_res(window);
     140        gc = ui_window_get_gc(window);
    130141
    131142        rc = ui_fixed_create(&demo.fixed);
     
    135146        }
    136147
    137         rc = ui_label_create(ui_res, "Hello there!", &demo.label);
     148        rc = ui_label_create(ui_res, "Text label", &demo.label);
    138149        if (rc != EOK) {
    139150                printf("Error creating label.\n");
     
    154165        }
    155166
    156         rc = ui_pbutton_create(ui_res, "Confirm", &demo.pb1);
     167        rc = ui_pbutton_create(ui_res, "OK", &demo.pb1);
    157168        if (rc != EOK) {
    158169                printf("Error creating button.\n");
     
    163174
    164175        rect.p0.x = 15;
    165         rect.p0.y = 60;
     176        rect.p0.y = 70;
    166177        rect.p1.x = 105;
    167         rect.p1.y = 88;
     178        rect.p1.y = 98;
    168179        ui_pbutton_set_rect(demo.pb1, &rect);
    169180
     
    185196
    186197        rect.p0.x = 115;
    187         rect.p0.y = 60;
     198        rect.p0.y = 70;
    188199        rect.p1.x = 205;
    189         rect.p1.y = 88;
     200        rect.p1.y = 98;
    190201        ui_pbutton_set_rect(demo.pb2, &rect);
    191202
    192203        rc = ui_fixed_add(demo.fixed, ui_pbutton_ctl(demo.pb2));
     204        if (rc != EOK) {
     205                printf("Error adding control to layout.\n");
     206                return rc;
     207        }
     208
     209        rc = ui_entry_create(ui_res, "", &demo.entry);
     210        if (rc != EOK) {
     211                printf("Error creating entry.\n");
     212                return rc;
     213        }
     214
     215        rect.p0.x = 15;
     216        rect.p0.y = 110;
     217        rect.p1.x = 205;
     218        rect.p1.y = 135;
     219        ui_entry_set_rect(demo.entry, &rect);
     220        ui_entry_set_halign(demo.entry, gfx_halign_center);
     221
     222        rc = ui_fixed_add(demo.fixed, ui_entry_ctl(demo.entry));
     223        if (rc != EOK) {
     224                printf("Error adding control to layout.\n");
     225                return rc;
     226        }
     227
     228        gfx_bitmap_params_init(&bparams);
     229        bparams.rect.p0.x = 0;
     230        bparams.rect.p0.y = 0;
     231        bparams.rect.p1.x = 188;
     232        bparams.rect.p1.y = 24;
     233
     234        rc = gfx_bitmap_create(gc, &bparams, NULL, &bitmap);
     235        if (rc != EOK)
     236                return rc;
     237
     238        rc = bitmap_moire(bitmap, bparams.rect.p1.x, bparams.rect.p1.y);
     239        if (rc != EOK)
     240                return rc;
     241
     242        rc = ui_image_create(ui_res, bitmap, &params.rect, &demo.image);
     243        if (rc != EOK) {
     244                printf("Error creating label.\n");
     245                return rc;
     246        }
     247
     248        off.x = 15;
     249        off.y = 145;
     250        gfx_rect_translate(&off, &bparams.rect, &rect);
     251
     252        /* Adjust for frame width (2 x 1 pixel) */
     253        rect.p1.x += 2;
     254        rect.p1.y += 2;
     255        ui_image_set_rect(demo.image, &rect);
     256        ui_image_set_flags(demo.image, ui_imgf_frame);
     257
     258        rc = ui_fixed_add(demo.fixed, ui_image_ctl(demo.image));
    193259        if (rc != EOK) {
    194260                printf("Error adding control to layout.\n");
     
    208274        ui_window_destroy(window);
    209275        ui_destroy(ui);
     276
     277        return EOK;
     278}
     279
     280/** Fill bitmap with moire pattern.
     281 *
     282 * @param bitmap Bitmap
     283 * @param w Bitmap width
     284 * @param h Bitmap height
     285 * @return EOK on success or an error code
     286 */
     287static errno_t bitmap_moire(gfx_bitmap_t *bitmap, gfx_coord_t w, gfx_coord_t h)
     288{
     289        int i, j;
     290        int k;
     291        pixelmap_t pixelmap;
     292        gfx_bitmap_alloc_t alloc;
     293        errno_t rc;
     294
     295        rc = gfx_bitmap_get_alloc(bitmap, &alloc);
     296        if (rc != EOK)
     297                return rc;
     298
     299        /* In absence of anything else, use pixelmap */
     300        pixelmap.width = w;
     301        pixelmap.height = h;
     302        pixelmap.data = alloc.pixels;
     303
     304        for (i = 0; i < w; i++) {
     305                for (j = 0; j < h; j++) {
     306                        k = i * i + j * j;
     307                        pixelmap_put_pixel(&pixelmap, i, j,
     308                            PIXEL(255, k, k, 255 - k));
     309                }
     310        }
    210311
    211312        return EOK;
Note: See TracChangeset for help on using the changeset viewer.