From edc57cf7a1d673b78f03b4064692797a7b6b720c Mon Sep 17 00:00:00 2001 From: kojima Date: Mon, 25 Sep 2000 19:21:45 +0000 Subject: [PATCH] added some new functions --- WINGs/ChangeLog | 2 + WINGs/WINGs.h | 5 ++ WINGs/WINGsP.h | 1 + WINGs/memory.c | 2 + WINGs/puzzle.c | 241 +++++++++++++++++++++++++++++++++++++++++++++++++++ WINGs/wappresource.c | 18 ++++ WINGs/widgets.c | 7 ++ WINGs/wpanel.c | 2 +- 8 files changed, 277 insertions(+), 1 deletion(-) create mode 100644 WINGs/puzzle.c diff --git a/WINGs/ChangeLog b/WINGs/ChangeLog index 905340a7..b9858adf 100644 --- a/WINGs/ChangeLog +++ b/WINGs/ChangeLog @@ -11,6 +11,8 @@ changes since wmaker 0.62.1: - added WMCreateTabViewItem() - added W_CreateUnmanagedTopView() - added wtokenize() +- added WMWidgetIsMapped() +- added WMSetApplicationIconWindow() - restructured the directory tree. Added Documentation, Examples and Tests subdirectories - removed WMArrayBag and reorganized WMTreeBag to be WMBag. diff --git a/WINGs/WINGs.h b/WINGs/WINGs.h index c06ea71a..eaaa679e 100644 --- a/WINGs/WINGs.h +++ b/WINGs/WINGs.h @@ -596,6 +596,8 @@ void WMSetApplicationIconImage(WMScreen *app, WMPixmap *icon); WMPixmap *WMGetApplicationIconImage(WMScreen *app); +void WMSetApplicationIconWindow(WMScreen *scr, Window window); + void WMSetFocusToWidget(WMWidget *widget); WMEventHook *WMHookEventHandler(WMEventHook *handler); @@ -788,6 +790,8 @@ void WMUnmapWidget(WMWidget *w); void WMMapWidget(WMWidget *w); +Bool WMWidgetIsMapped(WMWidget *w); + void WMRaiseWidget(WMWidget *w); void WMLowerWidget(WMWidget *w); @@ -1067,6 +1071,7 @@ WMListItem *WMGetListItem(WMList *lPtr, int row); WMArray *WMGetListItems(WMList *lPtr); + void WMRemoveListItem(WMList *lPtr, int row); void WMSelectListItem(WMList *lPtr, int row); diff --git a/WINGs/WINGsP.h b/WINGs/WINGsP.h index 3b35f5fa..099f3b75 100644 --- a/WINGs/WINGsP.h +++ b/WINGs/WINGsP.h @@ -137,6 +137,7 @@ typedef struct W_Screen { W_FocusInfo *focusInfo; struct W_Pixmap *applicationIcon; + Window applicationIconWindow; struct W_Window *windowList; /* list of windows in the app */ diff --git a/WINGs/memory.c b/WINGs/memory.c index 89f3ad5d..4956ad7b 100644 --- a/WINGs/memory.c +++ b/WINGs/memory.c @@ -80,6 +80,8 @@ void *wmalloc(size_t size) { void *tmp; + assert(size > 0); + #ifdef TEST_WITH_GC tmp = GC_malloc(size); #else diff --git a/WINGs/puzzle.c b/WINGs/puzzle.c new file mode 100644 index 00000000..6f9b6cae --- /dev/null +++ b/WINGs/puzzle.c @@ -0,0 +1,241 @@ + + + +#include +#include +#include + + + +#define MAX_SIZE 10*10 + + +WMWindow *win; +WMButton *Button[MAX_SIZE]; +char Map[MAX_SIZE]; +int Size = 4; +int MoveCount; + +#define MAP(x,y) Map[(x)+(y)*Size] + +int WinSize = 120; + + +Bool CheckWin(void) +{ + int i; + + for (i = 0; i < Size*Size-1; i++) { + if (Map[i] != i) + return False; + } + + return True; +} + + +void MoveButton(int button, int x, int y) +{ + WMMoveWidget(Button[button], x*(WinSize/Size), y*(WinSize/Size)); +} + + +Bool SlideButton(int button) +{ + int x, y, done = 0; + + /* locate the button */ + for (y = 0; y < Size; y++) { + for (x = 0; x < Size; x++) { + if (MAP(x,y) == button) { + done = 1; + break; + } + } + if (done) + break; + } + + if (x > 0 && MAP(x-1, y) < 0) { + MAP(x,y) = -1; + MoveButton(button, x-1, y); + MAP(x-1,y) = button; + } else if (x < Size-1 && MAP(x+1, y) < 0) { + MAP(x,y) = -1; + MoveButton(button, x+1, y); + MAP(x+1,y) = button; + } else if (y > 0 && MAP(x, y-1) < 0) { + MAP(x,y) = -1; + MoveButton(button, x, y-1); + MAP(x,y-1) = button; + } else if (y < Size-1 && MAP(x, y+1) < 0) { + MAP(x,y) = -1; + MoveButton(button, x, y+1); + MAP(x,y+1) = button; + } else { + return False; + } + return True; +} + + +#define SWAP(a,b) {int tmp; tmp=a; a=b; b=tmp;} + +void ResetGame(void) +{ + int i, x, y, ox, oy; + + + MoveCount = 0; + + for (i = 0; i < Size*Size-1; i++) { + Map[i] = i; + } + Map[i] = -1; + ox = x = Size-1; + oy = y = Size-1; + for (i = 0; i < 5; i++) { + int ok; + ok = 1; + switch (rand()%4) { + case 0: + if (x > 0) x--; else ok = 0; + break; + case 2: + if (x < Size-1) x++; else ok = 0; + break; + case 1: + if (y > 0) y--; else ok = 0; + break; + case 3: + if (y < Size-1) y++; else ok = 0; + break; + } + if (ok) { + MoveButton(MAP(x,y), ox, oy); + + SWAP(MAP(ox, oy), MAP(x, y)); + + while (XPending(WMScreenDisplay(WMWidgetScreen(win)))) { + XEvent ev; + WMNextEvent(WMScreenDisplay(WMWidgetScreen(win)), &ev); + WMHandleEvent(&ev); + } + ox = x; + oy = y; + } + } +} + + +void buttonClick(WMWidget *w, void *ptr) +{ + char buffer[300]; + + if (SlideButton((int)ptr)) { + MoveCount++; + + if (CheckWin()) { + sprintf(buffer, "You finished the game in %i moves.", MoveCount); + + if (WMRunAlertPanel(WMWidgetScreen(w), win, "You Won!", buffer, + "Wee!", "Gah! Lemme retry!", NULL) == WAPRDefault) { + exit(0); + } + + ResetGame(); + } + } +} + + +static void resizeObserver(void *self, WMNotification *notif) +{ + WMSize size = WMGetViewSize(WMWidgetView(win)); + int x, y; + + WinSize = size.width; + for (y = 0; y < Size; y++) { + for (x = 0; x < Size; x++) { + if (MAP(x,y) >= 0) { + WMResizeWidget(Button[(int)MAP(x,y)], + WinSize/Size, WinSize/Size); + WMMoveWidget(Button[(int)MAP(x,y)], + x*(WinSize/Size), y*(WinSize/Size)); + } + } + } + +} + + +int main(int argc, char **argv) +{ + Display *dpy; + WMScreen *scr; + int x, y, i; + + WMInitializeApplication("Puzzle", &argc, argv); + + + dpy = XOpenDisplay(""); + if (!dpy) { + printf("could not open display\n"); + exit(1); + } + + scr = WMCreateScreen(dpy, DefaultScreen(dpy)); + + win = WMCreateWindow(scr, "puzzle"); + WMResizeWidget(win, WinSize, WinSize); + WMSetWindowTitle(win, "zuPzel"); + WMSetWindowMinSize(win, 80, 80); + WMSetWindowAspectRatio(win, 2, 2, 2, 2); + WMSetWindowResizeIncrements(win, Size, Size); + WMSetViewNotifySizeChanges(WMWidgetView(win), True); + WMAddNotificationObserver(resizeObserver, NULL, + WMViewSizeDidChangeNotification, + WMWidgetView(win)); + + + + for (i = y = 0; y < Size && i < Size*Size-1; y++) { + for (x = 0; x < Size && i < Size*Size-1; x++) { + char buf[32]; + WMColor *color; + RColor col; + RHSVColor hsv; + + hsv.hue = i*360/(Size*Size-1); + hsv.saturation = 120; + hsv.value = 200; + + RHSVtoRGB(&hsv, &col); + + color = WMCreateRGBColor(scr, col.red<<8, col.green<<8, + col.blue<<8, False); + + MAP(x,y) = i; + Button[i] = WMCreateButton(win, WBTMomentaryLight); + WMSetWidgetBackgroundColor(Button[i], color); + WMReleaseColor(color); + WMSetButtonAction(Button[i], buttonClick, (void*)i); + WMResizeWidget(Button[i], WinSize/Size, WinSize/Size); + WMMoveWidget(Button[i], x*(WinSize/Size), y*(WinSize/Size)); + sprintf(buf, "%i", i+1); + WMSetButtonText(Button[i], buf); + WMSetButtonTextAlignment(Button[i], WACenter); + i++; + } + } + + WMMapSubwidgets(win); + WMMapWidget(win); + WMRealizeWidget(win); + + ResetGame(); + + WMScreenMainLoop(scr); + + return 0; +} diff --git a/WINGs/wappresource.c b/WINGs/wappresource.c index 244bb3fd..de413893 100644 --- a/WINGs/wappresource.c +++ b/WINGs/wappresource.c @@ -26,6 +26,24 @@ extern struct W_Application WMApplication; void +WMSetApplicationIconWindow(WMScreen *scr, Window window) +{ + scr->applicationIconWindow = window; + + if (scr->groupLeader) { + XWMHints *hints; + + hints = XGetWMHints(scr->display, scr->groupLeader); + hints->flags |= IconWindowHint; + hints->icon_window = window; + + XSetWMHints(scr->display, scr->groupLeader, hints); + XFree(hints); + } +} + + +void WMSetApplicationIconImage(WMScreen *scr, WMPixmap *icon) { if (scr->applicationIcon) diff --git a/WINGs/widgets.c b/WINGs/widgets.c index 91ef02ac..8762553e 100644 --- a/WINGs/widgets.c +++ b/WINGs/widgets.c @@ -933,6 +933,13 @@ WMUnmapWidget(WMWidget *w) } +Bool +WMWidgetIsMapped(WMWidget *w) +{ + return W_VIEW(w)->flags.mapped; +} + + void WMSetWidgetBackgroundColor(WMWidget *w, WMColor *color) { diff --git a/WINGs/wpanel.c b/WINGs/wpanel.c index d421e6a9..1e030905 100644 --- a/WINGs/wpanel.c +++ b/WINGs/wpanel.c @@ -71,7 +71,7 @@ WMRunAlertPanel(WMScreen *scrPtr, WMWindow *owner, } WMSetWindowInitialPosition(panel->win, px, py); } - + scrPtr->modalView = W_VIEW(panel->win); WMMapWidget(panel->win); -- 2.11.4.GIT