ui/gtk/tree.c

changeset 116
480354705c2f
parent 86
3c63f57a8f77
child 123
55adc92e7c09
--- a/ui/gtk/tree.c	Mon Jan 25 16:36:31 2016 +0100
+++ b/ui/gtk/tree.c	Tue Jan 26 19:45:28 2016 +0100
@@ -307,3 +307,90 @@
     int *indices = gtk_tree_path_get_indices(path);
     return indices[depth - 1];
 }
+
+
+/* --------------------------- ComboBox ---------------------------  */
+
+UIWIDGET ui_combobox_str(UiObject *obj, UiList *list, ui_callback f, void *udata) {
+    return ui_combobox(obj, list, ui_strmodel_getvalue, f, udata);
+}
+
+UIWIDGET ui_combobox(UiObject *obj, UiList *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
+    UiListPtr *listptr = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr));
+    listptr->list = list;
+    return ui_combobox_var(obj, listptr, getvalue, f, udata);
+}
+
+UIWIDGET ui_combobox_nv(UiObject *obj, char *varname, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
+    UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST);
+    if(var) {
+        UiListVar *value = var->value;
+        return ui_combobox_var(obj, value->listptr, getvalue, f, udata);
+    } else {
+        // TODO: error
+    }
+    return NULL;
+}
+
+UIWIDGET ui_combobox_var(UiObject *obj, UiListPtr *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
+    UiModelInfo *modelinfo = ui_model_info(obj->ctx, UI_STRING, "", -1);
+    modelinfo->getvalue = getvalue;
+    UiListModel *model = ui_list_model_new(list, modelinfo);
+    
+    GtkWidget *combobox = ui_create_combobox(obj, model, f, udata);
+    UiContainer *ct = uic_get_current_container(obj);
+    ct->add(ct, combobox, FALSE);
+}
+
+GtkWidget* ui_create_combobox(UiObject *obj, UiListModel *model, ui_callback f, void *udata) {
+    GtkWidget *combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
+    
+    UiList *list = model->list->list;
+    list->observers = ui_add_observer(
+            list->observers,
+            (ui_callback)ui_listview_update,
+            combobox);
+    
+    GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
+    gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), renderer, TRUE);
+    gtk_cell_layout_set_attributes(
+            GTK_CELL_LAYOUT(combobox),
+            renderer,
+            "text",
+            0,
+            NULL);
+    gtk_combo_box_set_active(GTK_COMBO_BOX(combobox), 0);
+    
+    // add callback
+    if(f) {
+        UiEventData *event = ui_malloc(obj->ctx, sizeof(UiEventData));
+        event->obj = obj;
+        event->userdata = udata;
+        event->callback = f;
+        event->value = 0;
+
+        g_signal_connect(
+                combobox,
+                "changed",
+                G_CALLBACK(ui_combobox_change_event),
+                event);
+    }
+    
+    return combobox;
+}
+
+void ui_combobox_change_event(GtkComboBox *widget, UiEventData *e) {
+    UiEvent event;
+    event.obj = e->obj;
+    event.window = event.obj->window;
+    event.document = event.obj->ctx->document;
+    event.eventdata = NULL;
+    event.intval = gtk_combo_box_get_active(widget);
+    e->callback(&event, e->userdata);
+}
+
+void ui_combobox_update(UiEvent *event, void *combobox) {
+    printf("ui_combobox_update\n");
+    printf("TODO: implement\n");
+}
+

mercurial