image_widget.c (2168B)
1 /* 2 * Copyright (c) 2023-2024 lumidify <nobody@lumidify.org> 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #include <stddef.h> 18 19 #include "err.h" 20 #include "ltkd.h" 21 #include "widget.h" 22 #include "cmd.h" 23 24 #include <ltk/ltk.h> 25 #include <ltk/util.h> 26 #include <ltk/image.h> 27 #include <ltk/image_widget.h> 28 29 /* image <image id> create <filename> <data> */ 30 static int 31 ltkd_image_widget_cmd_create( 32 ltk_window *window, 33 ltkd_widget *widget_unneeded, 34 ltkd_cmd_token *tokens, 35 size_t num_tokens, 36 ltkd_error *err) { 37 (void)widget_unneeded; 38 ltkd_cmdarg_parseinfo cmd[] = { 39 {.type = CMDARG_IGNORE, .optional = 0}, 40 {.type = CMDARG_STRING, .optional = 0}, 41 {.type = CMDARG_IGNORE, .optional = 0}, 42 {.type = CMDARG_STRING, .optional = 0}, 43 {.type = CMDARG_DATA, .optional = 0}, 44 }; 45 if (ltkd_parse_cmd(tokens, num_tokens, cmd, LENGTH(cmd), err)) 46 return 1; 47 ltk_image *img = ltk_image_create_from_mem(cmd[3].val.str, cmd[4].val.data, cmd[4].len); 48 if (!img) { 49 /* FIXME: more sensible error name */ 50 err->type = ERR_UNKNOWN; 51 err->arg = -1; 52 return 1; 53 } 54 ltk_image_widget *imgw = ltk_image_widget_create(window, img); 55 if (!ltkd_widget_create(LTK_CAST_WIDGET(imgw), cmd[1].val.str, NULL, 0, err)) { 56 ltk_widget_destroy(LTK_CAST_WIDGET(imgw), 1); 57 err->arg = 1; 58 return 1; 59 } 60 61 return 0; 62 } 63 64 static ltkd_cmd_info image_widget_cmds[] = { 65 {"create", <kd_image_widget_cmd_create, 1}, 66 }; 67 68 GEN_CMD_HELPERS(ltkd_image_widget_cmd, LTK_WIDGET_IMAGE, image_widget_cmds)