2 * Copyright (c) 2002 Alban G. Hertroys
4 * Basic example of libDockapp usage
6 * This dockapp will draw a rectangle with a
10 /* also includes Xlib, Xresources, XPM, stdlib and stdio */
13 /* include the pixmap to use */
14 #include "ball_red.xpm"
20 * Prototypes for local functions
22 void drawRelief(Pixmap pixmap
);
30 DAShapedPixmap
*ballPix
;
37 main(int argc
, char **argv
)
39 unsigned int x
= 1, y
= 1;
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 */
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 "
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);
65 NULL
/* default display */,
66 argc
, argv
/* needed by libdockapp */
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();
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
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
,
90 /* Use the geometry of the shaped pixmap */
91 ballPix
->geometry
.width
, ballPix
->geometry
.height
,
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) */
103 /* Randomize movement variation.
104 * Run multiple versions of the dockapp simultaneously to see the effect
106 * (which function to use is set from the Imakefile)
114 DAShow(); /* Show the dockapp window. */
116 /* Process events and keep the dockapp running */
125 drawRelief(Pixmap pixmap
)
128 GC lightGrayGC
, darkGrayGC
;
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
);
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
);
166 signed int var
= random() % 3 - 1;
180 /* calculate new position */
194 if (x
+ ballPix
->geometry
.width
> 46) {
195 x
= 46 - ballPix
->geometry
.width
;
199 if (y
+ ballPix
->geometry
.height
> 46) {
200 y
= 46 - ballPix
->geometry
.height
;
204 XMoveWindow(DADisplay
, ballWin
, x
, y
);
211 XFreePixmap(DADisplay
, ballPix
->pixmap
);
212 XFreePixmap(DADisplay
, ballPix
->shape
);
214 fprintf(stderr
, "Destroyed!\n");
215 /* exit is done by libdockapp */