ui/gtk/tree.c

changeset 116
480354705c2f
parent 86
3c63f57a8f77
child 123
55adc92e7c09
equal deleted inserted replaced
115:102fc0b8fe3e 116:480354705c2f
305 return -1; 305 return -1;
306 } 306 }
307 int *indices = gtk_tree_path_get_indices(path); 307 int *indices = gtk_tree_path_get_indices(path);
308 return indices[depth - 1]; 308 return indices[depth - 1];
309 } 309 }
310
311
312 /* --------------------------- ComboBox --------------------------- */
313
314 UIWIDGET ui_combobox_str(UiObject *obj, UiList *list, ui_callback f, void *udata) {
315 return ui_combobox(obj, list, ui_strmodel_getvalue, f, udata);
316 }
317
318 UIWIDGET ui_combobox(UiObject *obj, UiList *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
319 UiListPtr *listptr = ucx_mempool_malloc(obj->ctx->mempool, sizeof(UiListPtr));
320 listptr->list = list;
321 return ui_combobox_var(obj, listptr, getvalue, f, udata);
322 }
323
324 UIWIDGET ui_combobox_nv(UiObject *obj, char *varname, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
325 UiVar *var = uic_connect_var(obj->ctx, varname, UI_VAR_LIST);
326 if(var) {
327 UiListVar *value = var->value;
328 return ui_combobox_var(obj, value->listptr, getvalue, f, udata);
329 } else {
330 // TODO: error
331 }
332 return NULL;
333 }
334
335 UIWIDGET ui_combobox_var(UiObject *obj, UiListPtr *list, ui_model_getvalue_f getvalue, ui_callback f, void *udata) {
336 UiModelInfo *modelinfo = ui_model_info(obj->ctx, UI_STRING, "", -1);
337 modelinfo->getvalue = getvalue;
338 UiListModel *model = ui_list_model_new(list, modelinfo);
339
340 GtkWidget *combobox = ui_create_combobox(obj, model, f, udata);
341 UiContainer *ct = uic_get_current_container(obj);
342 ct->add(ct, combobox, FALSE);
343 }
344
345 GtkWidget* ui_create_combobox(UiObject *obj, UiListModel *model, ui_callback f, void *udata) {
346 GtkWidget *combobox = gtk_combo_box_new_with_model(GTK_TREE_MODEL(model));
347
348 UiList *list = model->list->list;
349 list->observers = ui_add_observer(
350 list->observers,
351 (ui_callback)ui_listview_update,
352 combobox);
353
354 GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
355 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox), renderer, TRUE);
356 gtk_cell_layout_set_attributes(
357 GTK_CELL_LAYOUT(combobox),
358 renderer,
359 "text",
360 0,
361 NULL);
362 gtk_combo_box_set_active(GTK_COMBO_BOX(combobox), 0);
363
364 // add callback
365 if(f) {
366 UiEventData *event = ui_malloc(obj->ctx, sizeof(UiEventData));
367 event->obj = obj;
368 event->userdata = udata;
369 event->callback = f;
370 event->value = 0;
371
372 g_signal_connect(
373 combobox,
374 "changed",
375 G_CALLBACK(ui_combobox_change_event),
376 event);
377 }
378
379 return combobox;
380 }
381
382 void ui_combobox_change_event(GtkComboBox *widget, UiEventData *e) {
383 UiEvent event;
384 event.obj = e->obj;
385 event.window = event.obj->window;
386 event.document = event.obj->ctx->document;
387 event.eventdata = NULL;
388 event.intval = gtk_combo_box_get_active(widget);
389 e->callback(&event, e->userdata);
390 }
391
392 void ui_combobox_update(UiEvent *event, void *combobox) {
393 printf("ui_combobox_update\n");
394 printf("TODO: implement\n");
395 }
396

mercurial