X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=instimg;a=blobdiff_plain;f=src%2Fwidgets.c;h=2ad99839ba307f6fd653273fc949cd3081fbb4df;hp=2c08856c53ac3241b296138f70594404b64270a8;hb=9f8f8c6cde6f5d9174aa8bc88f530436aecb70d9;hpb=d9e1c38ba7dc1bb25604586a197b794983e28d78 diff --git a/src/widgets.c b/src/widgets.c index 2c08856..2ad9983 100644 --- a/src/widgets.c +++ b/src/widgets.c @@ -24,6 +24,8 @@ struct wgt_widget { char **itemlist; int num_items; + int disabled; + wgt_callback cb_click; wgt_callback cb_modify; }; @@ -118,6 +120,23 @@ void wgt_destroy_widget(struct wgt_widget *w) free(w->itemlist); } +void wgt_enable_widget(struct wgt_widget *w) +{ + w->disabled = 0; + EnableWindow(w->handle, 1); +} + +void wgt_disable_widget(struct wgt_widget *w) +{ + w->disabled = 1; + EnableWindow(w->handle, 0); +} + +int wgt_widget_enabled(struct wgt_widget *w) +{ + return !w->disabled; +} + struct wgt_window *wgt_widget_window(struct wgt_widget *w) { return w->win; @@ -266,12 +285,37 @@ struct wgt_widget *wgt_checkbox(struct wgt_window *win, const char *text, int on return w; } +static void combosize(struct wgt_window *win, const char **items, int num_items, + int width, int height, struct wgt_rect *sz) +{ + int i, max_width, max_height, sum_height; + 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; + + for(i=0; i max_width) max_width = textsz.width; + if(textsz.height > max_height) max_height = textsz.height; + sum_height += textsz.height; + } + if(width < 0) width = max_width * 3 / 2; + if(height < 0) height = sum_height * 2; + + 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) { - int i, max_width, max_height, sum_height, res; + int i, res; struct wgt_widget *w; - struct wgt_rect textsz; + struct wgt_rect rect; if(!(w = calloc(1, sizeof *w))) { fprintf(stderr, "wgt_combo: failed to allocate widget structure\n"); @@ -285,21 +329,11 @@ struct wgt_widget *wgt_combo(struct wgt_window *win, const char **items, int num return 0; } - max_width = max_height = sum_height = 0; - - for(i=0; i max_width) max_width = textsz.width; - if(textsz.height > max_height) max_height = textsz.height; - sum_height += textsz.height; - } - if(width < 0) width = max_width * 3 / 2; - if(height < 0) height = sum_height * 2; - + combosize(win, items, num_items, width, height, &rect); - if(!(w->handle = CreateWindow("COMBOBOX", items[0], + if(!(w->handle = CreateWindow("COMBOBOX", items[0] ? items[0] : "", WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | CBS_HASSTRINGS, - x, y, width, height, win->handle, 0, GetModuleHandle(0), 0))) { + x, y, rect.width, rect.height, win->handle, 0, GetModuleHandle(0), 0))) { fprintf(stderr, "wgt_combo: failed to create window\n"); free(w); return 0; @@ -322,12 +356,14 @@ struct wgt_widget *wgt_combo(struct wgt_window *win, const char **items, int num if(sel < 0) sel = 0; if(sel >= num_items) sel = num_items - 1; - SendMessage(w->handle, CB_SETCURSEL, sel, 0); + if(sel >= 0) { + SendMessage(w->handle, CB_SETCURSEL, sel, 0); + } w->rect.x = x; w->rect.y = y; - w->rect.width = width; - w->rect.height = max_height; + w->rect.width = rect.width; + w->rect.height = rect.y; /* single line height for layout purposes */ w->cb_modify = modfunc; w->next = win->wlist; @@ -335,6 +371,60 @@ struct wgt_widget *wgt_combo(struct wgt_window *win, const char **items, int num return w; } +void wgt_checkbox_check(struct wgt_widget *w) +{ + SendMessage(w->handle, BM_SETCHECK, BST_CHECKED, 0); +} + +void wgt_checkbox_uncheck(struct wgt_widget *w) +{ + SendMessage(w->handle, BM_SETCHECK, BST_UNCHECKED, 0); +} + +int wgt_checkbox_checked(struct wgt_widget *w) +{ + return SendMessage(w->handle, BM_GETCHECK, 0, 0) == BST_CHECKED; +} + +int wgt_combo_setitems(struct wgt_widget *w, const char **items, int num_items) +{ + int i; + char **newit; + struct wgt_rect size; + + if(!(newit = malloc(num_items * sizeof *newit))) { + fprintf(stderr, "wgt_combo_setitems: failed to allocate item list\n"); + return -1; + } + for(i=0; i= 0) { + free(newit[i]); + } + free(newit); + return -1; + } + } + + for(i=0; inum_items; i++) { + free(w->itemlist[i]); + } + free(w->itemlist); + SendMessage(w->handle, CB_RESETCONTENT, 0, 0); + + w->itemlist = newit; + w->num_items = num_items; + + for(i=0; ihandle, CB_ADDSTRING, 0, (long)w->itemlist[i]); + } + + combosize(w->win, 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; +} + int wgt_combo_selected(struct wgt_widget *w) { int sel = SendMessage(w->handle, CB_GETCURSEL, 0, 0);