ui/gtk/list.c

changeset 60
ee4e4742391e
parent 51
e324291ca9f8
equal deleted inserted replaced
59:6bd37fe6d905 60:ee4e4742391e
160 UIWIDGET ui_listview_create(UiObject *obj, UiListArgs args) { 160 UIWIDGET ui_listview_create(UiObject *obj, UiListArgs args) {
161 UiObject* current = uic_current_obj(obj); 161 UiObject* current = uic_current_obj(obj);
162 162
163 // create treeview 163 // create treeview
164 GtkWidget *view = gtk_tree_view_new(); 164 GtkWidget *view = gtk_tree_view_new();
165 ui_set_name_and_style(view, args.name, args.style_class);
166 ui_set_widget_groups(obj->ctx, view, args.groups);
165 GtkCellRenderer *renderer = gtk_cell_renderer_text_new(); 167 GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
166 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes(NULL, renderer, "text", 0, NULL); 168 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes(NULL, renderer, "text", 0, NULL);
167 gtk_tree_view_append_column(GTK_TREE_VIEW(view), column); 169 gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
168 170
169 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE); 171 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), FALSE);
176 #else 178 #else
177 // TODO: implement for gtk2 179 // TODO: implement for gtk2
178 #endif 180 #endif
179 181
180 UiModel *model = ui_model(obj->ctx, UI_STRING, "", -1); 182 UiModel *model = ui_model(obj->ctx, UI_STRING, "", -1);
181 model->getvalue = args.getvalue; 183 model->getvalue = args.getvalue ? args.getvalue : ui_strmodel_getvalue;
182 184
183 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST); 185 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST);
184 186
185 UiList *list = var ? var->value : NULL; 187 UiList *list = var ? var->value : NULL;
186 GtkListStore *listmodel = create_list_store(list, model); 188 GtkListStore *listmodel = create_list_store(list, model);
368 } 370 }
369 // TODO: destroy callback 371 // TODO: destroy callback
370 372
371 373
372 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(view)); 374 GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(view));
373 gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE); 375 if(args.multiselection) {
376 gtk_tree_selection_set_mode(selection, GTK_SELECTION_MULTIPLE);
377 }
374 378
375 // add widget to the current container 379 // add widget to the current container
376 GtkWidget *scroll_area = SCROLLEDWINDOW_NEW(); 380 GtkWidget *scroll_area = SCROLLEDWINDOW_NEW();
377 gtk_scrolled_window_set_policy( 381 gtk_scrolled_window_set_policy(
378 GTK_SCROLLED_WINDOW(scroll_area), 382 GTK_SCROLLED_WINDOW(scroll_area),
580 584
581 UIWIDGET ui_combobox_create(UiObject *obj, UiListArgs args) { 585 UIWIDGET ui_combobox_create(UiObject *obj, UiListArgs args) {
582 UiObject* current = uic_current_obj(obj); 586 UiObject* current = uic_current_obj(obj);
583 587
584 UiModel *model = ui_model(obj->ctx, UI_STRING, "", -1); 588 UiModel *model = ui_model(obj->ctx, UI_STRING, "", -1);
585 model->getvalue = args.getvalue; 589 model->getvalue = args.getvalue ? args.getvalue : ui_strmodel_getvalue;
586 590
587 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST); 591 UiVar* var = uic_widget_var(obj->ctx, current->ctx, args.list, args.varname, UI_VAR_LIST);
588 592
589 GtkWidget *combobox = ui_create_combobox(obj, model, var, args.onactivate, args.onactivatedata); 593 GtkWidget *combobox = ui_create_combobox(obj, model, var, args.onactivate, args.onactivatedata);
594 ui_set_name_and_style(combobox, args.name, args.style_class);
595 ui_set_widget_groups(obj->ctx, combobox, args.groups);
590 UI_APPLY_LAYOUT1(current, args); 596 UI_APPLY_LAYOUT1(current, args);
591 current->container->add(current->container, combobox, FALSE); 597 current->container->add(current->container, combobox, FALSE);
592 current->container->current = combobox; 598 current->container->current = combobox;
593 return combobox; 599 return combobox;
594 } 600 }
595 601
596 GtkWidget* ui_create_combobox(UiObject *obj, UiModel *model, UiVar *var, ui_callback f, void *udata) { 602 GtkWidget* ui_create_combobox(UiObject *obj, UiModel *model, UiVar *var, ui_callback f, void *udata) {
597 GtkWidget *combobox = gtk_combo_box_new(); 603 GtkWidget *combobox = gtk_combo_box_new();
598 604
599 UiListView *uicbox = malloc(sizeof(UiListView)); 605 UiListView *uicbox = malloc(sizeof(UiListView));
600 uicbox->obj = obj; 606 uicbox->obj = obj;
601 uicbox->widget = combobox; 607 uicbox->widget = combobox;
602 608
603 UiList *list = var ? var->value : NULL; 609 UiList *list = var ? var->value : NULL;
617 uicbox); 623 uicbox);
618 624
619 // bind var 625 // bind var
620 if(list) { 626 if(list) {
621 list->update = ui_combobox_modelupdate; 627 list->update = ui_combobox_modelupdate;
622 // TODO: combobox getselection 628 list->getselection = ui_combobox_getselection;
629 list->setselection = ui_combobox_setselection;
623 list->obj = uicbox; 630 list->obj = uicbox;
624 } 631 }
625 632
626 GtkCellRenderer *renderer = gtk_cell_renderer_text_new(); 633 GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
627 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), renderer, TRUE); 634 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), renderer, TRUE);
666 UiListView *view = list->obj; 673 UiListView *view = list->obj;
667 GtkListStore *store = create_list_store(view->var->value, view->model); 674 GtkListStore *store = create_list_store(view->var->value, view->model);
668 gtk_combo_box_set_model(GTK_COMBO_BOX(view->widget), GTK_TREE_MODEL(store)); 675 gtk_combo_box_set_model(GTK_COMBO_BOX(view->widget), GTK_TREE_MODEL(store));
669 } 676 }
670 677
678 UiListSelection ui_combobox_getselection(UiList *list) {
679 UiListView *combobox = list->obj;
680 UiListSelection ret;
681 ret.rows = malloc(sizeof(int*));
682 ret.count = 1;
683 ret.rows[0] = gtk_combo_box_get_active(GTK_COMBO_BOX(combobox->widget));
684 return ret;
685 }
686
687 void ui_combobox_setselection(UiList *list, UiListSelection selection) {
688 UiListView *combobox = list->obj;
689 if(selection.count > 0) {
690 gtk_combo_box_set_active(GTK_COMBO_BOX(combobox->widget), selection.rows[0]);
691 }
692 }

mercurial