libdockapp: Merge ChangeLog into NEWS (they were largely the same).
[dockapps.git] / libdockapp / examples / basic / basic.c
blob1ff6606735f8c326c4cb2471ad808626e99510c7
1 /*
2 * Copyright (c) 2002 Alban G. Hertroys
4 * Basic example of libDockapp usage
6 * This dockapp will draw a rectangle with a
7 * bouncing ball in it.
8 */
10 /* also includes Xlib, Xresources, XPM, stdlib and stdio */
11 #include <dockapp.h>
12 #include <time.h>
14 /* include the pixmap to use */
15 #include "ball_red.xpm"
17 #define SPEED 20
18 #define MAX_MOVE 4
21 * Prototypes for local functions
23 void drawRelief(Pixmap pixmap);
24 void moveBall(void);
25 void destroy(void);
28 * Global variables
30 Window ballWin;
31 DAShapedPixmap *ballPix;
34 * M A I N
37 int
38 main(int argc, char **argv)
40 unsigned int x = 1, y = 1;
41 Pixmap back;
42 DACallbacks eventCallbacks = {
43 destroy, /* destroy */
44 NULL, /* buttonPress */
45 NULL, /* buttonRelease */
46 NULL, /* motion (mouse) */
47 NULL, /* mouse enters window */
48 NULL, /* mouse leaves window */
49 moveBall /* timeout */
52 /* provide standard command-line options */
53 DAParseArguments(
54 argc, argv, /* Where the options come from */
55 NULL, 0, /* Our list with options - none as you can see */
56 "This is the help text for the basic example of how to "
57 "use libDockapp.\n",
58 "Basic example version 1.1");
60 /* Tell libdockapp what version we expect it to be (a date from
61 * NEWS should do).
63 DASetExpectedVersion(20020126);
65 DAOpenDisplay(
66 NULL /* default display */,
67 argc, argv /* needed by libdockapp */
69 DACreateIcon(
70 "daBasicExample" /* WM_CLASS hint; don't use chars in [.?*: ] */,
71 48, 48 /* geometry of dockapp internals */,
72 argc, argv /* needed by libdockapp */
75 /* The pixmap that makes up the background of the dockapp */
76 back = DAMakePixmap();
77 drawRelief(back);
78 DASetPixmap(back);
79 XFreePixmap(DADisplay, back);
81 /* A window(!) for the ball pixmap.
82 * Moving a window is a lot easier then erasing/copying the pixmap all
83 * the time.
85 * I use a DAShapedPixmap here, which contains all the information
86 * related to the pixmap: pixmap, mask and geometry.
88 ballPix = DAMakeShapedPixmapFromData(ball_red_xpm);
89 ballWin = XCreateSimpleWindow(DADisplay, DAWindow,
90 x, y,
91 /* Use the geometry of the shaped pixmap */
92 ballPix->geometry.width, ballPix->geometry.height,
93 0, 0, 0);
94 DASPSetPixmapForWindow(ballWin, ballPix);
96 /* Respond to destroy and timeout events (the ones not NULL in the
97 * eventCallbacks variable.
99 DASetCallbacks(&eventCallbacks);
101 /* Set the time for timeout events (in msec) */
102 DASetTimeout(SPEED);
104 /* Randomize movement variation.
105 * Run multiple versions of the dockapp simultaneously to see the effect
106 * best.
107 * (which function to use is set from the Imakefile)
109 #ifdef HAS_RANDOMDEV
110 srandomdev();
111 #else
112 srandom(time(NULL));
113 #endif
115 DAShow(); /* Show the dockapp window. */
117 /* Process events and keep the dockapp running */
118 DAEventLoop();
120 /* not reached */
121 exit(EXIT_SUCCESS);
125 void
126 drawRelief(Pixmap pixmap)
128 XGCValues gcv;
129 GC lightGrayGC, darkGrayGC;
131 /* GC's */
132 gcv.foreground = DAGetColor("Navy");
133 XChangeGC(DADisplay, DAClearGC, GCForeground, &gcv);
135 gcv.foreground = DAGetColor("lightGray");
136 gcv.graphics_exposures = False;
138 lightGrayGC = XCreateGC(DADisplay, DAWindow,
139 GCForeground | GCGraphicsExposures, &gcv);
141 gcv.foreground = DAGetColor("#222222");
142 darkGrayGC = XCreateGC(DADisplay, DAWindow,
143 GCForeground | GCGraphicsExposures, &gcv);
145 /* Drawing */
146 XFillRectangle(DADisplay, pixmap, DAClearGC, 1, 1, 46, 46);
148 XDrawLine(DADisplay, pixmap, darkGrayGC, 0, 0, 0, 46);
149 XDrawLine(DADisplay, pixmap, darkGrayGC, 1, 0, 47, 0);
151 XDrawLine(DADisplay, pixmap, lightGrayGC, 0, 47, 47, 47);
152 XDrawLine(DADisplay, pixmap, lightGrayGC, 47, 1, 47, 46);
154 /* Free the GC's, we don't use them anymore */
155 XFreeGC(DADisplay, lightGrayGC);
156 XFreeGC(DADisplay, darkGrayGC);
160 void
161 moveBall(void)
163 static int x = 1;
164 static int y = 1;
165 static int dx;
166 static int dy;
167 signed int var = random() % 3 - 1;
169 if (dx == 0)
170 dx = var;
172 if (dy == 0)
173 dy = var;
175 if (dx > MAX_MOVE)
176 dx = MAX_MOVE;
178 if (dy > MAX_MOVE)
179 dy = MAX_MOVE;
181 /* calculate new position */
182 x += dx;
183 y += dy;
185 if (x < 1) {
186 x = 1;
187 dx = -dx + var;
190 if (y < 1) {
191 y = 1;
192 dy = -dy + var;
195 if (x + ballPix->geometry.width > 46) {
196 x = 46 - ballPix->geometry.width;
197 dx = -dx + var;
200 if (y + ballPix->geometry.height > 46) {
201 y = 46 - ballPix->geometry.height;
202 dy = -dy + var;
205 XMoveWindow(DADisplay, ballWin, x, y);
209 void
210 destroy(void)
212 XFreePixmap(DADisplay, ballPix->pixmap);
213 XFreePixmap(DADisplay, ballPix->shape);
215 fprintf(stderr, "Destroyed!\n");
216 /* exit is done by libdockapp */