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_swap(UiList *list, int i1, int i2) { 129 CxList *cxlist = list->data; 130 cxListSwap(cxlist, i1, i2); 131 } 132 133 void ui_srclist_clear(UiList *list) { 134 CxList *cxlist = list->data; 135 cxListClear(cxlist); 136 } 137 138 int ui_srclist_size(UiList *list) { 139 return ui_list_count(list); 140 } 141 142 /* 143 * numerates all sublists and sets the sublist index as userdata 144 */ 145 void ui_srclist_generate_sublist_num_data(UiList *list) { 146 CxList *cxlist = list->data; 147 CxIterator i = cxListIterator(cxlist); 148 cx_foreach(UiSubList *, sublist, i) { 149 sublist->userdata = (void*)i.index; 150 } 151 } 152 153 154 /* ---------------------------- UiSubListEventData ---------------------------- */ 155 156 UiList* ui_sublist_event_get_list(UiSubListEventData *event) { 157 return event->list; 158 } 159 160 int ui_sublist_event_get_sublist_index(UiSubListEventData *event) { 161 return event->sublist_index; 162 } 163 164 int ui_sublist_event_get_row_index(UiSubListEventData *event) { 165 return event->row_index; 166 } 167 168 void* ui_sublist_event_get_row_data(UiSubListEventData *event) { 169 return event->row_data; 170 } 171 172 void* ui_sublist_event_get_sublist_userdata(UiSubListEventData *event) { 173 return event->sublist_userdata; 174 } 175 176 void* ui_sublist_event_get_event_data(UiSubListEventData *event) { 177 return event->event_data; 178 } 179 180 181 /* ---------------------------- UiEvent ---------------------------- */ 182 183 UiObject* ui_event_get_obj(UiEvent *event) { 184 return event->obj; 185 } 186 187 void* ui_event_get_document(UiEvent *event) { 188 return event->document; 189 } 190 191 void* ui_event_get_windowdata(UiEvent *event) { 192 return event->window; 193 } 194 195 void* ui_event_get_eventdata(UiEvent *event) { 196 return event->eventdata; 197 } 198 199 int ui_event_get_eventdatatype(UiEvent *event) { 200 return event->eventdatatype; 201 } 202 203 int ui_event_get_int(UiEvent *event) { 204 return event->intval; 205 } 206 207 int ui_event_get_set(UiEvent *event) { 208 return event->set; 209 } 210 211 212 /* ------------------------- SubListItem (public) ------------------------- */ 213 214 void ui_sublist_item_set_icon(UiSubListItem *item, const char *icon) { 215 item->icon = icon ? strdup(icon) : NULL; 216 } 217 218 void ui_sublist_item_set_label(UiSubListItem *item, const char *label) { 219 item->label = label ? strdup(label) : NULL; 220 } 221 222 void ui_sublist_item_set_button_icon(UiSubListItem *item, const char *button_icon) { 223 item->button_icon = button_icon ? strdup(button_icon) : NULL; 224 } 225 226 void ui_sublist_item_set_button_label(UiSubListItem *item, const char *button_label) { 227 item->button_label = button_label ? strdup(button_label) : NULL; 228 } 229 230 void ui_sublist_item_set_button_menu(UiSubListItem *item, UiMenuBuilder *menu) { 231 item->button_menu = menu; 232 } 233 234 void ui_sublist_item_set_badge(UiSubListItem *item, const char *badge) { 235 item->badge = badge ? strdup(badge) : NULL; 236 } 237 238 void ui_sublist_item_set_eventdata(UiSubListItem *item, void *eventdata) { 239 item->eventdata = NULL; 240 } 241 242 /* ---------------------------- UiListSelection ---------------------------- */ 243 244 UiListSelection* ui_list_get_selection_allocated(UiList *list) { 245 UiListSelection *sel = malloc(sizeof(UiListSelection)); 246 *sel = ui_list_get_selection(list); 247 return sel; 248 249 } 250 251 int ui_list_selection_get_count(UiListSelection *sel) { 252 return sel->count; 253 } 254 255 int* ui_list_selection_get_rows(UiListSelection *sel) { 256 return sel->rows; 257 } 258 259 UIEXPORT void ui_list_set_selected_indices(UiList *list, int *indices, int num) { 260 UiListSelection sel; 261 sel.rows = indices; 262 sel.count = num; 263 if(list->setselection) { 264 list->setselection(list, sel); 265 } 266 } 267 268 void ui_list_selection_free(UiListSelection *sel) { 269 ui_listselection_free(*sel); 270 free(sel); 271 } 272 273 /* ---------------------------- UiFileList ---------------------------- */ 274 275 int ui_filelist_count(UiFileList *flist) { 276 return flist->nfiles; 277 } 278 279 char* ui_filelist_get(UiFileList *flist, int index) { 280 if(index >= 0 && index < flist->nfiles) { 281 return flist->files[index]; 282 } 283 return NULL; 284 } 285 286 /* ---------------------------- UiTextStyle ---------------------------- */ 287 288 void ui_textstyle_set_bold(UiTextStyle *style, UiBool set) { 289 if(set) { 290 style->text_style |= UI_TEXT_STYLE_BOLD; 291 } else { 292 style->text_style &= ~UI_TEXT_STYLE_BOLD; 293 } 294 } 295 296 void ui_textstyle_set_underline(UiTextStyle *style, UiBool set) { 297 if(set) { 298 style->text_style |= UI_TEXT_STYLE_UNDERLINE; 299 } else { 300 style->text_style &= ~UI_TEXT_STYLE_UNDERLINE; 301 } 302 } 303 304 void ui_textstyle_set_italic(UiTextStyle *style, UiBool set) { 305 if(set) { 306 style->text_style |= UI_TEXT_STYLE_ITALIC; 307 } else { 308 style->text_style &= ~UI_TEXT_STYLE_ITALIC; 309 } 310 } 311 312 void ui_textstyle_set_color(UiTextStyle *style, int r, int g, int b) { 313 style->fg_set = TRUE; 314 style->fg.red = r; 315 style->fg.green = g; 316 style->fg.blue = b; 317 } 318 319 void ui_textstyle_enable_color(UiTextStyle *style, UiBool enable) { 320 style->fg_set = enable; 321 } 322 323 324 /* ---------------------------- UiCellValue ---------------------------- */ 325 326 UiBool ui_cell_value_is_string(UiCellValue *value) { 327 return value->type == UI_STRING_EDITABLE; 328 } 329 330 UiBool ui_cell_value_is_int(UiCellValue *value) { 331 return FALSE; // TODO 332 } 333 334 const char* ui_cell_value_get_string(UiCellValue *value) { 335 return value->string; 336 } 337 338 int64_t ui_cell_value_get_int(UiCellValue *value) { 339 return value->i; 340 } 341