Changeset 45208e22 in mainline
- Timestamp:
- 2024-03-13T09:07:04Z (6 months ago)
- Branches:
- master
- Children:
- 41e1258
- Parents:
- c3d4b39b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/viewer/viewer.c
rc3d4b39b r45208e22 40 40 #include <stdlib.h> 41 41 #include <str.h> 42 #include <ui/filedialog.h> 42 43 #include <ui/image.h> 43 44 #include <ui/ui.h> … … 60 61 ui_image_t *image; 61 62 gfx_context_t *window_gc; 63 ui_file_dialog_t *dialog; 62 64 63 65 gfx_rect_t img_rect; … … 67 69 gfx_rect_t *); 68 70 static bool viewer_img_setup(viewer_t *, gfx_bitmap_t *, gfx_rect_t *); 71 static errno_t viewer_window_create(viewer_t *); 69 72 static void viewer_window_destroy(viewer_t *); 70 73 … … 75 78 .close = wnd_close, 76 79 .kbd = wnd_kbd_event 80 }; 81 82 static void file_dialog_bok(ui_file_dialog_t *, void *, const char *); 83 static void file_dialog_bcancel(ui_file_dialog_t *, void *); 84 static void file_dialog_close(ui_file_dialog_t *, void *); 85 86 static ui_file_dialog_cb_t file_dialog_cb = { 87 .bok = file_dialog_bok, 88 .bcancel = file_dialog_bcancel, 89 .close = file_dialog_close 77 90 }; 78 91 … … 132 145 } 133 146 } 147 } 148 149 /** File dialog OK button press. 150 * 151 * @param dialog File dialog 152 * @param arg Argument (viewer_t *) 153 * @param fname File name 154 */ 155 static void file_dialog_bok(ui_file_dialog_t *dialog, void *arg, 156 const char *fname) 157 { 158 viewer_t *viewer = (viewer_t *) arg; 159 errno_t rc; 160 161 viewer->imgs_count = 1; 162 viewer->imgs = calloc(viewer->imgs_count, sizeof(char *)); 163 if (viewer->imgs == NULL) { 164 printf("Out of memory.\n"); 165 ui_quit(viewer->ui); 166 return; 167 } 168 169 viewer->imgs[0] = str_dup(fname); 170 if (viewer->imgs[0] == NULL) { 171 printf("Out of memory.\n"); 172 ui_quit(viewer->ui); 173 return; 174 } 175 176 rc = viewer_window_create(viewer); 177 if (rc != EOK) 178 ui_quit(viewer->ui); 179 180 ui_file_dialog_destroy(dialog); 181 viewer->dialog = NULL; 182 } 183 184 /** File dialog cancel button press. 185 * 186 * @param dialog File dialog 187 * @param arg Argument (viewer_t *) 188 */ 189 static void file_dialog_bcancel(ui_file_dialog_t *dialog, void *arg) 190 { 191 viewer_t *viewer = (viewer_t *) arg; 192 193 ui_file_dialog_destroy(dialog); 194 ui_quit(viewer->ui); 195 } 196 197 /** File dialog close request. 198 * 199 * @param dialog File dialog 200 * @param arg Argument (viewer_t *) 201 */ 202 static void file_dialog_close(ui_file_dialog_t *dialog, void *arg) 203 { 204 viewer_t *viewer = (viewer_t *) arg; 205 206 ui_file_dialog_destroy(dialog); 207 ui_quit(viewer->ui); 134 208 } 135 209 … … 295 369 error: 296 370 viewer_window_destroy(viewer); 371 ui_quit(viewer->ui); 297 372 return rc; 298 373 } … … 313 388 int i; 314 389 unsigned u; 390 ui_file_dialog_params_t fdparams; 315 391 316 392 viewer = calloc(1, sizeof(viewer_t)); … … 341 417 } 342 418 343 if (i >= argc) { 344 printf("No image files specified.\n"); 345 print_syntax(); 346 goto error; 347 } 348 349 viewer->imgs_count = argc - i; 350 viewer->imgs = calloc(viewer->imgs_count, sizeof(char *)); 351 if (viewer->imgs == NULL) { 352 printf("Out of memory.\n"); 353 goto error; 354 } 355 356 for (int j = 0; j < argc - i; j++) { 357 viewer->imgs[j] = str_dup(argv[i + j]); 358 if (viewer->imgs[j] == NULL) { 419 /* Images specified? */ 420 if (i < argc) { 421 viewer->imgs_count = argc - i; 422 viewer->imgs = calloc(viewer->imgs_count, sizeof(char *)); 423 if (viewer->imgs == NULL) { 359 424 printf("Out of memory.\n"); 360 425 goto error; 426 } 427 428 for (int j = 0; j < argc - i; j++) { 429 viewer->imgs[j] = str_dup(argv[i + j]); 430 if (viewer->imgs[j] == NULL) { 431 printf("Out of memory.\n"); 432 goto error; 433 } 361 434 } 362 435 } … … 373 446 viewer->ui = ui; 374 447 375 rc = viewer_window_create(viewer); 376 if (rc != EOK) 377 goto error; 448 if (viewer->imgs != NULL) { 449 /* We have images, create viewer window. */ 450 rc = viewer_window_create(viewer); 451 if (rc != EOK) 452 goto error; 453 } else { 454 /* No images specified, browse for one. */ 455 ui_file_dialog_params_init(&fdparams); 456 fdparams.caption = "Open Image"; 457 458 rc = ui_file_dialog_create(viewer->ui, &fdparams, 459 &viewer->dialog); 460 if (rc != EOK) { 461 printf("Error creating file dialog.\n"); 462 goto error; 463 } 464 465 ui_file_dialog_set_cb(viewer->dialog, &file_dialog_cb, 466 (void *)viewer); 467 } 378 468 379 469 ui_run(ui); … … 385 475 return 0; 386 476 error: 477 if (viewer != NULL && viewer->dialog != NULL) 478 ui_file_dialog_destroy(viewer->dialog); 387 479 if (viewer != NULL && viewer->imgs != NULL) { 388 480 for (u = 0; u < viewer->imgs_count; u++) {
Note:
See TracChangeset
for help on using the changeset viewer.