libdockapp: Use consistent code formatting.
[dockapps.git] / libdockapp / examples / basic / basic.c
blob17d5264fd69b1702ca166cd35aabbc4e697550d8
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>
13 /* include the pixmap to use */
14 #include "ball_red.xpm"
16 #define SPEED 20
17 #define MAX_MOVE 4
20 * Prototypes for local functions
22 void drawRelief(Pixmap pixmap);
23 void moveBall(void);
24 void destroy(void);
27 * Global variables
29 Window ballWin;
30 DAShapedPixmap *ballPix;
33 * M A I N
36 int
37 main(int argc, char **argv)
39 unsigned int x = 1, y = 1;
40 Pixmap back;
41 DACallbacks eventCallbacks = {
42 destroy, /* destroy */
43 NULL, /* buttonPress */
44 NULL, /* buttonRelease */
45 NULL, /* motion (mouse) */
46 NULL, /* mouse enters window */
47 NULL, /* mouse leaves window */
48 moveBall /* timeout */
51 /* provide standard command-line options */
52 DAParseArguments(
53 argc, argv, /* Where the options come from */
54 NULL, 0, /* Our list with options - none as you can see */
55 "This is the help text for the basic example of how to "
56 "use libDockapp.\n",
57 "Basic example version 1.1");
59 /* Tell libdockapp what version we expect it to be (a date from the
60 * ChangeLog should do).
62 DASetExpectedVersion(20020126);
64 DAOpenDisplay(
65 NULL /* default display */,
66 argc, argv /* needed by libdockapp */
68 DACreateIcon(
69 "daBasicExample" /* WM_CLASS hint; don't use chars in [.?*: ] */,
70 48, 48 /* geometry of dockapp internals */,
71 argc, argv /* needed by libdockapp */
74 /* The pixmap that makes up the background of the dockapp */
75 back = DAMakePixmap();
76 drawRelief(back);
77 DASetPixmap(back);
78 XFreePixmap(DADisplay, back);
80 /* A window(!) for the ball pixmap.
81 * Moving a window is a lot easier then erasing/copying the pixmap all
82 * the time.
84 * I use a DAShapedPixmap here, which contains all the information
85 * related to the pixmap: pixmap, mask and geometry.
87 ballPix = DAMakeShapedPixmapFromData(ball_red_xpm);
88 ballWin = XCreateSimpleWindow(DADisplay, DAWindow,
89 x, y,
90 /* Use the geometry of the shaped pixmap */
91 ballPix->geometry.width, ballPix->geometry.height,
92 0, 0, 0);
93 DASPSetPixmapForWindow(ballWin, ballPix);
95 /* Respond to destroy and timeout events (the ones not NULL in the
96 * eventCallbacks variable.
98 DASetCallbacks(&eventCallbacks);
100 /* Set the time for timeout events (in msec) */
101 DASetTimeout(SPEED);
103 /* Randomize movement variation.
104 * Run multiple versions of the dockapp simultaneously to see the effect
105 * best.
106 * (which function to use is set from the Imakefile)
108 #ifdef HAS_RANDOMDEV
109 srandomdev();
110 #else
111 srandom(time(NULL));
112 #endif
114 DAShow(); /* Show the dockapp window. */
116 /* Process events and keep the dockapp running */
117 DAEventLoop();
119 /* not reached */
120 exit(EXIT_SUCCESS);
124 void
125 drawRelief(Pixmap pixmap)
127 XGCValues gcv;
128 GC lightGrayGC, darkGrayGC;
130 /* GC's */
131 gcv.foreground = DAGetColor("Navy");
132 XChangeGC(DADisplay, DAClearGC, GCForeground, &gcv);
134 gcv.foreground = DAGetColor("lightGray");
135 gcv.graphics_exposures = False;
137 lightGrayGC = XCreateGC(DADisplay, DAWindow,
138 GCForeground | GCGraphicsExposures, &gcv);
140 gcv.foreground = DAGetColor("#222222");
141 darkGrayGC = XCreateGC(DADisplay, DAWindow,
142 GCForeground | GCGraphicsExposures, &gcv);
144 /* Drawing */
145 XFillRectangle(DADisplay, pixmap, DAClearGC, 1, 1, 46, 46);
147 XDrawLine(DADisplay, pixmap, darkGrayGC, 0, 0, 0, 46);
148 XDrawLine(DADisplay, pixmap, darkGrayGC, 1, 0, 47, 0);
150 XDrawLine(DADisplay, pixmap, lightGrayGC, 0, 47, 47, 47);
151 XDrawLine(DADisplay, pixmap, lightGrayGC, 47, 1, 47, 46);
153 /* Free the GC's, we don't use them anymore */
154 XFreeGC(DADisplay, lightGrayGC);
155 XFreeGC(DADisplay, darkGrayGC);
159 void
160 moveBall(void)
162 static int x = 1;
163 static int y = 1;
164 static int dx;
165 static int dy;
166 signed int var = random() % 3 - 1;
168 if (dx == 0)
169 dx = var;
171 if (dy == 0)
172 dy = var;
174 if (dx > MAX_MOVE)
175 dx = MAX_MOVE;
177 if (dy > MAX_MOVE)
178 dy = MAX_MOVE;
180 /* calculate new position */
181 x += dx;
182 y += dy;
184 if (x < 1) {
185 x = 1;
186 dx = -dx + var;
189 if (y < 1) {
190 y = 1;
191 dy = -dy + var;
194 if (x + ballPix->geometry.width > 46) {
195 x = 46 - ballPix->geometry.width;
196 dx = -dx + var;
199 if (y + ballPix->geometry.height > 46) {
200 y = 46 - ballPix->geometry.height;
201 dy = -dy + var;
204 XMoveWindow(DADisplay, ballWin, x, y);
208 void
209 destroy(void)
211 XFreePixmap(DADisplay, ballPix->pixmap);
212 XFreePixmap(DADisplay, ballPix->shape);
214 fprintf(stderr, "Destroyed!\n");
215 /* exit is done by libdockapp */