ui/gtk/list.c

changeset 738
0dbf92544d4f
parent 723
e513086eadbb
child 741
b653d8296d12
equal deleted inserted replaced
737:ac7f0869e8ae 738:0dbf92544d4f
90 tableview->ondragcompletedata = args->ondragcompletedata; 90 tableview->ondragcompletedata = args->ondragcompletedata;
91 tableview->ondrop = args->ondrop; 91 tableview->ondrop = args->ondrop;
92 tableview->ondropdata = args->ondropdata; 92 tableview->ondropdata = args->ondropdata;
93 tableview->selection.count = 0; 93 tableview->selection.count = 0;
94 tableview->selection.rows = NULL; 94 tableview->selection.rows = NULL;
95 tableview->current_row = -1;
96 tableview->getstyle = args->getstyle;
97 tableview->getstyledata = args->getstyledata;
98 #if GTK_CHECK_VERSION(4, 10, 0)
99 tableview->default_attributes = pango_attr_list_new();
100 #endif
95 101
96 if(args->getvalue2) { 102 if(args->getvalue2) {
97 tableview->getvalue = args->getvalue2; 103 tableview->getvalue = args->getvalue2;
98 tableview->getvaluedata = args->getvalue2data; 104 tableview->getvaluedata = args->getvalue2data;
99 } else if(args->getvalue) { 105 } else if(args->getvalue) {
100 tableview->getvalue = getvalue_wrapper; 106 tableview->getvalue = getvalue_wrapper;
101 tableview->getvaluedata = (void*)args->getvalue; 107 tableview->getvaluedata = (void*)args->getvalue;
102 } else { 108 } else {
103 tableview->getvalue = null_getvalue; 109 tableview->getvalue = null_getvalue;
104 } 110 }
105 111
106 return tableview; 112 return tableview;
107 } 113 }
108 114
109 #if GTK_CHECK_VERSION(4, 10, 0) 115 #if GTK_CHECK_VERSION(4, 10, 0)
110 116
161 gtk_label_set_xalign(GTK_LABEL(label), 0); 167 gtk_label_set_xalign(GTK_LABEL(label), 0);
162 gtk_list_item_set_child(item, label); 168 gtk_list_item_set_child(item, label);
163 } 169 }
164 } 170 }
165 171
172 PangoAttrList* textstyle2pangoattributes(UiTextStyle style) {
173 PangoAttrList *attr = pango_attr_list_new();
174
175 if(style.text_style & UI_TEXT_STYLE_BOLD) {
176 pango_attr_list_insert(attr, pango_attr_weight_new(PANGO_WEIGHT_BOLD));
177 }
178 if(style.text_style & UI_TEXT_STYLE_ITALIC) {
179 pango_attr_list_insert(attr, pango_attr_style_new(PANGO_STYLE_ITALIC));
180 }
181 if(style.text_style & UI_TEXT_STYLE_UNDERLINE) {
182 pango_attr_list_insert(attr, pango_attr_underline_new(PANGO_UNDERLINE_SINGLE));
183 }
184
185 // foreground color, convert from 8bit to 16bit
186 guint16 r = (guint16)style.fg.red * 257;
187 guint16 g = (guint16)style.fg.green * 257;
188 guint16 b = (guint16)style.fg.blue * 257;
189 pango_attr_list_insert(attr, pango_attr_foreground_new(r, g, b));
190
191 return attr;
192 }
193
166 static void column_factory_bind(GtkListItemFactory *factory, GtkListItem *item, gpointer userdata) { 194 static void column_factory_bind(GtkListItemFactory *factory, GtkListItem *item, gpointer userdata) {
167 UiColData *col = userdata; 195 UiColData *col = userdata;
168 UiList *list = col->listview->var ? col->listview->var->value : NULL; 196 UiList *list = col->listview->var ? col->listview->var->value : NULL;
169 UiListView *listview = col->listview; 197 UiListView *listview = col->listview;
170 198
173 UiModelType type = model->types[col->model_column]; 201 UiModelType type = model->types[col->model_column];
174 202
175 UiBool freevalue = FALSE; 203 UiBool freevalue = FALSE;
176 void *data = listview->getvalue(list, obj->data, obj->i, col->data_column, listview->getvaluedata, &freevalue); 204 void *data = listview->getvalue(list, obj->data, obj->i, col->data_column, listview->getvaluedata, &freevalue);
177 GtkWidget *child = gtk_list_item_get_child(item); 205 GtkWidget *child = gtk_list_item_get_child(item);
206
207 PangoAttrList *attributes = NULL;
208 UiTextStyle style = { 0, 0 };
209 if(listview->getstyle) {
210 // query current row style, if it wasn't already queried
211 if(obj->i != listview->current_row) {
212 listview->current_row = obj->i;
213 listview->row_style = (UiTextStyle){ 0, 0 };
214 listview->apply_row_style = listview->getstyle(list, obj->data, obj->i, -1, listview->getstyledata, &listview->row_style);
215 style = listview->row_style;
216 if(listview->apply_row_style) {
217 pango_attr_list_unref(listview->current_row_attributes);
218 listview->current_row_attributes = textstyle2pangoattributes(style);
219 }
220 }
221
222 int style_col = col->data_column;
223 if(type == UI_ICON_TEXT || type == UI_ICON_TEXT_FREE) {
224 style_col++; // col->data_column is the icon, we need the next col for the label
225 }
226
227 // get the column style
228 if(listview->getstyle(list, obj->data, obj->i, style_col, listview->getstyledata, &style)) {
229 attributes = textstyle2pangoattributes(style);
230 } else if(listview->apply_row_style) {
231 attributes = listview->current_row_attributes;
232 }
233 }
178 234
179 switch(type) { 235 switch(type) {
180 case UI_STRING_FREE: { 236 case UI_STRING_FREE: {
181 freevalue = TRUE; 237 freevalue = TRUE;
182 } 238 }
183 case UI_STRING: { 239 case UI_STRING: {
184 gtk_label_set_label(GTK_LABEL(child), data); 240 gtk_label_set_label(GTK_LABEL(child), data);
185 if(freevalue) { 241 if(freevalue) {
186 free(data); 242 free(data);
187 } 243 }
244 gtk_label_set_attributes(GTK_LABEL(child), attributes);
188 break; 245 break;
189 } 246 }
190 case UI_INTEGER: { 247 case UI_INTEGER: {
191 intptr_t intvalue = (intptr_t)data; 248 intptr_t intvalue = (intptr_t)data;
192 char buf[32]; 249 char buf[32];
193 snprintf(buf, 32, "%d", (int)intvalue); 250 snprintf(buf, 32, "%d", (int)intvalue);
194 gtk_label_set_label(GTK_LABEL(child), buf); 251 gtk_label_set_label(GTK_LABEL(child), buf);
252 gtk_label_set_attributes(GTK_LABEL(child), attributes);
195 break; 253 break;
196 } 254 }
197 case UI_ICON: { 255 case UI_ICON: {
198 UiIcon *icon = data; 256 UiIcon *icon = data;
199 if(icon) { 257 if(icon) {
215 UiIcon *icon = data; 273 UiIcon *icon = data;
216 gtk_image_set_from_paintable(GTK_IMAGE(image), GDK_PAINTABLE(icon->info)); 274 gtk_image_set_from_paintable(GTK_IMAGE(image), GDK_PAINTABLE(icon->info));
217 } 275 }
218 if(data2 && label) { 276 if(data2 && label) {
219 gtk_label_set_label(GTK_LABEL(label), data2); 277 gtk_label_set_label(GTK_LABEL(label), data2);
278 gtk_label_set_attributes(GTK_LABEL(label), attributes);
220 } 279 }
221 if(freevalue) { 280 if(freevalue) {
222 free(data2); 281 free(data2);
223 } 282 }
224 break; 283 break;
225 } 284 }
285 }
286
287 if(attributes != listview->current_row_attributes) {
288 pango_attr_list_unref(attributes);
226 } 289 }
227 } 290 }
228 291
229 static GtkSelectionModel* create_selection_model(UiListView *listview, GListStore *liststore, bool multiselection) { 292 static GtkSelectionModel* create_selection_model(UiListView *listview, GListStore *liststore, bool multiselection) {
230 GtkSelectionModel *selection_model; 293 GtkSelectionModel *selection_model;
1674 } 1737 }
1675 free(v->elements); 1738 free(v->elements);
1676 } 1739 }
1677 #if GTK_CHECK_VERSION(4, 10, 0) 1740 #if GTK_CHECK_VERSION(4, 10, 0)
1678 free(v->columns); 1741 free(v->columns);
1742 pango_attr_list_unref(v->default_attributes);
1743 pango_attr_list_unref(v->current_row_attributes);
1679 #endif 1744 #endif
1680 free(v->selection.rows); 1745 free(v->selection.rows);
1681 free(v); 1746 free(v);
1682 } 1747 }
1683 1748

mercurial