UNIXworkcode

1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 * 4 * Copyright 2017 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 #ifndef UI_TREE_H 30 #define UI_TREE_H 31 32 #include "toolkit.h" 33 34 #ifdef __cplusplus 35 extern "C" { 36 #endif 37 38 typedef struct UiModel UiModel; 39 typedef struct UiListCallbacks UiListCallbacks; 40 typedef struct UiListDnd UiListDnd; 41 42 typedef struct UiListArgs UiListArgs; 43 typedef struct UiSourceListArgs UiSourceListArgs; 44 45 typedef struct UiSubList UiSubList; 46 typedef struct UiSubListItem UiSubListItem; 47 48 typedef enum UiModelType { 49 UI_STRING = 0, 50 UI_STRING_FREE, 51 UI_INTEGER, 52 UI_ICON, 53 UI_ICON_TEXT, 54 UI_ICON_TEXT_FREE 55 } UiModelType; 56 57 struct UiModel { 58 /* 59 * number of columns 60 */ 61 int columns; 62 63 /* 64 * array of column types 65 * array length is the number of columns 66 */ 67 UiModelType *types; 68 69 /* 70 * array of column titles 71 * array length is the number of columns 72 */ 73 char **titles; 74 75 /* 76 * array of column size hints 77 */ 78 int *columnsize; 79 80 /* 81 * function for translating model data to view data 82 * first argument is the pointer returned by UiList->get or UiTree->get 83 * second argument is the column index 84 * TODO: return 85 */ 86 void*(*getvalue)(void*, int); 87 }; 88 89 struct UiListCallbacks { 90 /* 91 * selection callback 92 */ 93 ui_callback activate; 94 95 /* 96 * cursor callback 97 */ 98 ui_callback selection; 99 100 /* 101 * userdata for all callbacks 102 */ 103 void *userdata; 104 }; 105 106 struct UiListArgs { 107 UiTri fill; 108 UiBool hexpand; 109 UiBool vexpand; 110 UiBool hfill; 111 UiBool vfill; 112 int colspan; 113 int rowspan; 114 const char *name; 115 const char *style_class; 116 117 UiList* list; 118 const char* varname; 119 UiModel* model; 120 ui_getvaluefunc getvalue; 121 ui_callback onactivate; 122 void* onactivatedata; 123 ui_callback onselection; 124 void* onselectiondata; 125 ui_callback ondragstart; 126 void* ondragstartdata; 127 ui_callback ondragcomplete; 128 void* ondragcompletedata; 129 ui_callback ondrop; 130 void* ondropsdata; 131 UiBool multiselection; 132 UiMenuBuilder *contextmenu; 133 134 const int *groups; 135 }; 136 137 typedef void (*ui_sublist_getvalue_func)(void *sublist_userdata, void *rowdata, int index, UiSubListItem *item); 138 139 struct UiSubList { 140 UiList *value; 141 const char *varname; 142 const char *header; 143 UiBool separator; 144 void *userdata; 145 }; 146 147 /* 148 * list item members must be filled by the sublist getvalue func 149 * all members must be allocated (by malloc, strdup, ...) the pointer 150 * will be passed to free 151 */ 152 struct UiSubListItem { 153 char *icon; 154 char *label; 155 char *button_icon; 156 char *button_label; 157 char *badge; 158 void *eventdata; 159 }; 160 161 struct UiSourceListArgs { 162 UiTri fill; 163 UiBool hexpand; 164 UiBool vexpand; 165 UiBool hfill; 166 UiBool vfill; 167 int colspan; 168 int rowspan; 169 const char *name; 170 const char *style_class; 171 172 const int *groups; 173 174 /* 175 * list of sublists 176 * a sublist must have a varname or a value 177 * 178 * the last entry in the list must contain all NULL values or numsublists 179 * must contain the number of sublists 180 */ 181 UiSubList *sublists; 182 /* 183 * optional number of sublists 184 * if the value is 0, it is assumed, that sublists is null-terminated 185 * (last item contains only NULL values) 186 */ 187 size_t numsublists; 188 189 /* 190 * callback for each list item, that should fill all necessary 191 * UiSubListItem fields 192 */ 193 ui_sublist_getvalue_func getvalue; 194 195 /* 196 * activated when a list item is selected 197 */ 198 ui_callback onactivate; 199 void *onactivatedata; 200 201 /* 202 * activated, when the additional list item button is clicked 203 */ 204 ui_callback onbuttonclick; 205 void *onbuttonclickdata; 206 }; 207 208 #define UI_SUBLIST(...) (UiSubList){ __VA_ARGS__ } 209 #define UI_SUBLISTS(...) (UiSubList[]){ __VA_ARGS__, (UiSubList){NULL,NULL,NULL,0} } 210 211 212 UIEXPORT UiModel* ui_model(UiContext *ctx, ...); 213 UIEXPORT UiModel* ui_model_copy(UiContext *ctx, UiModel* model); 214 UIEXPORT void ui_model_free(UiContext *ctx, UiModel *mi); 215 216 #define ui_listview(obj, ...) ui_listview_create(obj, (UiListArgs) { __VA_ARGS__ } ) 217 #define ui_table(obj, ...) ui_table_create(obj, (UiListArgs) { __VA_ARGS__ } ) 218 #define ui_combobox(obj, ...) ui_combobox_create(obj, (UiListArgs) { __VA_ARGS__ } ) 219 #define ui_breadcrumbbar(obj, ...) ui_breadcrumbbar_create(obj, (UiListArgs) { __VA_ARGS__ } ) 220 #define ui_sourcelist(obj, ...) ui_sourcelist_create(obj, (UiSourceListArgs) { __VA_ARGS__ } ) 221 222 UIEXPORT UIWIDGET ui_listview_create(UiObject* obj, UiListArgs args); 223 UIEXPORT UIWIDGET ui_table_create(UiObject* obj, UiListArgs args); 224 UIEXPORT UIWIDGET ui_combobox_create(UiObject* obj, UiListArgs args); 225 UIEXPORT UIWIDGET ui_breadcrumbbar_create(UiObject* obj, UiListArgs args); 226 227 UIEXPORT UIWIDGET ui_sourcelist_create(UiObject *obj, UiSourceListArgs args); 228 229 230 #ifdef __cplusplus 231 } 232 #endif 233 234 #endif /* UI_TREE_H */ 235 236