Changeset 47728678 in mainline
- Timestamp:
- 2020-10-13T09:24:56Z (4 years ago)
- 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)
- Location:
- uspace
- Files:
-
- 5 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/uidemo/uidemo.c
rf80690a r47728678 36 36 #include <stdio.h> 37 37 #include <str.h> 38 #include <task.h> 38 39 #include <ui/pbutton.h> 40 #include <ui/resource.h> 41 42 static void wnd_close_event(void *); 43 static void wnd_kbd_event(void *, kbd_event_t *); 44 45 static display_wnd_cb_t wnd_cb = { 46 .close_event = wnd_close_event, 47 .kbd_event = wnd_kbd_event 48 }; 49 50 static bool quit = false; 39 51 40 52 static void print_syntax(void) … … 43 55 } 44 56 57 static void wnd_close_event(void *arg) 58 { 59 printf("Close event\n"); 60 quit = true; 61 } 62 63 static 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. */ 71 static 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(¶ms); 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, ¶ms, &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 45 170 int main(int argc, char *argv[]) 46 171 { 47 172 const char *display_svc = DISPLAY_DEFAULT; 48 ui_pbutton_t *pbutton;49 173 errno_t rc; 50 174 int i; … … 73 197 } 74 198 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 84 203 return 0; 85 204 } -
uspace/lib/ui/include/ui/pbutton.h
rf80690a r47728678 38 38 39 39 #include <errno.h> 40 #include <gfx/coord.h> 40 41 #include <types/ui/pbutton.h> 42 #include <types/ui/resource.h> 41 43 42 extern errno_t ui_pbutton_create(const char *, ui_pbutton_t **); 44 extern errno_t ui_pbutton_create(ui_resource_t *, const char *, 45 ui_pbutton_t **); 43 46 extern void ui_pbutton_destroy(ui_pbutton_t *); 47 extern void ui_pbutton_set_rect(ui_pbutton_t *, gfx_rect_t *); 48 extern errno_t ui_pbutton_paint(ui_pbutton_t *); 44 49 45 50 #endif -
uspace/lib/ui/meson.build
rf80690a r47728678 27 27 # 28 28 29 deps = [ 'gfx' ]29 deps = [ 'gfx', 'gfxfont' ] 30 30 src = files( 31 31 'src/pbutton.c', 32 'src/resource.c', 32 33 ) 33 34 … … 35 36 'test/main.c', 36 37 'test/pbutton.c', 38 'test/resource.c', 37 39 ) -
uspace/lib/ui/private/pbutton.h
rf80690a r47728678 38 38 #define _UI_PRIVATE_PBUTTON_H 39 39 40 #include <gfx/context.h> 41 #include <gfx/coord.h> 42 40 43 /** Actual structure of push button. 41 44 * … … 43 46 */ 44 47 struct ui_pbutton { 48 /** UI resource */ 49 struct ui_resource *res; 50 /** Push button rectangle */ 51 gfx_rect_t rect; 52 /** Caption */ 45 53 const char *caption; 46 54 }; -
uspace/lib/ui/src/pbutton.c
rf80690a r47728678 35 35 36 36 #include <errno.h> 37 #include <gfx/color.h> 38 #include <gfx/context.h> 39 #include <gfx/render.h> 40 #include <gfx/text.h> 37 41 #include <stdlib.h> 42 #include <str.h> 38 43 #include <ui/pbutton.h> 39 44 #include "../private/pbutton.h" 45 #include "../private/resource.h" 40 46 41 47 /** Create new push button. 42 48 * 49 * @param resource UI resource 43 50 * @param caption Caption 44 51 * @param rpbutton Place to store pointer to new push button 45 52 * @return EOK on success, ENOMEM if out of memory 46 53 */ 47 errno_t ui_pbutton_create(const char *caption, ui_pbutton_t **rpbutton) 54 errno_t ui_pbutton_create(ui_resource_t *resource, const char *caption, 55 ui_pbutton_t **rpbutton) 48 56 { 49 57 ui_pbutton_t *pbutton; … … 53 61 return ENOMEM; 54 62 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; 56 70 *rpbutton = pbutton; 57 71 return EOK; … … 70 84 } 71 85 86 /** Set button rectangle. 87 * 88 * @param pbutton Button 89 * @param rect New button rectanle 90 */ 91 void 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 */ 101 errno_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; 145 error: 146 if (color != NULL) 147 gfx_color_delete(color); 148 return rc; 149 } 150 72 151 /** @} 73 152 */ -
uspace/lib/ui/test/main.c
rf80690a r47728678 32 32 33 33 PCUT_IMPORT(pbutton); 34 PCUT_IMPORT(resource); 34 35 35 36 PCUT_MAIN(); -
uspace/lib/ui/test/pbutton.c
rf80690a r47728678 40 40 errno_t rc; 41 41 42 rc = ui_pbutton_create( "Hello", &pbutton);42 rc = ui_pbutton_create(NULL, "Hello", &pbutton); 43 43 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 44 44
Note:
See TracChangeset
for help on using the changeset viewer.