From: John Tsiombikas Date: Wed, 8 Jan 2020 10:06:11 +0000 (+0200) Subject: widget disabling and autosize with minimum X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=instimg;a=commitdiff_plain;h=5ac430128e1fa0e70e1cb232bd5d756b7e8c6abf widget disabling and autosize with minimum --- diff --git a/src/main.c b/src/main.c index 68b4c98..b49c9e3 100644 --- a/src/main.c +++ b/src/main.c @@ -31,7 +31,7 @@ int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprevinst, char *cmdline, int show lb_instto = wgt_label(win, "Install to device:", 10, 10); x = wgt_xpos_after(lb_instto, WGT_AUTO); - cb_devs = wgt_combo(win, items, num_rawdev, 0, x, 10, WGT_AUTO, WGT_AUTO, + cb_devs = wgt_combo(win, items, num_rawdev, 0, x, 10, WGT_AUTOMIN(30), WGT_AUTO, onmodify); y = wgt_ypos_after(cb_devs, 16); @@ -44,6 +44,11 @@ int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hprevinst, char *cmdline, int show x = wgt_xpos_after(bn_inst, WGT_AUTO); bn_cancel = wgt_button(win, "Cancel", x, y, WGT_AUTO, WGT_AUTO, onclick); + if(!num_rawdev) { + wgt_disable_widget(cb_devs); + wgt_disable_widget(bn_inst); + } + while(GetMessage(&msg, 0, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); @@ -65,4 +70,4 @@ static void onmodify(struct wgt_widget *w) const char *selstr = wgt_get_combo_item(w, sel); MessageBox(0, selstr, "selected item", MB_OK); } -} \ No newline at end of file +} diff --git a/src/widgets.c b/src/widgets.c index 2c08856..707d515 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; @@ -285,7 +304,10 @@ struct wgt_widget *wgt_combo(struct wgt_window *win, const char **items, int num return 0; } - max_width = max_height = sum_height = 0; + 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; ihandle = 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))) { fprintf(stderr, "wgt_combo: failed to create window\n"); @@ -322,7 +344,9 @@ 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; diff --git a/src/widgets.h b/src/widgets.h index a81da8f..33cd490 100644 --- a/src/widgets.h +++ b/src/widgets.h @@ -3,7 +3,8 @@ #include -#define WGT_AUTO (-0x4242) +#define WGT_AUTO (-0x4242) +#define WGT_AUTOMIN(x) (-x) struct wgt_window; struct wgt_widget; @@ -19,6 +20,9 @@ struct wgt_window *wgt_window(const char *title, int width, int height); void wgt_destroy_window(struct wgt_window *win); void wgt_destroy_widget(struct wgt_widget *w); +void wgt_enable_widget(struct wgt_widget *w); +void wgt_disable_widget(struct wgt_widget *w); +int wgt_widget_enabled(struct wgt_widget *w); struct wgt_window *wgt_widget_window(struct wgt_widget *w); struct wgt_rect *wgt_widget_rect(struct wgt_widget *w, struct wgt_rect *rect); int wgt_xpos_after(struct wgt_widget *w, int pad); @@ -36,4 +40,4 @@ struct wgt_widget *wgt_combo(struct wgt_window *win, const char **items, int num int wgt_combo_selected(struct wgt_widget *w); const char *wgt_get_combo_item(struct wgt_widget *w, int idx); -#endif /* WIDGETS_H_ */ \ No newline at end of file +#endif /* WIDGETS_H_ */