Changeset 9a07ee3 in mainline
- Timestamp:
- 2024-11-12T09:56:19Z (3 weeks ago)
- Branches:
- master
- Children:
- 12dd36c
- Parents:
- 0d00e53
- git-author:
- Jiri Svoboda <jiri@…> (2024-11-11 19:56:06)
- git-committer:
- Jiri Svoboda <jiri@…> (2024-11-12 09:56:19)
- Location:
- uspace
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/uidemo/uidemo.c
r0d00e53 r9a07ee3 1 1 /* 2 * Copyright (c) 202 3Jiri Svoboda2 * Copyright (c) 2024 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 108 108 static void uidemo_file_load(ui_menu_entry_t *, void *); 109 109 static void uidemo_file_message(ui_menu_entry_t *, void *); 110 static void uidemo_file_confirmation(ui_menu_entry_t *, void *); 110 111 static void uidemo_file_exit(ui_menu_entry_t *, void *); 111 112 static void uidemo_edit_modify(ui_menu_entry_t *, void *); … … 340 341 } 341 342 342 /** Display a message window .343 /** Display a message window with OK button. 343 344 * 344 345 * @param demo UI demo … … 405 406 mdparams.caption = "Message For You"; 406 407 mdparams.text = "Hello, world!"; 408 409 rc = ui_msg_dialog_create(demo->ui, &mdparams, &dialog); 410 if (rc != EOK) { 411 printf("Error creating message dialog.\n"); 412 return; 413 } 414 415 ui_msg_dialog_set_cb(dialog, &msg_dialog_cb, &demo); 416 } 417 418 /** File / Confirmation menu entry selected. 419 * 420 * @param mentry Menu entry 421 * @param arg Argument (demo) 422 */ 423 static void uidemo_file_confirmation(ui_menu_entry_t *mentry, void *arg) 424 { 425 ui_demo_t *demo = (ui_demo_t *) arg; 426 ui_msg_dialog_params_t mdparams; 427 ui_msg_dialog_t *dialog; 428 errno_t rc; 429 430 ui_msg_dialog_params_init(&mdparams); 431 mdparams.caption = "Confirmation"; 432 mdparams.text = "This will not actually do anything. Proceed?"; 433 mdparams.choice = umdc_ok_cancel; 407 434 408 435 rc = ui_msg_dialog_create(demo->ui, &mdparams, &dialog); … … 782 809 ui_menu_entry_set_cb(mmsg, uidemo_file_message, (void *) &demo); 783 810 811 rc = ui_menu_entry_create(demo.mfile, "~C~onfirmation", "", &mmsg); 812 if (rc != EOK) { 813 printf("Error creating menu.\n"); 814 return rc; 815 } 816 817 ui_menu_entry_set_cb(mmsg, uidemo_file_confirmation, (void *) &demo); 818 784 819 rc = ui_menu_entry_create(demo.mfile, "~L~oad", "", &mload); 785 820 if (rc != EOK) { -
uspace/lib/ui/include/types/ui/msgdialog.h
r0d00e53 r9a07ee3 1 1 /* 2 * Copyright (c) 202 1Jiri Svoboda2 * Copyright (c) 2024 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 45 45 typedef struct ui_msg_dialog ui_msg_dialog_t; 46 46 47 enum { 48 /** Maximum number of buttons in message dialog. */ 49 ui_msg_dialog_maxbtn = 2 50 }; 51 52 /** Which choices the user can select from. */ 53 typedef enum { 54 /** OK (the default) */ 55 umdc_ok, 56 /** OK, Cancel */ 57 umdc_ok_cancel 58 } ui_msg_dialog_choice_t; 59 47 60 /** Message dialog parameters */ 48 61 typedef struct { … … 51 64 /** Message text */ 52 65 const char *text; 66 /** The choice that the user is given */ 67 ui_msg_dialog_choice_t choice; 53 68 } ui_msg_dialog_params_t; 54 69 -
uspace/lib/ui/private/msgdialog.h
r0d00e53 r9a07ee3 1 1 /* 2 * Copyright (c) 202 1Jiri Svoboda2 * Copyright (c) 2024 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 38 38 #define _UI_PRIVATE_MSGDIALOG_H 39 39 40 #include <types/ui/msgdialog.h> 41 #include <ui/pbutton.h> 42 #include <ui/window.h> 43 40 44 /** Actual structure of message dialog. 41 45 * … … 45 49 /** Dialog window */ 46 50 struct ui_window *window; 47 /** OK button*/48 struct ui_pbutton *b ok;51 /** Buttons */ 52 struct ui_pbutton *btn[ui_msg_dialog_maxbtn]; 49 53 /** Message dialog callbacks */ 50 54 struct ui_msg_dialog_cb *cb; -
uspace/lib/ui/src/msgdialog.c
r0d00e53 r9a07ee3 1 1 /* 2 * Copyright (c) 202 1Jiri Svoboda2 * Copyright (c) 2024 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 58 58 }; 59 59 60 static const char *ui_msg_dialog_captions[][ui_msg_dialog_maxbtn + 1] = { 61 [umdc_ok] = { "OK", NULL }, 62 [umdc_ok_cancel] = { "OK", "Cancel", NULL } 63 }; 64 60 65 /** Initialize message dialog parameters structure. 61 66 * … … 86 91 ui_fixed_t *fixed = NULL; 87 92 ui_label_t *label = NULL; 88 ui_pbutton_t *bok = NULL; 93 ui_pbutton_t *btn[ui_msg_dialog_maxbtn]; 94 const char **cp; 95 unsigned i, nb; 89 96 gfx_rect_t rect; 97 gfx_coord_t bw, bpad, btnsw, bp0x; 90 98 ui_resource_t *ui_res; 99 100 for (i = 0; i < ui_msg_dialog_maxbtn; i++) 101 btn[i] = NULL; 91 102 92 103 dialog = calloc(1, sizeof(ui_msg_dialog_t)); … … 103 114 wparams.rect.p0.x = 0; 104 115 wparams.rect.p0.y = 0; 105 wparams.rect.p1.x = 40;116 wparams.rect.p1.x = 60; 106 117 wparams.rect.p1.y = 7; 107 118 } else { 108 119 wparams.rect.p0.x = 0; 109 120 wparams.rect.p0.y = 0; 110 wparams.rect.p1.x = 200;121 wparams.rect.p1.x = 400; 111 122 wparams.rect.p1.y = 110; 112 123 } … … 132 143 rect.p0.x = 3; 133 144 rect.p0.y = 2; 134 rect.p1.x = 17;145 rect.p1.x = 57; 135 146 rect.p1.y = 3; 136 147 } else { 137 148 rect.p0.x = 10; 138 149 rect.p0.y = 35; 139 rect.p1.x = 190;150 rect.p1.x = 390; 140 151 rect.p1.y = 50; 141 152 } … … 150 161 label = NULL; 151 162 152 rc = ui_pbutton_create(ui_res, "OK", &bok); 153 if (rc != EOK) 154 goto error; 155 156 ui_pbutton_set_cb(bok, &ui_msg_dialog_btn_cb, dialog); 157 158 /* FIXME: Auto layout */ 163 i = 0; 164 assert(params->choice == umdc_ok || params->choice == umdc_ok_cancel); 165 cp = ui_msg_dialog_captions[params->choice]; 166 167 while (*cp != NULL) { 168 rc = ui_pbutton_create(ui_res, *cp, &btn[i]); 169 if (rc != EOK) 170 goto error; 171 172 ui_pbutton_set_cb(btn[i], &ui_msg_dialog_btn_cb, dialog); 173 ++cp; 174 ++i; 175 } 176 177 nb = i; 178 159 179 if (ui_is_textmode(ui)) { 160 rect.p0.x = 8; 161 rect.p0.y = 4; 162 rect.p1.x = 12; 163 rect.p1.y = 5; 180 bw = 12; 181 bpad = 2; 164 182 } else { 165 rect.p0.x = 55; 166 rect.p0.y = 60; 167 rect.p1.x = 145; 168 rect.p1.y = 88; 169 } 170 171 ui_pbutton_set_rect(bok, &rect); 172 173 ui_pbutton_set_default(bok, true); 174 175 rc = ui_fixed_add(fixed, ui_pbutton_ctl(bok)); 176 if (rc != EOK) 177 goto error; 178 179 dialog->bok = bok; 180 bok = NULL; 183 bw = 90; 184 bpad = 10; 185 } 186 187 btnsw = (nb - 1) * (bw + bpad) + bw; 188 bp0x = (wparams.rect.p0.x + wparams.rect.p1.x - btnsw) / 2; 189 190 for (i = 0; i < nb; i++) { 191 /* FIXME: Auto layout */ 192 if (ui_is_textmode(ui)) { 193 rect.p0.x = bp0x + i * (bw + bpad); 194 rect.p0.y = 4; 195 rect.p1.x = bp0x + bw + i * (bw + bpad); 196 rect.p1.y = 5; 197 } else { 198 rect.p0.x = bp0x + i * (bw + bpad); 199 rect.p0.y = 60; 200 rect.p1.x = bp0x + bw + i * (bw + bpad); 201 rect.p1.y = 88; 202 } 203 204 ui_pbutton_set_rect(btn[i], &rect); 205 206 rc = ui_fixed_add(fixed, ui_pbutton_ctl(btn[i])); 207 if (rc != EOK) 208 goto error; 209 } 210 211 ui_pbutton_set_default(btn[0], true); 212 213 for (i = 0; i < ui_msg_dialog_maxbtn; i++) { 214 dialog->btn[i] = btn[i]; 215 btn[i] = NULL; 216 } 181 217 182 218 ui_window_add(window, ui_fixed_ctl(fixed)); … … 191 227 return EOK; 192 228 error: 193 if (bok != NULL) 194 ui_pbutton_destroy(bok); 229 for (i = 0; i < ui_msg_dialog_maxbtn; i++) { 230 if (btn[i] != NULL) 231 ui_pbutton_destroy(btn[i]); 232 } 195 233 if (label != NULL) 196 234 ui_label_destroy(label); … … 251 289 { 252 290 ui_msg_dialog_t *dialog = (ui_msg_dialog_t *) arg; 253 254 if (dialog->cb != NULL && dialog->cb->button != NULL) 255 dialog->cb->button(dialog, dialog->arg, 0); 291 unsigned i; 292 293 if (dialog->cb != NULL && dialog->cb->button != NULL) { 294 for (i = 0; i < ui_msg_dialog_maxbtn; i++) { 295 if (dialog->btn[i] == pbutton) 296 dialog->cb->button(dialog, dialog->arg, i); 297 } 298 } 256 299 } 257 300 -
uspace/lib/ui/test/msgdialog.c
r0d00e53 r9a07ee3 1 1 /* 2 * Copyright (c) 202 1Jiri Svoboda2 * Copyright (c) 2024 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 92 92 ui_msg_dialog_params_t params; 93 93 ui_msg_dialog_t *dialog = NULL; 94 unsigned i; 94 95 test_cb_resp_t resp; 95 96 … … 100 101 params.caption = "Message"; 101 102 params.text = "Hello"; 103 params.choice = umdc_ok_cancel; 102 104 103 105 rc = ui_msg_dialog_create(ui, ¶ms, &dialog); … … 106 108 107 109 /* Button callback with no callbacks set */ 108 ui_pbutton_clicked(dialog->b ok);110 ui_pbutton_clicked(dialog->btn[0]); 109 111 110 112 /* Button callback with callback not implemented */ 111 113 ui_msg_dialog_set_cb(dialog, &dummy_msg_dialog_cb, NULL); 112 ui_pbutton_clicked(dialog->b ok);114 ui_pbutton_clicked(dialog->btn[0]); 113 115 114 /* Button callback with real callback set */ 115 resp.button = false; 116 resp.bnum = 123; 117 ui_msg_dialog_set_cb(dialog, &test_msg_dialog_cb, &resp); 118 ui_pbutton_clicked(dialog->bok); 119 PCUT_ASSERT_TRUE(resp.button); 120 PCUT_ASSERT_INT_EQUALS(0, resp.bnum); 116 for (i = 0; i < 2; i++) { 117 /* Button callback with real callback set */ 118 resp.button = false; 119 resp.bnum = 123; 120 ui_msg_dialog_set_cb(dialog, &test_msg_dialog_cb, &resp); 121 ui_pbutton_clicked(dialog->btn[i]); 122 PCUT_ASSERT_TRUE(resp.button); 123 PCUT_ASSERT_INT_EQUALS(i, resp.bnum); 124 } 121 125 122 126 ui_msg_dialog_destroy(dialog);
Note:
See TracChangeset
for help on using the changeset viewer.