--- a/ui/gtk/list.c Sun Aug 24 15:24:16 2025 +0200 +++ b/ui/gtk/list.c Sat Oct 04 14:52:59 2025 +0200 @@ -92,6 +92,11 @@ tableview->ondropdata = args->ondropdata; tableview->selection.count = 0; tableview->selection.rows = NULL; + tableview->current_row = -1; + tableview->getstyle = args->getstyle; + tableview->getstyledata = args->getstyledata; + tableview->onsave = args->onsave; + tableview->onsavedata = args->onsavedata; if(args->getvalue2) { tableview->getvalue = args->getvalue2; @@ -102,7 +107,7 @@ } else { tableview->getvalue = null_getvalue; } - + return tableview; } @@ -140,6 +145,58 @@ /* END GObject wrapper for generic pointers */ +typedef struct UiCellEntry { + GtkEntry *entry; + UiListView *listview; + char *previous_value; + int row; + int col; +} UiCellEntry; + +static void cell_save_value(UiCellEntry *data, int restore) { + if(data->listview && data->listview->onsave) { + UiVar *var = data->listview->var; + UiList *list = var ? var->value : NULL; + const char *str = ENTRY_GET_TEXT(data->entry); + UiCellValue value; + value.string = str; + value.type = UI_STRING_EDITABLE; + if(data->listview->onsave(list, data->row, data->col, &value, data->listview->onsavedata)) { + free(data->previous_value); + data->previous_value = strdup(str); + } else if(restore) { + ENTRY_SET_TEXT(data->entry, data->previous_value); + } + } +} + +static void cell_entry_leave_focus( + GtkEventControllerFocus *self, + UiCellEntry *data) +{ + // TODO: use a different singal to track focus + // we only want to call cell_save_value, when another entry is selected, + // not when the window loses focus or something like that + cell_save_value(data, TRUE); +} + +static void cell_entry_destroy(GtkWidget *object, UiCellEntry *data) { + free(data->previous_value); + free(data); +} + +static void cell_entry_unmap(GtkWidget *w, UiCellEntry *data) { + const char *text = ENTRY_GET_TEXT(w); + cell_save_value(data, FALSE); +} + +static void cell_entry_activate( + GtkEntry *self, + UiCellEntry *data) +{ + cell_save_value(data, TRUE); +} + static void column_factory_setup(GtkListItemFactory *factory, GtkListItem *item, gpointer userdata) { UiColData *col = userdata; UiModel *model = col->listview->model; @@ -156,6 +213,38 @@ } else if(type == UI_ICON) { GtkWidget *image = gtk_image_new(); gtk_list_item_set_child(item, image); + } else if(type == UI_STRING_EDITABLE) { + GtkWidget *textfield = gtk_entry_new(); + gtk_widget_add_css_class(textfield, "ui-table-entry"); + gtk_list_item_set_child(item, textfield); + + UiCellEntry *entry_data = malloc(sizeof(UiCellEntry)); + entry_data->entry = GTK_ENTRY(textfield); + entry_data->listview = NULL; + entry_data->previous_value = NULL; + entry_data->col = 0; + entry_data->row = 0; + g_object_set_data(G_OBJECT(textfield), "ui_entry_data", entry_data); + + g_signal_connect( + textfield, + "destroy", + G_CALLBACK(cell_entry_destroy), + entry_data); + g_signal_connect( + textfield, + "activate", + G_CALLBACK(cell_entry_activate), + entry_data); + g_signal_connect( + textfield, + "unmap", + G_CALLBACK(cell_entry_unmap), + entry_data); + + GtkEventController *focus_controller = gtk_event_controller_focus_new(); + g_signal_connect(focus_controller, "leave", G_CALLBACK(cell_entry_leave_focus), entry_data); + gtk_widget_add_controller(textfield, focus_controller); } else { GtkWidget *label = gtk_label_new(NULL); gtk_label_set_xalign(GTK_LABEL(label), 0); @@ -163,19 +252,83 @@ } } -static void column_factory_bind(GtkListItemFactory *factory, GtkListItem *item, gpointer userdata) { +PangoAttrList* textstyle2pangoattributes(UiTextStyle style) { + PangoAttrList *attr = pango_attr_list_new(); + + if(style.text_style & UI_TEXT_STYLE_BOLD) { + pango_attr_list_insert(attr, pango_attr_weight_new(PANGO_WEIGHT_BOLD)); + } + if(style.text_style & UI_TEXT_STYLE_ITALIC) { + pango_attr_list_insert(attr, pango_attr_style_new(PANGO_STYLE_ITALIC)); + } + if(style.text_style & UI_TEXT_STYLE_UNDERLINE) { + pango_attr_list_insert(attr, pango_attr_underline_new(PANGO_UNDERLINE_SINGLE)); + } + + // foreground color, convert from 8bit to 16bit + guint16 r = (guint16)style.fg.red * 257; + guint16 g = (guint16)style.fg.green * 257; + guint16 b = (guint16)style.fg.blue * 257; + pango_attr_list_insert(attr, pango_attr_foreground_new(r, g, b)); + + return attr; +} + +static void column_factory_bind(GtkListItemFactory *unused, GtkListItem *item, gpointer userdata) { UiColData *col = userdata; UiList *list = col->listview->var ? col->listview->var->value : NULL; UiListView *listview = col->listview; - + ObjWrapper *obj = gtk_list_item_get_item(item); UiModel *model = col->listview->model; UiModelType type = model->types[col->model_column]; + // cache the GtkListItem + CxHashKey row_key = cx_hash_key(&obj->i, sizeof(int)); + UiRowItems *row = cxMapGet(listview->bound_rows, row_key); + if(row) { + if(row->items[col->model_column] == NULL) { + row->bound++; + } + } else { + row = calloc(1, sizeof(UiRowItems) + listview->numcolumns * sizeof(GtkListItem*)); + cxMapPut(listview->bound_rows, row_key, row); + row->bound = 1; + } + row->items[col->model_column] = item; + UiBool freevalue = FALSE; void *data = listview->getvalue(list, obj->data, obj->i, col->data_column, listview->getvaluedata, &freevalue); GtkWidget *child = gtk_list_item_get_child(item); + PangoAttrList *attributes = NULL; + UiTextStyle style = { 0, 0 }; + if(listview->getstyle) { + // query current row style, if it wasn't already queried + if(obj->i != listview->current_row) { + listview->current_row = obj->i; + listview->row_style = (UiTextStyle){ 0, 0 }; + listview->apply_row_style = listview->getstyle(list, obj->data, obj->i, -1, listview->getstyledata, &listview->row_style); + style = listview->row_style; + if(listview->apply_row_style) { + pango_attr_list_unref(listview->current_row_attributes); + listview->current_row_attributes = textstyle2pangoattributes(style); + } + } + + int style_col = col->data_column; + if(type == UI_ICON_TEXT || type == UI_ICON_TEXT_FREE) { + style_col++; // col->data_column is the icon, we need the next col for the label + } + + // get the column style + if(listview->getstyle(list, obj->data, obj->i, style_col, listview->getstyledata, &style)) { + attributes = textstyle2pangoattributes(style); + } else if(listview->apply_row_style) { + attributes = listview->current_row_attributes; + } + } + switch(type) { case UI_STRING_FREE: { freevalue = TRUE; @@ -185,6 +338,7 @@ if(freevalue) { free(data); } + gtk_label_set_attributes(GTK_LABEL(child), attributes); break; } case UI_INTEGER: { @@ -192,6 +346,7 @@ char buf[32]; snprintf(buf, 32, "%d", (int)intvalue); gtk_label_set_label(GTK_LABEL(child), buf); + gtk_label_set_attributes(GTK_LABEL(child), attributes); break; } case UI_ICON: { @@ -217,15 +372,55 @@ } if(data2 && label) { gtk_label_set_label(GTK_LABEL(label), data2); + gtk_label_set_attributes(GTK_LABEL(label), attributes); } if(freevalue) { free(data2); } break; } + case UI_STRING_EDITABLE: { + UiCellEntry *entry = g_object_get_data(G_OBJECT(child), "ui_entry_data"); + if(entry) { + entry->listview = col->listview; + entry->row = obj->i; + entry->col = col->data_column; + entry->previous_value = strdup(data); + } + ENTRY_SET_TEXT(child, data); + break; + } + } + + if(attributes != listview->current_row_attributes) { + pango_attr_list_unref(attributes); } } +static void column_factory_unbind(GtkSignalListItemFactory *self, GtkListItem *item, UiColData *col) { + ObjWrapper *obj = gtk_list_item_get_item(item); + UiListView *listview = col->listview; + CxHashKey row_key = cx_hash_key(&obj->i, sizeof(int)); + UiRowItems *row = cxMapGet(listview->bound_rows, row_key); + if(row) { + row->items[col->model_column] = NULL; + row->bound--; + if(row->bound == 0) { + cxMapRemove(listview->bound_rows, row_key); + } + } // else: should not happen + + GtkWidget *child = gtk_list_item_get_child(item); + UiCellEntry *entry = g_object_get_data(G_OBJECT(child), "ui_entry_data"); + if(entry) { + cell_save_value(entry, FALSE); + entry->listview = NULL; + free(entry->previous_value); + entry->previous_value = NULL; + } +} + + static GtkSelectionModel* create_selection_model(UiListView *listview, GListStore *liststore, bool multiselection) { GtkSelectionModel *selection_model; if(multiselection) { @@ -253,10 +448,14 @@ listview->getvalue = str_getvalue; } + listview->numcolumns = 1; listview->columns = malloc(sizeof(UiColData)); listview->columns->listview = listview; listview->columns->data_column = 0; listview->columns->model_column = 0; + + listview->bound_rows = cxHashMapCreate(NULL, CX_STORE_POINTERS, 128); + listview->bound_rows->collection.simple_destructor = (cx_destructor_func)free; GtkListItemFactory *factory = gtk_signal_list_item_factory_new(); g_signal_connect(factory, "setup", G_CALLBACK(column_factory_setup), listview->columns); @@ -339,11 +538,15 @@ listview->getvalue = str_getvalue; } + listview->numcolumns = 1; listview->columns = malloc(sizeof(UiColData)); listview->columns->listview = listview; listview->columns->data_column = 0; listview->columns->model_column = 0; + listview->bound_rows = cxHashMapCreate(NULL, CX_STORE_POINTERS, 128); + listview->bound_rows->collection.simple_destructor = (cx_destructor_func)free; + GtkListItemFactory *factory = gtk_signal_list_item_factory_new(); g_signal_connect(factory, "setup", G_CALLBACK(column_factory_setup), listview->columns); g_signal_connect(factory, "bind", G_CALLBACK(column_factory_bind), listview->columns); @@ -432,6 +635,10 @@ int columns = model ? model->columns : 0; tableview->columns = calloc(columns, sizeof(UiColData)); + tableview->numcolumns = columns; + + tableview->bound_rows = cxHashMapCreate(NULL, CX_STORE_POINTERS, 128); + tableview->bound_rows->collection.simple_destructor = (cx_destructor_func)free; int addi = 0; for(int i=0;i<columns;i++) { @@ -631,21 +838,20 @@ } else { void *value = list->get(list, i); if(value) { - ObjWrapper *obj = obj_wrapper_new(value, i); - UiListSelection sel = list->getselection(list); - // TODO: if index i is selected, the selection is lost - // is it possible to update the item without removing it? - // workaround: save selection and reapply it - int count = g_list_model_get_n_items(G_LIST_MODEL(view->liststore)); - if(count <= i) { - g_list_store_splice(view->liststore, i, 0, (void **)&obj, 1); - } else { - g_list_store_splice(view->liststore, i, 1, (void **)&obj, 1); + ObjWrapper *obj = g_list_model_get_item(G_LIST_MODEL(view->liststore), i); + if(obj) { + obj->data = value; } - if(sel.count > 0) { - list->setselection(list, sel); + + CxHashKey row_key = cx_hash_key(&i, sizeof(int)); + UiRowItems *row = cxMapGet(view->bound_rows, row_key); + if(row) { + for(int c=0;c<view->numcolumns;c++) { + if(row->items[c] != NULL) { + column_factory_bind(NULL, row->items[c], &view->columns[c]); + } + } } - ui_listselection_free(sel); } } } @@ -709,14 +915,40 @@ static void update_list_row(UiListView *listview, GtkListStore *store, GtkTreeIter *iter, UiList *list, void *elm, int row) { UiModel *model = listview->model; + ui_getstylefunc getstyle = listview->getstyle; + + // get the row style + UiBool style_set = FALSE; + UiTextStyle style = { 0, 0 }; + if(getstyle) { + style_set = getstyle(list, elm, row, -1, listview->getstyledata, &style); + } + // set column values int c = 0; for(int i=0;i<model->columns;i++,c++) { UiBool freevalue = FALSE; void *data = listview->getvalue(list, elm, row, c, listview->getvaluedata, &freevalue); + + UiModelType type = model->types[i]; + + if(getstyle) { + // in case the column is icon+text, only get a style for the text column + int style_col = c; + if(type == UI_ICON_TEXT || type == UI_ICON_TEXT_FREE) { + style_col++; + } + + // Get the individual column style + // The column style overrides the row style, however if no column style + // is provided, we stick with the row style + if(getstyle(list, elm, row, style_col, listview->getstyledata, &style)) { + style_set = TRUE; + } + } GValue value = G_VALUE_INIT; - switch(model->types[i]) { + switch(type) { case UI_STRING_FREE: { freevalue = TRUE; } @@ -789,13 +1021,64 @@ } gtk_list_store_set_value(store, iter, c, &value); + + if(style_set) { + int soff = listview->style_offset + i*6; + + GValue style_set_value = G_VALUE_INIT; + g_value_init(&style_set_value, G_TYPE_BOOLEAN); + g_value_set_boolean(&style_set_value, TRUE); + gtk_list_store_set_value(store, iter, soff, &style_set_value); + + GValue style_weight_value = G_VALUE_INIT; + g_value_init(&style_weight_value, G_TYPE_INT); + if(style.text_style & UI_TEXT_STYLE_BOLD) { + g_value_set_int(&style_weight_value, 600); + } else { + g_value_set_int(&style_weight_value, 400); + } + gtk_list_store_set_value(store, iter, soff + 1, &style_weight_value); + + GValue style_underline_value = G_VALUE_INIT; + g_value_init(&style_underline_value, G_TYPE_INT); + if(style.text_style & UI_TEXT_STYLE_UNDERLINE) { + g_value_set_int(&style_underline_value, PANGO_UNDERLINE_SINGLE); + } else { + g_value_set_int(&style_underline_value, PANGO_UNDERLINE_NONE); + } + gtk_list_store_set_value(store, iter, soff + 2, &style_underline_value); + + GValue style_italic_value = G_VALUE_INIT; + g_value_init(&style_italic_value, G_TYPE_INT); + if(style.text_style & UI_TEXT_STYLE_ITALIC) { + g_value_set_int(&style_italic_value, PANGO_STYLE_ITALIC); + } else { + g_value_set_int(&style_italic_value, PANGO_STYLE_NORMAL); + } + gtk_list_store_set_value(store, iter, soff + 3, &style_italic_value); + + GValue style_fgset_value = G_VALUE_INIT; + g_value_init(&style_fgset_value, G_TYPE_BOOLEAN); + g_value_set_boolean(&style_fgset_value, style.fg_set); + gtk_list_store_set_value(store, iter, soff + 4, &style_fgset_value); + + if(style.fg_set) { + char buf[8]; + snprintf(buf, 8, "#%02X%02X%02X", (int)style.fg.red, (int)style.fg.green, (int)style.fg.blue); + + GValue style_fg_value = G_VALUE_INIT; + g_value_init(&style_fg_value, G_TYPE_STRING); + g_value_set_string(&style_fg_value, buf); + gtk_list_store_set_value(store, iter, soff + 5, &style_fg_value); + } + } } } static GtkListStore* create_list_store(UiListView *listview, UiList *list) { UiModel *model = listview->model; int columns = model->columns; - GType types[2*columns]; + GType *types = calloc(columns*8, sizeof(GType)); int c = 0; for(int i=0;i<columns;i++,c++) { switch(model->types[i]) { @@ -810,8 +1093,18 @@ } } } + int s = 0; + for(int i=0;i<columns;i++) { + types[listview->style_offset+s] = G_TYPE_BOOLEAN; s++; // *-set + types[listview->style_offset+s] = G_TYPE_INT; s++; // weight + types[listview->style_offset+s] = G_TYPE_INT; s++; // underline + types[listview->style_offset+s] = G_TYPE_INT; s++; // style + types[listview->style_offset+s] = G_TYPE_BOOLEAN; s++; // foreground-set + types[listview->style_offset+s] = G_TYPE_STRING; s++; // foreground + } - GtkListStore *store = gtk_list_store_newv(c, types); + GtkListStore *store = gtk_list_store_newv(c+s, types); + free(types); if(list) { void *elm = list->first(list); @@ -857,6 +1150,7 @@ UiModel *model = ui_model(obj->ctx, UI_STRING, "", -1); UiListView *listview = create_listview(obj, args); + listview->style_offset = 1; if(!args->getvalue && !args->getvalue2) { listview->getvalue = str_getvalue; } @@ -957,10 +1251,22 @@ UiModel *model = args->model; int columns = model ? model->columns : 0; + // find the last data column index int addi = 0; - for(int i=0;i<columns;i++) { + int style_offset = 0; + int i = 0; + for(;i<columns;i++) { + if(model->types[i] == UI_ICON_TEXT || model->types[i] == UI_ICON_TEXT_FREE) { + addi++; + } + } + style_offset = i+addi; + + // create columns and init cell renderers + addi = 0; + for(i=0;i<columns;i++) { GtkTreeViewColumn *column = NULL; - if(model->types[i] == UI_ICON_TEXT) { + if(model->types[i] == UI_ICON_TEXT || model->types[i] == UI_ICON_TEXT_FREE) { column = gtk_tree_view_column_new(); gtk_tree_view_column_set_title(column, model->titles[i]); @@ -971,8 +1277,21 @@ gtk_tree_view_column_pack_start(column, iconrenderer, FALSE); - gtk_tree_view_column_add_attribute(column, iconrenderer, "pixbuf", i); - gtk_tree_view_column_add_attribute(column, textrenderer, "text", i+1); + gtk_tree_view_column_add_attribute(column, iconrenderer, "pixbuf", addi + i); + gtk_tree_view_column_add_attribute(column, textrenderer, "text", addi + i+1); + + if(args->getstyle) { + int soff = style_offset + i*6; + gtk_tree_view_column_add_attribute(column, textrenderer, "weight-set", soff); + gtk_tree_view_column_add_attribute(column, textrenderer, "underline-set", soff); + gtk_tree_view_column_add_attribute(column, textrenderer, "style-set", soff); + + gtk_tree_view_column_add_attribute(column, textrenderer, "weight", soff + 1); + gtk_tree_view_column_add_attribute(column, textrenderer, "underline", soff + 2); + gtk_tree_view_column_add_attribute(column, textrenderer, "style", soff + 3); + gtk_tree_view_column_add_attribute(column, textrenderer, "foreground-set", soff + 4); + gtk_tree_view_column_add_attribute(column, textrenderer, "foreground", soff + 5); + } addi++; } else if (model->types[i] == UI_ICON) { @@ -984,13 +1303,26 @@ i + addi, NULL); } else { - GtkCellRenderer *renderer = gtk_cell_renderer_text_new(); + GtkCellRenderer *textrenderer = gtk_cell_renderer_text_new(); column = gtk_tree_view_column_new_with_attributes( model->titles[i], - renderer, + textrenderer, "text", i + addi, NULL); + + if(args->getstyle) { + int soff = style_offset + i*6; + gtk_tree_view_column_add_attribute(column, textrenderer, "weight-set", soff); + gtk_tree_view_column_add_attribute(column, textrenderer, "underline-set", soff); + gtk_tree_view_column_add_attribute(column, textrenderer, "style-set", soff); + + gtk_tree_view_column_add_attribute(column, textrenderer, "weight", soff + 1); + gtk_tree_view_column_add_attribute(column, textrenderer, "underline", soff + 2); + gtk_tree_view_column_add_attribute(column, textrenderer, "style", soff + 3); + gtk_tree_view_column_add_attribute(column, textrenderer, "foreground-set", soff + 4); + gtk_tree_view_column_add_attribute(column, textrenderer, "foreground", soff + 5); + } } int colsz = model->columnsize[i]; @@ -1019,6 +1351,8 @@ // add TreeView as observer to the UiList to update the TreeView if the // data changes UiListView *tableview = create_listview(obj, args); + tableview->widget = view; + tableview->style_offset = style_offset; g_signal_connect( view, "destroy", @@ -1154,6 +1488,7 @@ UiListView *listview = create_listview(obj, args); listview->widget = combobox; + listview->style_offset = 1; listview->model = ui_model(obj->ctx, UI_STRING, "", -1); g_signal_connect( combobox, @@ -1676,6 +2011,8 @@ } #if GTK_CHECK_VERSION(4, 10, 0) free(v->columns); + pango_attr_list_unref(v->current_row_attributes); + cxMapFree(v->bound_rows); #endif free(v->selection.rows); free(v); @@ -1947,7 +2284,7 @@ } } -static GtkWidget* create_listbox_row(UiListBox *listbox, UiListBoxSubList *sublist, UiSubListItem *item, int index) { +static void listbox_fill_row(UiListBox *listbox, GtkWidget *row, UiListBoxSubList *sublist, UiSubListItem *item, int index) { GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10); if(item->icon) { GtkWidget *icon = ICON_IMAGE(item->icon); @@ -1959,7 +2296,6 @@ if(item->badge) { } - GtkWidget *row = gtk_list_box_row_new(); LISTBOX_ROW_SET_CHILD(row, hbox); // signals @@ -1987,7 +2323,7 @@ if(item->badge) { GtkWidget *badge = gtk_label_new(item->badge); WIDGET_ADD_CSS_CLASS(badge, "ui-badge"); -#if GTK_CHECK_VERSION(4, 0, 0) +#if GTK_CHECK_VERSION(3, 14, 0) gtk_widget_set_valign(badge, GTK_ALIGN_CENTER); BOX_ADD(hbox, badge); #else @@ -2010,8 +2346,36 @@ event ); } +} + +static void update_sublist_item(UiListBox *listbox, UiListBoxSubList *sublist, int index) { + GtkListBoxRow *row = gtk_list_box_get_row_at_index(listbox->listbox, sublist->startpos + index); + if(!row) { + return; + } + UiList *list = sublist->var->value; + if(!list) { + return; + } - return row; + void *elm = list->get(list, index); + UiSubListItem item = { NULL, NULL, NULL, NULL, NULL, NULL }; + if(listbox->getvalue) { + listbox->getvalue(list, sublist->userdata, elm, index, &item, listbox->getvaluedata); + } else { + item.label = strdup(elm); + } + + LISTBOX_ROW_REMOVE_CHILD(row); + + listbox_fill_row(listbox, GTK_WIDGET(row), sublist, &item, index); + + // cleanup + free(item.label); + free(item.icon); + free(item.button_label); + free(item.button_icon); + free(item.badge); } void ui_listbox_update_sublist(UiListBox *listbox, UiListBoxSubList *sublist, size_t listbox_insert_index) { @@ -2058,7 +2422,8 @@ } // create listbox item - GtkWidget *row = create_listbox_row(listbox, sublist, &item, (int)index); + GtkWidget *row = gtk_list_box_row_new(); + listbox_fill_row(listbox, row, sublist, &item, (int)index); if(index == 0) { // first row in the sublist, set ui_listbox data to the row // which is then used by the headerfunc @@ -2092,14 +2457,17 @@ void ui_listbox_list_update(UiList *list, int i) { UiListBoxSubList *sublist = list->obj; - ui_listbox_update_sublist(sublist->listbox, sublist, sublist->startpos); - size_t pos = 0; - CxIterator it = cxListIterator(sublist->listbox->sublists); - cx_foreach(UiListBoxSubList *, ls, it) { - ls->startpos = pos; - pos += sublist->numitems; + if(i < 0) { + ui_listbox_update_sublist(sublist->listbox, sublist, sublist->startpos); + size_t pos = 0; + CxIterator it = cxListIterator(sublist->listbox->sublists); + cx_foreach(UiListBoxSubList *, ls, it) { + ls->startpos = pos; + pos += ls->numitems; + } + } else { + update_sublist_item(sublist->listbox, sublist, i); } - } void ui_listbox_row_activate(GtkListBox *self, GtkListBoxRow *row, gpointer user_data) {