ui/gtk/tree.c

branch
newapi
changeset 291
302a150ff5e4
parent 278
a8faf8757450
child 299
48763a9d19a7
equal deleted inserted replaced
290:0dcf13ba5a78 291:302a150ff5e4
184 G_CALLBACK(ui_listview_destroy), 184 G_CALLBACK(ui_listview_destroy),
185 listview); 185 listview);
186 186
187 // bind var 187 // bind var
188 list->update = ui_listview_update; 188 list->update = ui_listview_update;
189 list->getselection = ui_listview_getselection;
189 list->obj = listview; 190 list->obj = listview;
190 191
191 // add callback 192 // add callback
192 if(args.onactivate) { 193 if(args.onactivate) {
193 UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData)); 194 UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData));
320 G_CALLBACK(ui_listview_destroy), 321 G_CALLBACK(ui_listview_destroy),
321 tableview); 322 tableview);
322 323
323 // bind var 324 // bind var
324 list->update = ui_listview_update; 325 list->update = ui_listview_update;
326 list->getselection = ui_listview_getselection;
325 list->obj = tableview; 327 list->obj = tableview;
326 328
327 // add callback 329 // add callback
328 UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData)); 330 UiTreeEventData *event = ui_malloc(obj->ctx, sizeof(UiTreeEventData));
329 event->obj = obj; 331 event->obj = obj;
461 gtk_tree_view_set_model(GTK_TREE_VIEW(view->widget), GTK_TREE_MODEL(store)); 463 gtk_tree_view_set_model(GTK_TREE_VIEW(view->widget), GTK_TREE_MODEL(store));
462 g_object_unref(G_OBJECT(store)); 464 g_object_unref(G_OBJECT(store));
463 // TODO: free old model 465 // TODO: free old model
464 } 466 }
465 467
468 UiListSelection ui_listview_getselection(UiList *list) {
469 UiListView *view = list->obj;
470 UiListSelection selection = ui_listview_selection(
471 gtk_tree_view_get_selection(GTK_TREE_VIEW(view->widget)),
472 NULL);
473 return selection;
474 }
475
466 void ui_listview_destroy(GtkWidget *w, UiListView *v) { 476 void ui_listview_destroy(GtkWidget *w, UiListView *v) {
467 gtk_tree_view_set_model(GTK_TREE_VIEW(w), NULL); 477 gtk_tree_view_set_model(GTK_TREE_VIEW(w), NULL);
468 ui_destroy_boundvar(v->obj->ctx, v->var); 478 ui_destroy_boundvar(v->obj->ctx, v->var);
469 // TODO: destroy model? 479 // TODO: destroy model?
470 free(v); 480 free(v);
482 GtkTreeView *treeview, 492 GtkTreeView *treeview,
483 GtkTreePath *path, 493 GtkTreePath *path,
484 GtkTreeViewColumn *column, 494 GtkTreeViewColumn *column,
485 UiTreeEventData *event) 495 UiTreeEventData *event)
486 { 496 {
487 UiListSelection *selection = ui_listview_selection( 497 UiListSelection selection = ui_listview_selection(
488 gtk_tree_view_get_selection(treeview), 498 gtk_tree_view_get_selection(treeview),
489 event); 499 event);
490 500
491 UiEvent e; 501 UiEvent e;
492 e.obj = event->obj; 502 e.obj = event->obj;
493 e.window = event->obj->window; 503 e.window = event->obj->window;
494 e.document = event->obj->ctx->document; 504 e.document = event->obj->ctx->document;
495 e.eventdata = selection; 505 e.eventdata = &selection;
496 e.intval = selection->count > 0 ? selection->rows[0] : -1; 506 e.intval = selection.count > 0 ? selection.rows[0] : -1;
497 event->activate(&e, event->activatedata); 507 event->activate(&e, event->activatedata);
498 508
499 if(selection->count > 0) { 509 if(selection.count > 0) {
500 free(selection->rows); 510 free(selection.rows);
501 } 511 }
502 free(selection);
503 } 512 }
504 513
505 void ui_listview_selection_event( 514 void ui_listview_selection_event(
506 GtkTreeSelection *treeselection, 515 GtkTreeSelection *treeselection,
507 UiTreeEventData *event) 516 UiTreeEventData *event)
508 { 517 {
509 UiListSelection *selection = ui_listview_selection(treeselection, event); 518 UiListSelection selection = ui_listview_selection(treeselection, event);
510 519
511 UiEvent e; 520 UiEvent e;
512 e.obj = event->obj; 521 e.obj = event->obj;
513 e.window = event->obj->window; 522 e.window = event->obj->window;
514 e.document = event->obj->ctx->document; 523 e.document = event->obj->ctx->document;
515 e.eventdata = selection; 524 e.eventdata = &selection;
516 e.intval = selection->count > 0 ? selection->rows[0] : -1; 525 e.intval = selection.count > 0 ? selection.rows[0] : -1;
517 event->selection(&e, event->selectiondata); 526 event->selection(&e, event->selectiondata);
518 527
519 if(selection->count > 0) { 528 if(selection.count > 0) {
520 free(selection->rows); 529 free(selection.rows);
521 } 530 }
522 free(selection); 531 }
523 } 532
524 533 UiListSelection ui_listview_selection(
525 UiListSelection* ui_listview_selection(
526 GtkTreeSelection *selection, 534 GtkTreeSelection *selection,
527 UiTreeEventData *event) 535 UiTreeEventData *event)
528 { 536 {
529 GList *rows = gtk_tree_selection_get_selected_rows(selection, NULL); 537 GList *rows = gtk_tree_selection_get_selected_rows(selection, NULL);
530 538
531 UiListSelection *ls = malloc(sizeof(UiListSelection)); 539 UiListSelection ls;
532 ls->count = g_list_length(rows); 540 ls.count = g_list_length(rows);
533 ls->rows = calloc(ls->count, sizeof(int)); 541 ls.rows = calloc(ls.count, sizeof(int));
534 GList *r = rows; 542 GList *r = rows;
535 int i = 0; 543 int i = 0;
536 while(r) { 544 while(r) {
537 GtkTreePath *path = r->data; 545 GtkTreePath *path = r->data;
538 ls->rows[i] = ui_tree_path_list_index(path); 546 ls.rows[i] = ui_tree_path_list_index(path);
539 r = r->next; 547 r = r->next;
540 i++; 548 i++;
541 } 549 }
542 return ls; 550 return ls;
543 } 551 }
594 uicbox); 602 uicbox);
595 603
596 // bind var 604 // bind var
597 if(list) { 605 if(list) {
598 list->update = ui_combobox_modelupdate; 606 list->update = ui_combobox_modelupdate;
607 // TODO: combobox getselection
599 list->obj = uicbox; 608 list->obj = uicbox;
600 } 609 }
601 610
602 GtkCellRenderer *renderer = gtk_cell_renderer_text_new(); 611 GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
603 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), renderer, TRUE); 612 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), renderer, TRUE);

mercurial