diff --git a/GNUmakefile b/GNUmakefile index 14571e6..a498f59 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -35,6 +35,8 @@ L_CFLAGS += -DUSE_X11 L_OBJS += src/x11.o L_LIBS += -lX11 +E_LIBS += -lm + SO = .so EXEC = else ifeq ($(WINDOWS),1) @@ -47,7 +49,7 @@ SO = .dll EXEC = .exe endif -EXAMPLES = examples/example$(EXEC) +EXAMPLES = examples/example$(EXEC) examples/rotate$(EXEC) .PHONY: all format clean lib examples diff --git a/examples/rotate.c b/examples/rotate.c new file mode 100644 index 0000000..0466a98 --- /dev/null +++ b/examples/rotate.c @@ -0,0 +1,93 @@ +/* $Id$ */ +/* this demo does not work well on windows */ + +#include + +#include + +#ifndef M_PI +#define M_PI 3.14159265 +#endif + +#define SIZE 100 + +MwWidget buttons[6]; +double deg = 0; + +int ww, wh; + +void handler(MwWidget w, void* user, void* client){ + int i; + double cdeg = deg; + + (void)w; + (void)user; + (void)client; + + for(i = 0; i < (int)(sizeof(buttons) / sizeof(buttons[0])); i++){ + double rad = cdeg / 180 * M_PI; + int x = ww / 2; + int y = wh / 2; + + x += cos(rad) * (ww / 2 - SIZE / 2); + y += sin(rad) * (wh / 2 - SIZE / 2); + + x -= SIZE / 2; + y -= SIZE / 2; + + MwVaApply(buttons[i], + MwNx, x, + MwNy, y, + NULL); + + cdeg += 360 / (sizeof(buttons) / sizeof(buttons[0])); + } + + deg += 120.0 / (1000.0 / MwWaitMS); +} + +void resize(MwWidget w, void* user, void* client){ + (void)w; + (void)user; + (void)client; + + ww = MwGetInteger(w, MwNwidth); + wh = MwGetInteger(w, MwNheight); +} + +int main(){ + MwWidget window = MwVaCreateWidget(MwWindowClass, "window", NULL, 0, 0, (ww = 500), (wh = 500), + MwNtitle, "rotate", + NULL); + int i; + + for(i = 0; i < (int)(sizeof(buttons) / sizeof(buttons[0])); i++){ + const char* color = ""; + char fgcolor[5]; + int j; + + if(i == 0) color = "#f66"; + if(i == 1) color = "#6f6"; + if(i == 2) color = "#ff6"; + if(i == 3) color = "#66f"; + if(i == 4) color = "#f6f"; + if(i == 5) color = "#6ff"; + + fgcolor[0] = '#'; + fgcolor[4] = 0; + for(j = 0; j < 3; j++){ + fgcolor[j + 1] = color[j + 1] == 'f' ? '6' : 'f'; + } + + buttons[i] = MwVaCreateWidget(MwButtonClass, "button", window, 0, 0, SIZE, SIZE, + MwNbackground, color, + MwNforeground, fgcolor, + MwNtext, "fancy", + NULL); + } + + MwAddUserHandler(window, MwNtickHandler, handler, NULL); + MwAddUserHandler(window, MwNresizeHandler, resize, NULL); + + MwLoop(window); +} diff --git a/include/Mw/Core.h b/include/Mw/Core.h index e9c6780..281af46 100644 --- a/include/Mw/Core.h +++ b/include/Mw/Core.h @@ -8,6 +8,8 @@ #define MwDispatch(x, y) \ if(x->widget_class != NULL && x->widget_class->y != NULL) x->widget_class->y(x) +#define MwWaitMS 5 + #ifdef __cplusplus extern "C" { #endif diff --git a/include/Mw/StringDefs.h b/include/Mw/StringDefs.h index 0dd7678..6cfd4dc 100644 --- a/include/Mw/StringDefs.h +++ b/include/Mw/StringDefs.h @@ -14,5 +14,6 @@ #define MwNactivateHandler "Cactivate" #define MwNresizeHandler "Cresize" +#define MwNtickHandler "Ctick" #endif diff --git a/src/core.c b/src/core.c index 31f8540..ff24157 100644 --- a/src/core.c +++ b/src/core.c @@ -135,7 +135,9 @@ int MwPending(MwWidget handle) { void MwLoop(MwWidget handle) { while(1) { MwStep(handle); - MwLLSleep(5); + + MwDispatchUserHandler(handle, MwNtickHandler, NULL); + MwLLSleep(MwWaitMS); } } diff --git a/src/gdi.c b/src/gdi.c index fb90895..9fce4d2 100644 --- a/src/gdi.c +++ b/src/gdi.c @@ -13,9 +13,27 @@ static LRESULT CALLBACK wndproc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) { if(msg == WM_PAINT) { PAINTSTRUCT ps; - u->ll->hDC = BeginPaint(hWnd, &ps); + RECT rc; + HBITMAP hbmp; + HDC dc, hbdc; + + GetClientRect(hWnd, &rc); + + dc = GetDC(hWnd); + hbmp = CreateCompatibleBitmap(dc, rc.right - rc.left, rc.bottom - rc.top); + hbdc = CreateCompatibleDC(dc); + SelectObject(hbdc, hbmp); + ReleaseDC(hWnd, dc); + + u->ll->hDC = hbdc; MwLLDispatch(u->ll, draw); + + dc = BeginPaint(hWnd, &ps); + StretchBlt(dc, 0, 0, rc.right - rc.left, rc.bottom - rc.top, hbdc, 0, 0, rc.right - rc.left, rc.bottom - rc.top, SRCCOPY); EndPaint(hWnd, &ps); + + DeleteDC(hbdc); + DeleteObject(hbmp); } else if(msg == WM_LBUTTONDOWN) { SetCapture(hWnd); MwLLDispatch(u->ll, down); @@ -127,7 +145,7 @@ MwLLColor MwLLAllocColor(MwLL handle, int r, int g, int b) { c->green = g; c->blue = b; - DeleteDC(dc); + ReleaseDC(handle->hWnd, dc); return c; }