Changeset d8ddf7a in mainline


Ignore:
Timestamp:
2020-11-22T17:52:37Z (3 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.

Location:
uspace
Files:
8 edited

Legend:

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

    r4f64b7b8 rd8ddf7a  
    228228        }
    229229
    230         off.x = 5;
     230        off.x = 6;
    231231        off.y = 32;
    232232        gfx_rect_translate(&off, &logo_rect, &rect);
     233
     234        /* Adjust for frame width (2 x 1 pixel) */
     235        rect.p1.x += 2;
     236        rect.p1.y += 2;
    233237        ui_image_set_rect(launcher.image, &rect);
     238        ui_image_set_flags(launcher.image, ui_imgf_frame);
    234239
    235240        rc = ui_fixed_add(launcher.fixed, ui_image_ctl(launcher.image));
     
    238243                return rc;
    239244        }
     245
    240246        rc = ui_label_create(ui_res, "Launch application", &launcher.label);
    241247        if (rc != EOK) {
  • 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;
  • uspace/app/uidemo/uidemo.h

    r4f64b7b8 rd8ddf7a  
    3838
    3939#include <display.h>
     40#include <ui/entry.h>
    4041#include <ui/fixed.h>
    4142#include <ui/label.h>
     
    4950        ui_window_t *window;
    5051        ui_fixed_t *fixed;
     52        ui_entry_t *entry;
     53        ui_image_t *image;
    5154        ui_label_t *label;
    5255        ui_pbutton_t *pb1;
  • uspace/lib/ui/include/types/ui/image.h

    r4f64b7b8 rd8ddf7a  
    4040typedef struct ui_image ui_image_t;
    4141
     42/** UI image flags */
     43typedef enum {
     44        /** Draw a frame around the image */
     45        ui_imgf_frame = 0x1
     46} ui_image_flags_t;
     47
    4248#endif
    4349
  • uspace/lib/ui/include/ui/image.h

    r4f64b7b8 rd8ddf7a  
    5050extern void ui_image_set_bmp(ui_image_t *, gfx_bitmap_t *, gfx_rect_t *);
    5151extern void ui_image_set_rect(ui_image_t *, gfx_rect_t *);
     52extern void ui_image_set_flags(ui_image_t *, ui_image_flags_t);
    5253extern errno_t ui_image_paint(ui_image_t *);
    5354
  • uspace/lib/ui/private/image.h

    r4f64b7b8 rd8ddf7a  
    5252        /** Image rectangle */
    5353        gfx_rect_t rect;
     54        /** Flags */
     55        ui_image_flags_t flags;
    5456        /** Bitmap */
    5557        gfx_bitmap_t *bitmap;
     58        /** Bitmap rectangle */
    5659        gfx_rect_t brect;
    5760};
  • uspace/lib/ui/src/image.c

    r4f64b7b8 rd8ddf7a  
    124124}
    125125
     126/** Set image flags.
     127 *
     128 * @param image Image
     129 * @param flags Flags
     130 */
     131void ui_image_set_flags(ui_image_t *image, ui_image_flags_t flags)
     132{
     133        image->flags = flags;
     134}
     135
    126136/** Paint image.
    127137 *
     
    131141errno_t ui_image_paint(ui_image_t *image)
    132142{
     143        gfx_rect_t irect;
    133144        gfx_rect_t srect;
    134145        gfx_coord2_t offs;
     146        errno_t rc;
     147
     148        if ((image->flags & ui_imgf_frame) != 0) {
     149                rc = ui_paint_bevel(image->res->gc, &image->rect,
     150                    image->res->btn_frame_color, image->res->btn_frame_color,
     151                    1, NULL);
     152                if (rc != EOK)
     153                        return rc;
     154        }
    135155
    136156        if (image->bitmap == NULL)
    137157                return EOK;
     158
     159        irect = image->rect;
     160        if ((image->flags & ui_imgf_frame) != 0) {
     161                irect.p0.x++;
     162                irect.p0.y++;
     163                irect.p1.x--;
     164                irect.p1.y--;
     165        }
    138166
    139167        /*
     
    141169         * we need to subtract it.
    142170         */
    143         offs.x = image->rect.p0.x - image->brect.p0.x;
    144         offs.y = image->rect.p0.y - image->brect.p0.y;
     171        offs.x = irect.p0.x - image->brect.p0.x;
     172        offs.y = irect.p0.y - image->brect.p0.y;
    145173
    146174        /*
    147          * Transalte image rectangle back to bitmap coordinate space.
     175         * Translate image rectangle back to bitmap coordinate space.
    148176         * Thus the bitmap will be clipped to the image rectangle.
    149177         */
    150         gfx_rect_rtranslate(&offs, &image->rect, &srect);
     178        gfx_rect_rtranslate(&offs, &irect, &srect);
    151179        return gfx_bitmap_render(image->bitmap, &srect, &offs);
     180
    152181}
    153182
  • uspace/lib/ui/test/image.c

    r4f64b7b8 rd8ddf7a  
    110110}
    111111
     112/** Set image flags sets internal field */
     113PCUT_TEST(set_flags)
     114{
     115        ui_image_t *image = NULL;
     116        gfx_rect_t brect;
     117        errno_t rc;
     118
     119        rc = ui_image_create(NULL, NULL, &brect, &image);
     120        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     121        PCUT_ASSERT_NOT_NULL(image);
     122
     123        PCUT_ASSERT_INT_EQUALS(0, image->flags);
     124
     125        ui_image_set_flags(image, ui_imgf_frame);
     126        PCUT_ASSERT_INT_EQUALS(ui_imgf_frame, image->flags);
     127
     128        ui_image_destroy(image);
     129}
     130
    112131/** Set image bitmap */
    113132PCUT_TEST(set_bmp)
Note: See TracChangeset for help on using the changeset viewer.