ui/win32/list.c

changeset 936
d40a72210be8
parent 919
d9018dcd4e2d
child 938
be4c88ded783
equal deleted inserted replaced
935:d95e8723545c 936:d40a72210be8
58 } 58 }
59 59
60 /* 60 /*
61 * Creates an UiListView widget object and initializes it from the UiListArgs 61 * Creates an UiListView widget object and initializes it from the UiListArgs
62 */ 62 */
63 static UiListView* create_listview_widget(UiObject *obj, HWND hwnd, UiListArgs *args, UiBool table) { 63 static UiListView* create_listview_widget(UiObject *obj, W32WidgetClass *widget_class, HWND hwnd, UiListArgs *args, UiBool table) {
64 UiListView *listview = w32_widget_create(&listview_widget_class, hwnd, sizeof(UiListView)); 64 UiListView *listview = w32_widget_create(widget_class, hwnd, sizeof(UiListView));
65 listview->widget.hwnd = hwnd; 65 listview->widget.hwnd = hwnd;
66 listview->obj = obj; 66 listview->obj = obj;
67 listview->preferred_width = args->width ? args->width : 300; // 300: default width/height 67 listview->preferred_width = args->width ? args->width : 300; // 300: default width/height
68 listview->preferred_height = args->height ? args->height : 300; 68 listview->preferred_height = args->height ? args->height : 300;
69 listview->onactivate = args->onactivate; 69 listview->onactivate = args->onactivate;
117 ListView_SetExtendedListViewStyle( 117 ListView_SetExtendedListViewStyle(
118 hwnd, 118 hwnd,
119 LVS_EX_FULLROWSELECT //| LVS_EX_GRIDLINES 119 LVS_EX_FULLROWSELECT //| LVS_EX_GRIDLINES
120 ); 120 );
121 121
122 UiListView *listview = create_listview_widget(obj, hwnd, args, table); 122 UiListView *listview = create_listview_widget(obj, &listview_widget_class, hwnd, args, table);
123 ui_container_add(container, (W32Widget*)listview, &layout); 123 ui_container_add(container, (W32Widget*)listview, &layout);
124 124
125 // init list model 125 // init list model
126 // always initialize listview->model 126 // always initialize listview->model
127 int numcolumns = 0; 127 int numcolumns = 0;
341 341
342 // public API 342 // public API
343 UIWIDGET ui_table_create(UiObject *obj, UiListArgs *args) { 343 UIWIDGET ui_table_create(UiObject *obj, UiListArgs *args) {
344 return listview_create(obj, args, TRUE); 344 return listview_create(obj, args, TRUE);
345 } 345 }
346
347
348 /* ------------------------------------ DropDown ------------------------------------*/
349
350 static W32WidgetClass dropdown_widget_class = {
351 .eventproc = ui_dropdown_eventproc,
352 .enable = w32_widget_default_enable,
353 .show = w32_widget_default_show,
354 .get_preferred_size = ui_dropdown_get_preferred_size,
355 .destroy = w32_widget_default_destroy
356 };
357
358 UIWIDGET ui_combobox_create(UiObject *obj, UiListArgs *args) {
359 HINSTANCE hInstance = GetModuleHandle(NULL);
360 UiContainerPrivate *container = ui_obj_container(obj);
361 HWND parent = ui_container_get_parent(container);
362 UiLayout layout = UI_ARGS2LAYOUT(args);
363
364 HWND hwnd = CreateWindowEx(
365 WS_EX_CLIENTEDGE,
366 WC_COMBOBOX,
367 "",
368 WS_CHILD | WS_VISIBLE | WS_VSCROLL | CBS_DROPDOWNLIST,
369 0, 0, 100, 100,
370 parent,
371 (HMENU)1337,
372 hInstance,
373 NULL);
374 ui_win32_set_ui_font(hwnd);
375
376 UiListView *dropdown = create_listview_widget(obj, &dropdown_widget_class, hwnd, args, FALSE);
377 ui_container_add(container, (W32Widget*)dropdown, &layout);
378
379 // bind the dropdown to the provided UiList
380 if (dropdown->var) {
381 UiList *list = dropdown->var->value;
382 list->obj = dropdown;
383 list->update = ui_dropdown_update;
384 list->getselection = ui_dropdown_getselection;
385 list->setselection = ui_dropdown_setselection;
386
387 ui_dropdown_update(list, -1);
388 }
389
390
391 return (W32Widget*)dropdown;
392 }
393
394 void ui_dropdown_eventproc(W32Widget *widget, HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
395
396 }
397
398 W32Size ui_dropdown_get_preferred_size(W32Widget *widget) {
399 W32Size size;
400 size.width = 200;
401 size.height = 30;
402 return size;
403 }
404
405 static void dropdown_insert_item(UiList *list, int row, void *elm) {
406 UiListView *listview = (UiListView*)list->obj;
407 HWND hwnd = listview->widget.hwnd;
408
409 UiBool freeResult = FALSE;
410 char *str = listview->getvalue(list, elm, row, 0, listview->getvaluedata, &freeResult);
411 SendMessage(hwnd, CB_ADDSTRING, 0, (LPARAM)str);
412
413 if (freeResult) {
414 free(str);
415 }
416 }
417
418 void ui_dropdown_update(UiList *list, int row) {
419 UiListView *listview = (UiListView*)list->obj;
420 HWND hwnd = listview->widget.hwnd;
421 if (row < 0) {
422 SendMessage(hwnd, CB_RESETCONTENT, 0, 0);
423
424 void *elm = list->first(list);
425 int row = 0;
426 while (elm) {
427 dropdown_insert_item(list, row, elm);
428 elm = list->next(list);
429 row++;
430 }
431 } else {
432 SendMessage(hwnd, CB_DELETESTRING, row, 0);
433 void *elm = list->get(list, row);
434 dropdown_insert_item(list, row, elm);
435 }
436 }
437
438 UiListSelection ui_dropdown_getselection(UiList *list) {
439 UiListSelection sel = { 0, NULL };
440 UiListView *listview = (UiListView*)list->obj;
441 int index = (int)SendMessage(listview->widget.hwnd, CB_GETCURSEL, 0, 0);
442 if (index >= 0) {
443 sel.rows = malloc(sizeof(int));
444 sel.rows[0] = index;
445 sel.count = 1;
446 }
447 return sel;
448 }
449
450 void ui_dropdown_setselection(UiList *list, UiListSelection selection) {
451 UiListView *listview = (UiListView*)list->obj;
452 SendMessage(listview->widget.hwnd, CB_SETCURSEL, 0, 0);
453 }

mercurial