ui/gtk/model.c

changeset 140
c03c338a7dcf
parent 129
5babf09f5f19
child 142
46448d38885c
equal deleted inserted replaced
139:dbde25a5bc53 140:c03c338a7dcf
1 /* 1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * 3 *
4 * Copyright 2014 Olaf Wintermann. All rights reserved. 4 * Copyright 2017 Olaf Wintermann. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met: 7 * modification, are permitted provided that the following conditions are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
90 i->iter_parent = ui_list_model_iter_parent; 90 i->iter_parent = ui_list_model_iter_parent;
91 } 91 }
92 92
93 static void list_model_init(UiListModel *instance, GObjectClass *cl) { 93 static void list_model_init(UiListModel *instance, GObjectClass *cl) {
94 instance->columntypes = NULL; 94 instance->columntypes = NULL;
95 instance->list = NULL; 95 instance->var = NULL;
96 instance->numcolumns = 0; 96 instance->numcolumns = 0;
97 instance->stamp = g_random_int(); 97 instance->stamp = g_random_int();
98 } 98 }
99 99
100 static GType ui_gtk_type(UiModelType type) { 100 static GType ui_gtk_type(UiModelType type) {
120 } 120 }
121 } 121 }
122 value->g_type = G_TYPE_INVALID; 122 value->g_type = G_TYPE_INVALID;
123 } 123 }
124 124
125 UiListModel* ui_list_model_new(UiListPtr *list, UiModelInfo *info) { 125 UiListModel* ui_list_model_new(UiVar *var, UiModelInfo *info) {
126 UiListModel *model = g_object_new(list_model_type, NULL); 126 UiListModel *model = g_object_new(list_model_type, NULL);
127 model->info = info; 127 model->info = info;
128 model->list = list; 128 model->var = var;
129 model->columntypes = calloc(sizeof(GType), 2 * info->columns); 129 model->columntypes = calloc(sizeof(GType), 2 * info->columns);
130 int ncol = 0; 130 int ncol = 0;
131 for(int i=0;i<info->columns;i++) { 131 for(int i=0;i<info->columns;i++) {
132 UiModelType type = info->types[i]; 132 UiModelType type = info->types[i];
133 if(type == UI_ICON_TEXT) { 133 if(type == UI_ICON_TEXT) {
166 GtkTreeIter *iter, 166 GtkTreeIter *iter,
167 GtkTreePath *path) 167 GtkTreePath *path)
168 { 168 {
169 g_assert(IS_UI_LIST_MODEL(tree_model)); 169 g_assert(IS_UI_LIST_MODEL(tree_model));
170 UiListModel *model = UI_LIST_MODEL(tree_model); 170 UiListModel *model = UI_LIST_MODEL(tree_model);
171 UiList *list = model->list->list; 171 UiList *list = model->var->value;
172 172
173 // check the depth of the path 173 // check the depth of the path
174 // a list must have a depth of 1 174 // a list must have a depth of 1
175 gint depth = gtk_tree_path_get_depth(path); 175 gint depth = gtk_tree_path_get_depth(path);
176 g_assert(depth == 1); 176 g_assert(depth == 1);
213 g_return_val_if_fail(IS_UI_LIST_MODEL(tree_model), NULL); 213 g_return_val_if_fail(IS_UI_LIST_MODEL(tree_model), NULL);
214 g_return_val_if_fail(iter != NULL, NULL); 214 g_return_val_if_fail(iter != NULL, NULL);
215 g_return_val_if_fail(iter->user_data != NULL, NULL); 215 g_return_val_if_fail(iter->user_data != NULL, NULL);
216 216
217 UiListModel *model = UI_LIST_MODEL(tree_model); 217 UiListModel *model = UI_LIST_MODEL(tree_model);
218 UiList *list = model->list->list;
219 218
220 GtkTreePath *path = gtk_tree_path_new(); 219 GtkTreePath *path = gtk_tree_path_new();
221 gtk_tree_path_append_index(path, (int)(intptr_t)iter->user_data2); // list->index 220 gtk_tree_path_append_index(path, (int)(intptr_t)iter->user_data2); // list->index
222 221
223 return path; 222 return path;
232 g_return_if_fail(IS_UI_LIST_MODEL(tree_model)); 231 g_return_if_fail(IS_UI_LIST_MODEL(tree_model));
233 g_return_if_fail(iter != NULL); 232 g_return_if_fail(iter != NULL);
234 g_return_if_fail(iter->user_data != NULL); 233 g_return_if_fail(iter->user_data != NULL);
235 234
236 UiListModel *model = UI_LIST_MODEL(tree_model); 235 UiListModel *model = UI_LIST_MODEL(tree_model);
237 UiList *list = model->list->list; 236 UiList *list = model->var->value;
238 237
239 g_return_if_fail(column < model->numcolumns); 238 g_return_if_fail(column < model->numcolumns);
240 239
241 // TODO: return correct value from column 240 // TODO: return correct value from column
242 241
259 g_return_val_if_fail(IS_UI_LIST_MODEL(tree_model), FALSE); 258 g_return_val_if_fail(IS_UI_LIST_MODEL(tree_model), FALSE);
260 g_return_val_if_fail(iter != NULL, FALSE); 259 g_return_val_if_fail(iter != NULL, FALSE);
261 g_return_val_if_fail(iter->user_data != NULL, FALSE); 260 g_return_val_if_fail(iter->user_data != NULL, FALSE);
262 261
263 UiListModel *model = UI_LIST_MODEL(tree_model); 262 UiListModel *model = UI_LIST_MODEL(tree_model);
264 UiList *list = model->list->list; 263 UiList *list = model->var->value;
265 list->iter = iter->user_data; 264 list->iter = iter->user_data;
266 //list->index = (int)(intptr_t)iter->user_data2; 265 //list->index = (int)(intptr_t)iter->user_data2;
267 void *val = list->next(list); 266 void *val = list->next(list);
268 iter->user_data = list->iter; 267 iter->user_data = list->iter;
269 intptr_t index = (intptr_t)iter->user_data2; 268 intptr_t index = (intptr_t)iter->user_data2;
280 GtkTreeIter *parent) 279 GtkTreeIter *parent)
281 { 280 {
282 g_return_val_if_fail(IS_UI_LIST_MODEL(tree_model), FALSE); 281 g_return_val_if_fail(IS_UI_LIST_MODEL(tree_model), FALSE);
283 282
284 UiListModel *model = UI_LIST_MODEL(tree_model); 283 UiListModel *model = UI_LIST_MODEL(tree_model);
285 UiList *list = model->list->list; 284 UiList *list = model->var->value;
286 285
287 if(parent) { 286 if(parent) {
288 return FALSE; 287 return FALSE;
289 } 288 }
290 289
315 g_assert(IS_UI_LIST_MODEL(tree_model)); 314 g_assert(IS_UI_LIST_MODEL(tree_model));
316 315
317 if(!iter) { 316 if(!iter) {
318 // return number of rows 317 // return number of rows
319 UiListModel *model = UI_LIST_MODEL(tree_model); 318 UiListModel *model = UI_LIST_MODEL(tree_model);
320 UiList *list = model->list->list; 319 UiList *list = model->var->value;
321 return list->count(list); 320 return list->count(list);
322 } 321 }
323 322
324 return 0; 323 return 0;
325 } 324 }
335 if(parent) { 334 if(parent) {
336 return FALSE; 335 return FALSE;
337 } 336 }
338 337
339 UiListModel *model = UI_LIST_MODEL(tree_model); 338 UiListModel *model = UI_LIST_MODEL(tree_model);
340 UiList *list = model->list->list; 339 UiList *list = model->var->value;
341 340
342 // check n 341 // check n
343 if(n == 0) { 342 if(n == 0) {
344 // we don't need to count if the first element is requested 343 // we don't need to count if the first element is requested
345 if(list->first(list) == NULL) { 344 if(list->first(list) == NULL) {

mercurial