X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=instimg;a=blobdiff_plain;f=src%2Fwidgets.c;h=1b97637dd054564d3629109a30bfbc1f918dad62;hp=7796cba3e957ed6debc17da3ea1acb50d92df7f6;hb=4f2abaf24d97f5d6fc253cca51ec87a8a95ab048;hpb=4f312c9361b1f4c63d7697e58503cd0fb8b05fff diff --git a/src/widgets.c b/src/widgets.c index 7796cba..1b97637 100644 --- a/src/widgets.c +++ b/src/widgets.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "widgets.h" struct wgt_window { @@ -12,6 +13,9 @@ struct wgt_window { HDC dc; char cname[32]; struct wgt_widget *wlist; + + int close_quit; + int (*cb_close)(struct wgt_window*); }; struct wgt_widget { @@ -24,6 +28,8 @@ struct wgt_widget { char **itemlist; int num_items; + int disabled; + wgt_callback cb_click; wgt_callback cb_modify; }; @@ -32,6 +38,28 @@ struct wgt_widget { static LRESULT WINAPI event_handler(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam); static struct wgt_widget *find_widget(struct wgt_window *win, HWND handle); + +void *wgt_window_handle(struct wgt_window *win) +{ + return win->handle; +} + +void *wgt_widget_handle(struct wgt_widget *w) +{ + return w->handle; +} + +unsigned int wgt_window_xid(struct wgt_window *win) +{ + return 0; +} + +unsigned int wgt_widget_xid(struct wgt_widget *w) +{ + return 0; +} + + struct wgt_window *wgt_window(const char *title, int width, int height) { struct wgt_window *win; @@ -41,11 +69,11 @@ struct wgt_window *wgt_window(const char *title, int width, int height) DWORD err; RECT rect; - if(!(win = malloc(sizeof *win))) { + if(!(win = calloc(1, sizeof *win))) { fprintf(stderr, "wgl_create_window: failed to allocate window structure\n"); return 0; } - win->wlist = 0; + win->close_quit = 1; /* by default quit when the window closes */ tlen = strlen(title); if(tlen > sizeof win->cname - 10) { @@ -59,7 +87,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; @@ -70,7 +98,7 @@ struct wgt_window *wgt_window(const char *title, int width, int height) return 0; } - if(!(win->handle = CreateWindow(win->cname, title, WS_OVERLAPPEDWINDOW, + if(!(win->handle = CreateWindow(win->cname, title, WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME, CW_USEDEFAULT, CW_USEDEFAULT, width, height, 0, 0, hinst, 0))) { fprintf(stderr, "wgt_create_window: failed to create window\n"); UnregisterClass(title, hinst); @@ -79,7 +107,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; @@ -105,10 +133,16 @@ void wgt_destroy_window(struct wgt_window *win) free(w); } - ReleaseDC(win->handle, win->dc); - DestroyWindow(win->handle); - UnregisterClass(win->cname, GetModuleHandle(0)); + if(win->handle) { + ReleaseDC(win->handle, win->dc); + DestroyWindow(win->handle); + UnregisterClass(win->cname, GetModuleHandle(0)); + } +} +void wgt_free_window(struct wgt_window *win) +{ + wgt_destroy_window(win); free(win); } @@ -118,6 +152,40 @@ void wgt_destroy_widget(struct wgt_widget *w) free(w->itemlist); } +void wgt_resize_window(struct wgt_window *win, int width, int height) +{ + MoveWindow(win->handle, win->rect.x, win->rect.y, width, height, TRUE); + win->rect.width = width; + win->rect.height = height; +} + +void wgt_quit_on_close(struct wgt_window *win, int quit) +{ + win->close_quit = quit; +} + +void wgt_close_action(struct wgt_window *win, int (*func)(struct wgt_window*)) +{ + win->cb_close = func; +} + +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; @@ -131,6 +199,20 @@ struct wgt_rect *wgt_widget_rect(struct wgt_widget *w, struct wgt_rect *rect) return &w->rect; } +void wgt_move_widget(struct wgt_widget *w, int x, int y) +{ + MoveWindow(w->handle, x, y, w->rect.width, w->rect.height, TRUE); + w->rect.x = x; + w->rect.y = y; +} + +void wgt_resize_widget(struct wgt_widget *w, int width, int height) +{ + MoveWindow(w->handle, w->rect.x, w->rect.y, width, height, TRUE); + w->rect.width = width; + w->rect.height = height; +} + int wgt_xpos_after(struct wgt_widget *w, int pad) { if(pad == WGT_AUTO) { @@ -232,6 +314,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 +322,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; } @@ -251,24 +336,59 @@ struct wgt_widget *wgt_checkbox(struct wgt_window *win, const char *text, int on free(w); return 0; } + SendMessage(w->handle, BM_SETCHECK, on ? BST_CHECKED : BST_UNCHECKED, 0); w->rect.x = x; w->rect.y = y; 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; } +static void combosize(struct wgt_window *win, HWND cbwnd, const char **items, int num_items, + int width, int height, struct wgt_rect *sz) +{ + 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; + + /* first get the height of the editbox */ + sum_height = SendMessage(cbwnd, CB_GETITEMHEIGHT, -1, 0); + sz->y = sum_height; + /* then add the height of the dropdown list items */ + /* XXX I can't figure out why, but it's not sufficient to show all items + * without some more height, so let's double it... + */ + sum_height *= 2; + sum_height += SendMessage(cbwnd, CB_GETITEMHEIGHT, 0, 0) * num_items; + sum_height += GetSystemMetrics(SM_CYEDGE) * 2; + + if(width < 0) { + int sbar_width; + for(i=0; i max_width) max_width = textsz.width; + } + sbar_width = GetSystemMetrics(SM_CXVSCROLL); + width = max_width + sbar_width + GetSystemMetrics(SM_CXEDGE) * 2; + } + if(height < 0) height = sum_height; + + sz->width = width; + 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; 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"); @@ -276,62 +396,141 @@ struct wgt_widget *wgt_combo(struct wgt_window *win, const char **items, int num } w->win = win; - if(!(w->itemlist = malloc(num_items * sizeof *w->itemlist))) { - fprintf(stderr, "wgt_combo: failed to allocate item list\n"); + if(!(w->handle = CreateWindow("COMBOBOX", items[0] ? items[0] : "", + WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | CBS_HASSTRINGS, + x, y, 16, 16, win->handle, 0, GetModuleHandle(0), 0))) { + fprintf(stderr, "wgt_combo: failed to create window\n"); free(w); return 0; } - max_width = max_height = sum_height = 0; + 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); - for(i=0; i max_width) max_width = textsz.width; - if(textsz.height > max_height) max_height = textsz.height; - sum_height += textsz.height; + if(sel < 0) sel = 0; + if(sel >= num_items) sel = num_items - 1; + if(sel >= 0) { + SendMessage(w->handle, CB_SETCURSEL, sel, 0); } - if(width < 0) width = max_width * 3 / 2; - if(height < 0) height = sum_height * 2; + w->cb_modify = modfunc; + w->next = win->wlist; + win->wlist = w; + return w; +} - if(!(w->handle = CreateWindow("COMBOBOX", 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"); - free(w); +struct wgt_widget *wgt_progbar(struct wgt_window *win, int x, int y, int width, int height, int p) +{ + struct wgt_widget *w; + INITCOMMONCONTROLSEX commctrl; + + commctrl.dwSize = sizeof commctrl; + commctrl.dwICC = ICC_PROGRESS_CLASS; + InitCommonControlsEx(&commctrl); + + if(!(w = calloc(1, sizeof *w))) { + fprintf(stderr, "wgt_progbar: failed to allocate widget structure\n"); return 0; } + w->win = win; - for(i=0; iitemlist[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"); - } + if(height < 0) { + height = GetSystemMetrics(SM_CYVSCROLL); } - w->num_items = num_items; - if(sel < 0) sel = 0; - if(sel >= num_items) sel = num_items - 1; - SendMessage(w->handle, CB_SETCURSEL, sel, 0); + if(!(w->handle = CreateWindow(PROGRESS_CLASS, 0, WS_CHILD | WS_VISIBLE, x, y, + width, height, win->handle, 0, GetModuleHandle(0), 0))) { + fprintf(stderr, "wgt_progbar: failed to create window\n"); + free(w); + return 0; + } + + /* + SendMessage(w->handle, PBM_SETRANGE, 0, MAKELPARAM(0, 100)); + SendMessage(w->handle, PBM_SETSTEP, 1, 0); + */ + SendMessage(w->handle, PBM_SETPOS, p, 0); w->rect.x = x; w->rect.y = y; w->rect.width = width; - w->rect.height = max_height; + w->rect.height = height; - w->cb_modify = modfunc; w->next = win->wlist; win->wlist = w; return w; } +void wgt_set_text(struct wgt_widget *w, const char *text) +{ + SendMessage(w->handle, WM_SETTEXT, 0, (long)text); + + w->rect.width = wgt_string_size(w->win, text, 0); + MoveWindow(w->handle, w->rect.x, w->rect.y, w->rect.width, w->rect.height, TRUE); +} + +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, 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); + + SendMessage(w->handle, CB_SETCURSEL, 0, 0); + return 0; +} + int wgt_combo_selected(struct wgt_widget *w) { int sel = SendMessage(w->handle, CB_GETCURSEL, 0, 0); @@ -347,6 +546,11 @@ const char *wgt_get_combo_item(struct wgt_widget *w, int idx) return w->itemlist[idx]; } +void wgt_set_progress(struct wgt_widget *w, int p) +{ + SendMessage(w->handle, PBM_SETPOS, p, 0); +} + static LRESULT WINAPI event_handler(HWND wnd, unsigned int msg, WPARAM wparam, LPARAM lparam) { int ncode; @@ -357,8 +561,22 @@ static LRESULT WINAPI event_handler(HWND wnd, unsigned int msg, WPARAM wparam, L switch(msg) { case WM_CLOSE: + if(!win->cb_close || win->cb_close(win)) { + if(win) { + int quit = win->close_quit; + wgt_destroy_window(win); + memset(win, 0, sizeof *win); + win->close_quit = quit; + } else { + DestroyWindow(wnd); + } + } + break; + case WM_DESTROY: - PostQuitMessage(0); + if(win && win->close_quit) { + PostQuitMessage(0); + } break; case WM_COMMAND: @@ -383,7 +601,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 +618,4 @@ static struct wgt_widget *find_widget(struct wgt_window *win, HWND handle) w = w->next; } return 0; -} \ No newline at end of file +}