widget disabling and autosize with minimum
[instimg] / src / widgets.c
index 7796cba..707d515 100644 (file)
@@ -24,6 +24,8 @@ struct wgt_widget {
        char **itemlist;
        int num_items;
 
+       int disabled;
+
        wgt_callback cb_click;
        wgt_callback cb_modify;
 };
@@ -59,7 +61,7 @@ struct wgt_window *wgt_window(const char *title, int width, int height)
 
        memset(&wc, 0, sizeof wc);
        wc.hInstance = hinst;
-       wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
+       wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
        wc.hCursor = LoadCursor(0, IDC_ARROW);
        wc.hIcon = LoadIcon(0, IDI_APPLICATION);
        wc.lpszClassName = win->cname;
@@ -79,7 +81,7 @@ struct wgt_window *wgt_window(const char *title, int width, int height)
        win->dc = GetDC(win->handle);
        SetLastError(0);
        if(!SetWindowLong(win->handle, GWL_USERDATA, (long)win) && (err = GetLastError())) {
-               fprintf(stderr, "wgt_create_window: failed to store window user data (err: %d)\n", err);
+               fprintf(stderr, "wgt_create_window: failed to store window user data (err: %d)\n", (int)err);
                UnregisterClass(title, hinst);
                DestroyWindow(win->handle);
                return 0;
@@ -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;
@@ -232,6 +251,7 @@ struct wgt_widget *wgt_checkbox(struct wgt_window *win, const char *text, int on
 {
        struct wgt_widget *w;
        struct wgt_rect textsz;
+       int check_width;
 
        if(!(w = calloc(1, sizeof *w))) {
                fprintf(stderr, "wgt_checkbox: failed to allocate widget structure\n");
@@ -239,9 +259,11 @@ struct wgt_widget *wgt_checkbox(struct wgt_window *win, const char *text, int on
        }
        w->win = win;
 
+       check_width = GetSystemMetrics(SM_CXMENUCHECK);
+
        if(width < 0 || height < 0) {
                wgt_string_size(win, text, &textsz);
-               if(width < 0) width = textsz.width + 32;        /* XXX */
+               if(width < 0) width = textsz.width + check_width + 10;
                if(height < 0) height = textsz.height;
        }
 
@@ -257,7 +279,7 @@ struct wgt_widget *wgt_checkbox(struct wgt_window *win, const char *text, int on
        w->rect.width = width;
        w->rect.height = height;
 
-       w->cb_modify = modfunc;
+       w->cb_click = modfunc;  /* BN_CLICKED is sent for checkbox state changes */
        w->next = win->wlist;
        win->wlist = w;
        return w;
@@ -282,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; i<num_items; i++) {
                wgt_string_size(win, items[i], &textsz);
@@ -294,7 +319,7 @@ struct wgt_widget *wgt_combo(struct wgt_window *win, const char **items, int num
        if(height < 0) height = sum_height * 2;
 
 
-       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))) {
                fprintf(stderr, "wgt_combo: failed to create window\n");
@@ -319,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;
@@ -383,7 +410,7 @@ static LRESULT WINAPI event_handler(HWND wnd, unsigned int msg, WPARAM wparam, L
                default:
                        break;
                }
-       
+
        default:
                return DefWindowProc(wnd, msg, wparam, lparam);
        }
@@ -400,4 +427,4 @@ static struct wgt_widget *find_widget(struct wgt_window *win, HWND handle)
                w = w->next;
        }
        return 0;
-}
\ No newline at end of file
+}