box.c (3184B)
1 /* 2 * Copyright (c) 2021-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/box.h> 27 28 /* box <box id> add <widget id> [sticky] */ 29 static int 30 ltkd_box_cmd_add( 31 ltk_window *window, 32 ltkd_widget *widget, 33 ltkd_cmd_token *tokens, 34 size_t num_tokens, 35 ltkd_error *err) { 36 (void)window; 37 ltk_box *box = LTK_CAST_BOX(widget->widget); 38 ltkd_cmdarg_parseinfo cmd[] = { 39 {.type = CMDARG_WIDGET, .widget_type = LTK_WIDGET_UNKNOWN, .optional = 0}, 40 {.type = CMDARG_STICKY, .optional = 1}, 41 }; 42 if (ltkd_parse_cmd(tokens, num_tokens, cmd, LENGTH(cmd), err)) 43 return 1; 44 if (ltk_box_add(box, cmd[0].val.widget->widget, cmd[1].initialized ? cmd[1].val.sticky : 0)) { 45 err->type = ERR_WIDGET_IN_CONTAINER; 46 err->arg = 0; 47 return 1; 48 } 49 return 0; 50 } 51 52 /* box <box id> remove <widget id> */ 53 static int 54 ltkd_box_cmd_remove( 55 ltk_window *window, 56 ltkd_widget *widget, 57 ltkd_cmd_token *tokens, 58 size_t num_tokens, 59 ltkd_error *err) { 60 (void)window; 61 ltk_box *box = LTK_CAST_BOX(widget->widget); 62 ltkd_cmdarg_parseinfo cmd[] = { 63 {.type = CMDARG_WIDGET, .widget_type = LTK_WIDGET_UNKNOWN, .optional = 0}, 64 }; 65 if (ltkd_parse_cmd(tokens, num_tokens, cmd, LENGTH(cmd), err)) 66 return 1; 67 if (ltk_box_remove(box, cmd[0].val.widget->widget)) { 68 err->type = ERR_WIDGET_NOT_IN_CONTAINER; 69 err->arg = 0; 70 return 1; 71 } 72 return 0; 73 } 74 75 /* box <box id> create <orientation> */ 76 static int 77 ltkd_box_cmd_create( 78 ltk_window *window, 79 ltkd_widget *widget_unneeded, 80 ltkd_cmd_token *tokens, 81 size_t num_tokens, 82 ltkd_error *err) { 83 (void)widget_unneeded; 84 ltkd_cmdarg_parseinfo cmd[] = { 85 {.type = CMDARG_IGNORE, .optional = 0}, 86 {.type = CMDARG_STRING, .optional = 0}, 87 {.type = CMDARG_IGNORE, .optional = 0}, 88 {.type = CMDARG_ORIENTATION, .optional = 0}, 89 }; 90 if (ltkd_parse_cmd(tokens, num_tokens, cmd, LENGTH(cmd), err)) 91 return 1; 92 ltk_box *box = ltk_box_create(window, cmd[3].val.orient); 93 if (!ltkd_widget_create(LTK_CAST_WIDGET(box), cmd[1].val.str, NULL, 0, err)) { 94 ltk_widget_destroy(LTK_CAST_WIDGET(box), 1); 95 err->arg = 1; 96 return 1; 97 } 98 99 return 0; 100 } 101 102 static ltkd_cmd_info box_cmds[] = { 103 {"add", <kd_box_cmd_add, 0}, 104 {"create", <kd_box_cmd_create, 1}, 105 {"remove", <kd_box_cmd_remove, 0}, 106 }; 107 108 GEN_CMD_HELPERS(ltkd_box_cmd, LTK_WIDGET_BOX, box_cmds)