29 #include <stdio.h> |
29 #include <stdio.h> |
30 #include <stdlib.h> |
30 #include <stdlib.h> |
31 |
31 |
32 #include "model.h" |
32 #include "model.h" |
33 #include "image.h" |
33 #include "image.h" |
|
34 #include "toolkit.h" |
34 |
35 |
35 #define IS_UI_LIST_MODEL(obj) \ |
36 #define IS_UI_LIST_MODEL(obj) \ |
36 (G_TYPE_CHECK_INSTANCE_TYPE((obj), list_model_type)) |
37 (G_TYPE_CHECK_INSTANCE_TYPE((obj), list_model_type)) |
37 #define UI_LIST_MODEL(obj) \ |
38 #define UI_LIST_MODEL(obj) \ |
38 (G_TYPE_CHECK_INSTANCE_CAST((obj), list_model_type, UiListModel)) |
39 (G_TYPE_CHECK_INSTANCE_CAST((obj), list_model_type, UiListModel)) |
39 |
40 |
40 static void list_model_class_init(GObjectClass *cl, gpointer data); |
41 static void list_model_class_init(GObjectClass *cl, gpointer data); |
41 static void list_model_interface_init(GtkTreeModelIface *i, gpointer data); |
42 static void list_model_interface_init(GtkTreeModelIface *i, gpointer data); |
42 static void list_model_init(UiListModel *instance, GObjectClass *cl); |
43 static void list_model_init(UiListModel *instance, GObjectClass *cl); |
|
44 |
|
45 static void list_model_dnd_dest_interface_init(GtkTreeDragDestIface *i, gpointer data); |
|
46 static void list_model_dnd_src_interface_init(GtkTreeDragSourceIface *i, gpointer data); |
43 |
47 |
44 static GObjectClass list_model_class; |
48 static GObjectClass list_model_class; |
45 static const GTypeInfo list_model_info = { |
49 static const GTypeInfo list_model_info = { |
46 sizeof(GObjectClass), |
50 sizeof(GObjectClass), |
47 NULL, |
51 NULL, |
68 (GTypeFlags)0); |
83 (GTypeFlags)0); |
69 g_type_add_interface_static( |
84 g_type_add_interface_static( |
70 list_model_type, |
85 list_model_type, |
71 GTK_TYPE_TREE_MODEL, |
86 GTK_TYPE_TREE_MODEL, |
72 &list_model_interface_info); |
87 &list_model_interface_info); |
|
88 g_type_add_interface_static( |
|
89 list_model_type, |
|
90 GTK_TYPE_TREE_DRAG_DEST, |
|
91 &list_model_dnd_dest_interface_info); |
|
92 g_type_add_interface_static( |
|
93 list_model_type, |
|
94 GTK_TYPE_TREE_DRAG_SOURCE, |
|
95 &list_model_dnd_src_interface_info); |
73 } |
96 } |
74 |
97 |
75 static void list_model_class_init(GObjectClass *cl, gpointer data) { |
98 static void list_model_class_init(GObjectClass *cl, gpointer data) { |
76 //cl->finalize = ...; // TODO |
99 //cl->finalize = ...; // TODO |
|
100 |
77 } |
101 } |
78 |
102 |
79 static void list_model_interface_init(GtkTreeModelIface *i, gpointer data) { |
103 static void list_model_interface_init(GtkTreeModelIface *i, gpointer data) { |
80 i->get_flags = ui_list_model_get_flags; |
104 i->get_flags = ui_list_model_get_flags; |
81 i->get_n_columns = ui_list_model_get_n_columns; |
105 i->get_n_columns = ui_list_model_get_n_columns; |
89 i->iter_n_children = ui_list_model_iter_n_children; |
113 i->iter_n_children = ui_list_model_iter_n_children; |
90 i->iter_nth_child = ui_list_model_iter_nth_child; |
114 i->iter_nth_child = ui_list_model_iter_nth_child; |
91 i->iter_parent = ui_list_model_iter_parent; |
115 i->iter_parent = ui_list_model_iter_parent; |
92 } |
116 } |
93 |
117 |
|
118 static void list_model_dnd_dest_interface_init(GtkTreeDragDestIface *i, gpointer data) { |
|
119 i->drag_data_received = ui_list_model_drag_data_received; |
|
120 i->row_drop_possible = ui_list_model_row_drop_possible; |
|
121 } |
|
122 |
|
123 static void list_model_dnd_src_interface_init(GtkTreeDragSourceIface *i, gpointer data) { |
|
124 i->drag_data_delete = ui_list_model_drag_data_delete; |
|
125 i->drag_data_get = ui_list_model_drag_data_get; |
|
126 i->row_draggable = ui_list_model_row_draggable; |
|
127 } |
|
128 |
94 static void list_model_init(UiListModel *instance, GObjectClass *cl) { |
129 static void list_model_init(UiListModel *instance, GObjectClass *cl) { |
95 instance->columntypes = NULL; |
130 instance->columntypes = NULL; |
96 instance->var = NULL; |
131 instance->var = NULL; |
97 instance->numcolumns = 0; |
132 instance->numcolumns = 0; |
98 instance->stamp = g_random_int(); |
133 instance->stamp = g_random_int(); |
126 } |
161 } |
127 } |
162 } |
128 value->g_type = G_TYPE_INVALID; |
163 value->g_type = G_TYPE_INVALID; |
129 } |
164 } |
130 |
165 |
131 UiListModel* ui_list_model_new(UiVar *var, UiModel *info) { |
166 UiListModel* ui_list_model_new(UiObject *obj, UiVar *var, UiModel *info) { |
132 UiListModel *model = g_object_new(list_model_type, NULL); |
167 UiListModel *model = g_object_new(list_model_type, NULL); |
133 model->info = info; |
168 model->obj = obj; |
|
169 model->model = info; |
134 model->var = var; |
170 model->var = var; |
135 model->columntypes = calloc(sizeof(GType), 2 * info->columns); |
171 model->columntypes = calloc(sizeof(GType), 2 * info->columns); |
136 int ncol = 0; |
172 int ncol = 0; |
137 for(int i=0;i<info->columns;i++) { |
173 for(int i=0;i<info->columns;i++) { |
138 UiModelType type = info->types[i]; |
174 UiModelType type = info->types[i]; |
247 |
283 |
248 //value->g_type = G_TYPE_STRING; |
284 //value->g_type = G_TYPE_STRING; |
249 list->iter = iter->user_data; |
285 list->iter = iter->user_data; |
250 //list->index = (int)(intptr_t)iter->user_data2; |
286 //list->index = (int)(intptr_t)iter->user_data2; |
251 //list->current = iter->user_data3; |
287 //list->current = iter->user_data3; |
252 if(model->info->getvalue) { |
288 if(model->model->getvalue) { |
253 void *data = model->info->getvalue(iter->user_data3, column); |
289 void *data = model->model->getvalue(iter->user_data3, column); |
254 if(model->columntypes[column] == G_TYPE_OBJECT) { |
290 if(model->columntypes[column] == G_TYPE_OBJECT) { |
255 UiImage *img = data; |
291 UiImage *img = data; |
256 ui_model_set_value(model->columntypes[column], img->pixbuf, value); |
292 ui_model_set_value(model->columntypes[column], img->pixbuf, value); |
257 } else { |
293 } else { |
258 ui_model_set_value(model->columntypes[column], data, value); |
294 ui_model_set_value(model->columntypes[column], data, value); |
377 GtkTreeIter *iter, |
413 GtkTreeIter *iter, |
378 GtkTreeIter *child) |
414 GtkTreeIter *child) |
379 { |
415 { |
380 return FALSE; |
416 return FALSE; |
381 } |
417 } |
|
418 |
|
419 // ****** dnd ****** |
|
420 |
|
421 gboolean ui_list_model_drag_data_received( |
|
422 GtkTreeDragDest *drag_dest, |
|
423 GtkTreePath *dest_path, |
|
424 GtkSelectionData *selection_data) |
|
425 { |
|
426 //printf("drag received\n"); |
|
427 UiListModel *model = UI_LIST_MODEL(drag_dest); |
|
428 if(model->model->drop) { |
|
429 gint *indices = gtk_tree_path_get_indices(dest_path); |
|
430 gint row = indices[0]; |
|
431 UiEvent e; |
|
432 e.obj = model->obj; |
|
433 e.window = e.obj->window; |
|
434 e.document = e.obj->ctx->document; |
|
435 e.eventdata = NULL; |
|
436 e.intval = 0; |
|
437 UiSelection s; |
|
438 s.data = selection_data; |
|
439 model->model->drop(&e, &s, model->var->value, row); |
|
440 } |
|
441 return TRUE; |
|
442 } |
|
443 |
|
444 gboolean ui_list_model_row_drop_possible( |
|
445 GtkTreeDragDest *drag_dest, |
|
446 GtkTreePath *dest_path, |
|
447 GtkSelectionData *selection_data) |
|
448 { |
|
449 //printf("row_drop_possible\n"); |
|
450 UiListModel *model = UI_LIST_MODEL(drag_dest); |
|
451 if(model->model->candrop) { |
|
452 gint *indices = gtk_tree_path_get_indices(dest_path); |
|
453 gint row = indices[0]; |
|
454 UiEvent e; |
|
455 e.obj = model->obj; |
|
456 e.window = e.obj->window; |
|
457 e.document = e.obj->ctx->document; |
|
458 e.eventdata = NULL; |
|
459 e.intval = 0; |
|
460 UiSelection s; |
|
461 s.data = selection_data; |
|
462 return model->model->candrop(&e, &s, model->var->value, row); |
|
463 } |
|
464 return TRUE; |
|
465 } |
|
466 |
|
467 gboolean ui_list_model_row_draggable( |
|
468 GtkTreeDragSource *drag_source, |
|
469 GtkTreePath *path) |
|
470 { |
|
471 //printf("row_draggable\n"); |
|
472 UiListModel *model = UI_LIST_MODEL(drag_source); |
|
473 if(model->model->candrag) { |
|
474 gint *indices = gtk_tree_path_get_indices(path); |
|
475 gint row = indices[0]; |
|
476 UiEvent e; |
|
477 e.obj = model->obj; |
|
478 e.window = e.obj->window; |
|
479 e.document = e.obj->ctx->document; |
|
480 e.eventdata = NULL; |
|
481 e.intval = 0; |
|
482 return model->model->candrag(&e, model->var->value, row); |
|
483 } |
|
484 return TRUE; |
|
485 } |
|
486 |
|
487 gboolean ui_list_model_drag_data_get( |
|
488 GtkTreeDragSource *drag_source, |
|
489 GtkTreePath *path, |
|
490 GtkSelectionData *selection_data) |
|
491 { |
|
492 //printf("drag_data_get\n"); |
|
493 UiListModel *model = UI_LIST_MODEL(drag_source); |
|
494 if(model->model->data_get) { |
|
495 gint *indices = gtk_tree_path_get_indices(path); |
|
496 gint row = indices[0]; |
|
497 UiEvent e; |
|
498 e.obj = model->obj; |
|
499 e.window = e.obj->window; |
|
500 e.document = e.obj->ctx->document; |
|
501 e.eventdata = NULL; |
|
502 e.intval = 0; |
|
503 UiSelection s; |
|
504 s.data = selection_data; |
|
505 model->model->data_get(&e, &s, model->var->value, row); |
|
506 } |
|
507 return TRUE; |
|
508 } |
|
509 |
|
510 gboolean ui_list_model_drag_data_delete( |
|
511 GtkTreeDragSource *drag_source, |
|
512 GtkTreePath *path) |
|
513 { |
|
514 //printf("drag_data_delete\n"); |
|
515 UiListModel *model = UI_LIST_MODEL(drag_source); |
|
516 if(model->model->data_get) { |
|
517 gint *indices = gtk_tree_path_get_indices(path); |
|
518 gint row = indices[0]; |
|
519 UiEvent e; |
|
520 e.obj = model->obj; |
|
521 e.window = e.obj->window; |
|
522 e.document = e.obj->ctx->document; |
|
523 e.eventdata = NULL; |
|
524 e.intval = 0; |
|
525 model->model->data_delete(&e, model->var->value, row); |
|
526 } |
|
527 return TRUE; |
|
528 } |