trying to fix the combobox sizing
authorJohn Tsiombikas <nuclear@member.fsf.org>
Fri, 10 Jan 2020 07:59:10 +0000 (09:59 +0200)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Fri, 10 Jan 2020 07:59:10 +0000 (09:59 +0200)
src/widgets.c

index 2ad9983..82e4933 100644 (file)
@@ -285,35 +285,33 @@ struct wgt_widget *wgt_checkbox(struct wgt_window *win, const char *text, int on
        return w;
 }
 
        return w;
 }
 
-static void combosize(struct wgt_window *win, const char **items, int num_items,
+static void combosize(struct wgt_window *win, HWND cbwnd, const char **items, int num_items,
                int width, int height, struct wgt_rect *sz)
 {
                int width, int height, struct wgt_rect *sz)
 {
-       int i, max_width, max_height, sum_height;
+       int i, max_width, sum_height;
        struct wgt_rect textsz;
 
        wgt_string_size(win, "00", &textsz);
        max_width = width < 0 && width != WGT_AUTO ? -width : textsz.width;
        struct wgt_rect textsz;
 
        wgt_string_size(win, "00", &textsz);
        max_width = width < 0 && width != WGT_AUTO ? -width : textsz.width;
-       max_height = height < 0 && height != WGT_AUTO ? -height : textsz.height;
-       sum_height = num_items ? 0 : max_height;
+
+       sum_height = SendMessage(cbwnd, CB_GETITEMHEIGHT, -1, 0);
+       sz->y = sum_height;
 
        for(i=0; i<num_items; i++) {
                wgt_string_size(win, items[i], &textsz);
                if(textsz.width > max_width) max_width = textsz.width;
 
        for(i=0; i<num_items; i++) {
                wgt_string_size(win, items[i], &textsz);
                if(textsz.width > max_width) max_width = textsz.width;
-               if(textsz.height > max_height) max_height = textsz.height;
-               sum_height += textsz.height;
+               sum_height += SendMessage(cbwnd, CB_GETITEMHEIGHT, i, 0);
        }
        if(width < 0) width = max_width * 3 / 2;
        }
        if(width < 0) width = max_width * 3 / 2;
-       if(height < 0) height = sum_height * 2;
+       if(height < 0) height = sum_height;
 
        sz->width = width;
 
        sz->width = width;
-       sz->y = max_height;
        sz->height = height;
 }
 
 struct wgt_widget *wgt_combo(struct wgt_window *win, const char **items, int num_items,
                        int sel, int x, int y, int width, int height, wgt_callback modfunc)
 {
        sz->height = height;
 }
 
 struct wgt_widget *wgt_combo(struct wgt_window *win, const char **items, int num_items,
                        int sel, int x, int y, int width, int height, wgt_callback modfunc)
 {
-       int i, res;
        struct wgt_widget *w;
        struct wgt_rect rect;
 
        struct wgt_widget *w;
        struct wgt_rect rect;
 
@@ -323,36 +321,22 @@ struct wgt_widget *wgt_combo(struct wgt_window *win, const char **items, int num
        }
        w->win = win;
 
        }
        w->win = win;
 
-       if(!(w->itemlist = malloc(num_items * sizeof *w->itemlist))) {
-               fprintf(stderr, "wgt_combo: failed to allocate item list\n");
-               free(w);
-               return 0;
-       }
-
-       combosize(win, items, num_items, width, height, &rect);
-
        if(!(w->handle = CreateWindow("COMBOBOX", items[0] ? items[0] : "",
                        WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | CBS_HASSTRINGS,
        if(!(w->handle = CreateWindow("COMBOBOX", items[0] ? items[0] : "",
                        WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | CBS_HASSTRINGS,
-                       x, y, rect.width, rect.height, win->handle, 0, GetModuleHandle(0), 0))) {
+                       x, y, 16, 16, win->handle, 0, GetModuleHandle(0), 0))) {
                fprintf(stderr, "wgt_combo: failed to create window\n");
                free(w);
                return 0;
        }
 
                fprintf(stderr, "wgt_combo: failed to create window\n");
                free(w);
                return 0;
        }
 
-       for(i=0; i<num_items; i++) {
-               if(!(w->itemlist[i] = strdup(items[i]))) {
-                       fprintf(stderr, "wgt_combo: failed to allocate item\n");
-                       while(--i >= 0) free(w->itemlist[i]);
-                       DestroyWindow(w->handle);
-                       free(w->itemlist);
-                       free(w);
-                       return 0;
-               }
-               if((res = SendMessage(w->handle, CB_ADDSTRING, 0, (long)items[i])) != i) {
-                       fprintf(stderr, "wgt_combo: failed to add item\n");
-               }
-       }
-       w->num_items = num_items;
+       combosize(win, w->handle, items, num_items, width, height, &rect);
+       w->rect.x = x;
+       w->rect.y = y;
+       w->rect.width = rect.width;
+       w->rect.height = rect.y;        /* single line height for layout purposes */
+       MoveWindow(w->handle, x, y, rect.width, rect.height, FALSE);
+
+       wgt_combo_setitems(w, items, num_items);
 
        if(sel < 0) sel = 0;
        if(sel >= num_items) sel = num_items - 1;
 
        if(sel < 0) sel = 0;
        if(sel >= num_items) sel = num_items - 1;
@@ -360,10 +344,6 @@ struct wgt_widget *wgt_combo(struct wgt_window *win, const char **items, int num
                SendMessage(w->handle, CB_SETCURSEL, sel, 0);
        }
 
                SendMessage(w->handle, CB_SETCURSEL, sel, 0);
        }
 
-       w->rect.x = x;
-       w->rect.y = y;
-       w->rect.width = rect.width;
-       w->rect.height = rect.y;        /* single line height for layout purposes */
 
        w->cb_modify = modfunc;
        w->next = win->wlist;
 
        w->cb_modify = modfunc;
        w->next = win->wlist;
@@ -420,7 +400,7 @@ int wgt_combo_setitems(struct wgt_widget *w, const char **items, int num_items)
                SendMessage(w->handle, CB_ADDSTRING, 0, (long)w->itemlist[i]);
        }
 
                SendMessage(w->handle, CB_ADDSTRING, 0, (long)w->itemlist[i]);
        }
 
-       combosize(w->win, items, num_items, w->rect.width, WGT_AUTO, &size);
+       combosize(w->win, w->handle, items, num_items, w->rect.width, WGT_AUTO, &size);
        MoveWindow(w->handle, w->rect.x, w->rect.y, w->rect.width, size.height, TRUE);
        return 0;
 }
        MoveWindow(w->handle, w->rect.x, w->rect.y, w->rect.width, size.height, TRUE);
        return 0;
 }