UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2025 Olaf Wintermann. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "wrapper.h" 30 #include "types.h" 31 #include <cx/list.h> 32 #include <string.h> 33 34 /* ---------------------------- UiObject ---------------------------- */ 35 36 UiContext* ui_object_get_context(UiObject *obj) { 37 return obj->ctx; 38 } 39 40 void* ui_object_get_windowdata(UiObject *obj) { 41 return obj->window; 42 } 43 44 void ui_object_set_windowdata(UiObject *obj, void *windowdata) { 45 obj->window = windowdata; 46 } 47 48 49 /* ---------------------------- UiList ---------------------------- */ 50 51 void* ui_list_get_data(UiList *list) { 52 return list->data; 53 } 54 55 void ui_list_set_data(UiList *list, void *data) { 56 list->data = data; 57 } 58 59 void* ui_list_get_iter(UiList *list) { 60 return list->iter; 61 } 62 63 void ui_list_set_iter(UiList *list, void *iter) { 64 list->iter = iter; 65 } 66 67 68 /* ------------------------------ UiSublist ------------------------------ */ 69 70 UiSubList* ui_sublist_new(void) { 71 UiSubList *sublist = malloc(sizeof(UiSubList)); 72 memset(sublist, 0, sizeof(UiSubList)); 73 return sublist; 74 } 75 76 void ui_sublist_set_value(UiSubList *sublist, UiList *value) { 77 sublist->value = value; 78 } 79 80 void ui_sublist_set_varname(UiSubList *sublist, const char *varname) { 81 free((void*)sublist->varname); 82 sublist->varname = strdup(varname); 83 } 84 85 void ui_sublist_set_header(UiSubList *sublist, const char *header) { 86 free((void*)sublist->header); 87 sublist->header = strdup(header); 88 } 89 90 void ui_sublist_set_separator(UiSubList *sublist, UiBool separator) { 91 sublist->separator = separator; 92 } 93 94 void ui_sublist_set_userdata(UiSubList *sublist, void *userdata) { 95 sublist->userdata = userdata; 96 } 97 98 void ui_sublist_free(UiSubList *sublist) { 99 free((void*)sublist->varname); 100 free((void*)sublist->header); 101 free(sublist); 102 } 103 104 105 /* -------------------- Source list (UiList<UiSublist>) -------------------- */ 106 107 UiList* ui_srclist_new(UiContext *ctx, const char *name) { 108 UiList *list = ui_list_new2(ctx, name, uic_ucx_list_init, NULL); 109 CxList *cxlist = list->data; 110 cxlist->collection.simple_destructor = (cx_destructor_func)ui_sublist_free; 111 return list; 112 } 113 114 void ui_srclist_add(UiList *list, UiSubList *item) { 115 ui_list_append(list, item); 116 } 117 118 void ui_srclist_insert(UiList *list, int index, UiSubList *item) { 119 CxList *cxlist = list->data; 120 cxListInsert(cxlist, index, item); 121 } 122 123 void ui_srclist_remove(UiList *list, int index) { 124 CxList *cxlist = list->data; 125 cxListRemove(cxlist, index); 126 } 127 128 void ui_srclist_clear(UiList *list) { 129 CxList *cxlist = list->data; 130 cxListClear(cxlist); 131 } 132 133 int ui_srclist_size(UiList *list) { 134 return ui_list_count(list); 135 } 136 137 138 /* ---------------------------- UiSubListEventData ---------------------------- */ 139 140 UiList* ui_sublist_event_get_list(UiSubListEventData *event) { 141 return event->list; 142 } 143 144 int ui_sublist_event_get_sublist_index(UiSubListEventData *event) { 145 return event->sublist_index; 146 } 147 148 int ui_sublist_event_get_row_index(UiSubListEventData *event) { 149 return event->row_index; 150 } 151 152 void* ui_sublist_event_get_row_data(UiSubListEventData *event) { 153 return event->row_data; 154 } 155 156 void* ui_sublist_event_get_sublist_userdata(UiSubListEventData *event) { 157 return event->sublist_userdata; 158 } 159 160 void* ui_sublist_event_get_event_data(UiSubListEventData *event) { 161 return event->event_data; 162 } 163 164 165 /* ---------------------------- UiEvent ---------------------------- */ 166 167 UiObject* ui_event_get_obj(UiEvent *event) { 168 return event->obj; 169 } 170 171 void* ui_event_get_document(UiEvent *event) { 172 return event->document; 173 } 174 175 void* ui_event_get_windowdata(UiEvent *event) { 176 return event->window; 177 } 178 179 void* ui_event_get_eventdata(UiEvent *event) { 180 return event->eventdata; 181 } 182 183 int ui_event_get_eventdatatype(UiEvent *event) { 184 return event->eventdatatype; 185 } 186 187 int ui_event_get_int(UiEvent *event) { 188 return event->intval; 189 } 190 191 int ui_event_get_set(UiEvent *event) { 192 return event->set; 193 } 194 195 196 /* ------------------------- SubListItem (public) ------------------------- */ 197 198 void ui_sublist_item_set_icon(UiSubListItem *item, const char *icon) { 199 item->icon = icon ? strdup(icon) : NULL; 200 } 201 202 void ui_sublist_item_set_label(UiSubListItem *item, const char *label) { 203 item->label = label ? strdup(label) : NULL; 204 } 205 206 void ui_sublist_item_set_button_icon(UiSubListItem *item, const char *button_icon) { 207 item->button_icon = button_icon ? strdup(button_icon) : NULL; 208 } 209 210 void ui_sublist_item_set_button_label(UiSubListItem *item, const char *button_label) { 211 item->button_label = button_label ? strdup(button_label) : NULL; 212 } 213 214 void ui_sublist_item_set_badge(UiSubListItem *item, const char *badge) { 215 item->badge = badge ? strdup(badge) : NULL; 216 } 217 218 void ui_sublist_item_set_eventdata(UiSubListItem *item, void *eventdata) { 219 item->eventdata = NULL; 220 } 221 222 /* ---------------------------- UiListSelection ---------------------------- */ 223 224 UiListSelection* ui_list_get_selection_allocated(UiList *list) { 225 UiListSelection *sel = malloc(sizeof(UiListSelection)); 226 *sel = ui_list_get_selection(list); 227 return sel; 228 229 } 230 231 int ui_list_selection_get_count(UiListSelection *sel) { 232 return sel->count; 233 } 234 235 int* ui_list_selection_get_rows(UiListSelection *sel) { 236 return sel->rows; 237 } 238 239 UIEXPORT void ui_list_set_selected_indices(UiList *list, int *indices, int num) { 240 UiListSelection sel; 241 sel.rows = indices; 242 sel.count = num; 243 if(list->setselection) { 244 list->setselection(list, sel); 245 } 246 } 247 248 void ui_list_selection_free(UiListSelection *sel) { 249 ui_listselection_free(*sel); 250 free(sel); 251 } 252 253 /* ---------------------------- UiFileList ---------------------------- */ 254 255 int ui_filelist_count(UiFileList *flist) { 256 return flist->nfiles; 257 } 258 259 char* ui_filelist_get(UiFileList *flist, int index) { 260 if(index >= 0 && index < flist->nfiles) { 261 return flist->files[index]; 262 } 263 return NULL; 264 } 265 266 /* ---------------------------- UiTextStyle ---------------------------- */ 267 268 void ui_textstyle_set_bold(UiTextStyle *style, UiBool set) { 269 if(set) { 270 style->text_style |= UI_TEXT_STYLE_BOLD; 271 } else { 272 style->text_style &= ~UI_TEXT_STYLE_BOLD; 273 } 274 } 275 276 void ui_textstyle_set_underline(UiTextStyle *style, UiBool set) { 277 if(set) { 278 style->text_style |= UI_TEXT_STYLE_UNDERLINE; 279 } else { 280 style->text_style &= ~UI_TEXT_STYLE_UNDERLINE; 281 } 282 } 283 284 void ui_textstyle_set_italic(UiTextStyle *style, UiBool set) { 285 if(set) { 286 style->text_style |= UI_TEXT_STYLE_ITALIC; 287 } else { 288 style->text_style &= ~UI_TEXT_STYLE_ITALIC; 289 } 290 } 291 292 void ui_textstyle_set_color(UiTextStyle *style, int r, int g, int b) { 293 style->fg_set = TRUE; 294 style->fg.red = r; 295 style->fg.green = g; 296 style->fg.blue = b; 297 } 298 299 void ui_textstyle_enable_color(UiTextStyle *style, UiBool enable) { 300 style->fg_set = enable; 301 } 302 303 304 /* ---------------------------- UiCellValue ---------------------------- */ 305 306 UiBool ui_cell_value_is_string(UiCellValue *value) { 307 return value->type == UI_STRING_EDITABLE; 308 } 309 310 UiBool ui_cell_value_is_int(UiCellValue *value) { 311 return FALSE; // TODO 312 } 313 314 const char* ui_cell_value_get_string(UiCellValue *value) { 315 return value->string; 316 } 317 318 int64_t ui_cell_value_get_int(UiCellValue *value) { 319 return value->i; 320 } 321