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 */
11 #include <libdockapp/dockapp.h>
14 /* include the pixmap to use */
15 #include "ball_red.xpm"
21 * Prototypes for local functions
23 void drawRelief(Pixmap pixmap
);
31 DAShapedPixmap
*ballPix
;
38 main(int argc
, char **argv
)
40 unsigned int x
= 1, y
= 1;
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 */
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 "
58 "Basic example version 1.1");
60 /* Tell libdockapp what version we expect it to be (a date from
63 DASetExpectedVersion(20020126);
66 NULL
/* default display */,
67 argc
, argv
/* needed by libdockapp */
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();
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
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
,
91 /* Use the geometry of the shaped pixmap */
92 ballPix
->geometry
.width
, ballPix
->geometry
.height
,
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) */
104 /* Randomize movement variation.
105 * Run multiple versions of the dockapp simultaneously to see the effect
107 * (which function to use is set from the Imakefile)
115 DAShow(); /* Show the dockapp window. */
117 /* Process events and keep the dockapp running */
126 drawRelief(Pixmap pixmap
)
129 GC lightGrayGC
, darkGrayGC
;
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
);
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
);
167 signed int var
= random() % 3 - 1;
181 /* calculate new position */
195 if (x
+ ballPix
->geometry
.width
> 46) {
196 x
= 46 - ballPix
->geometry
.width
;
200 if (y
+ ballPix
->geometry
.height
> 46) {
201 y
= 46 - ballPix
->geometry
.height
;
205 XMoveWindow(DADisplay
, ballWin
, x
, y
);
212 XFreePixmap(DADisplay
, ballPix
->pixmap
);
213 XFreePixmap(DADisplay
, ballPix
->shape
);
215 fprintf(stderr
, "Destroyed!\n");
216 /* exit is done by libdockapp */