File: rectangles.c

package info (click to toggle)
libdockapp 1%3A0.7.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,732 kB
  • sloc: sh: 3,688; ansic: 2,175; makefile: 104
file content (668 lines) | stat: -rw-r--r-- 16,172 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/*
 * Copyright (c) 2002 Alban G. Hertroys
 *
 * libDockapp example - Usage of action rectangles
 *
 * Some clickable and draggable areas for you to play
 * with.
 *
 * There's a bit much in here...
 */

/* The cursor font - stamdard cursor glyphs. */
#include <X11/cursorfont.h>

/* Required because we don't use a pixmap for the shape (using a DASetShape
 * variation). Instead we use the XLib call "XShapeCombineRegion".
 * Window shapes are an extension (since X11R5).
 */
#include <X11/extensions/shape.h>

#include <libdockapp/dockapp.h>

/* already includes Xlib, Xresources, XPM, stdlib and stdio */

/*
 * Type definitions
 */

/* I like to keep my graphic contexts (GCs) together */
struct Colors {
	GC white;       /* foreground color from X-resource, or default */
	GC black;       /* background color, idem */
	GC lightGray;   /* Some GC's we'll have to define for colors */
	GC darkGray;
	GC slider;      /* draw-color when not highlighted,
			 * dark-color when highlighted
			 */
	GC sliderLight; /* draw-color when highlighted */
	GC sliderDark;  /* dark-color when not highlighted */
};


/*
 * Global variables
 */
Pixmap pixmap;          /* pixmap pixmap */
DARect *buttonDown = NULL;
struct Colors *colors;  /* our colors */
DAActionRect **actionRects;
float sliderPos = 0.7;
int mouseIn = 0;
Cursor pointer;

/*
 * Prototypes for local functions
 */
struct Colors *setGCs(Drawable d);
unsigned long adjustColor(unsigned long color, signed int adjustment);

/* drawing routines */
void createBtn(DARect rect);
void drawRaisedFrame(DARect rect);
void drawSunkenFrame(DARect rect);
void createSquare(DARect rect);
void drawSquare(DARect rect, int button);
void createSlider(DARect rect);
void drawSlider(DARect rect);
void setPointerColor(GC);

/* utility functions */
DAActionRect setRectAction(DARect rect, DARectCallback action);


/* event handlers functions */
void destroy(void);
void buttonPress(int button, int state, int x, int y);
void buttonRelease(int button, int state, int x, int y);
void mouseMove(int x, int y);
void mouseEnter(void);
void mouseLeave(void);

/* what to do for a specific event for every 'item' in the dockapp */
/*	Button that can be pressed "down" and jumps back "up" again */
void btnDown(int x, int y, DARect rect, void *data);
void btnUp(int x, int y, DARect rect, void *data);
void btnLeave(int x, int y, DARect rect, void *data);

/*	Square that tells which button was pressed (number) */
void squareDown(int x, int y, DARect rect, void *data);

/*	A draggable slider that highlights when the mouse is over it */
void sliderDown(int x, int y, DARect rect, void *data);
void sliderUp(int x, int y, DARect rect, void *data);
void sliderMove(int x, int y, DARect rect, void *data);
void sliderEnter(int x, int y, DARect rect, void *data);
void sliderLeave(int x, int y, DARect rect, void *data);


/*
 * M A I N
 */

int
main(int argc, char **argv)
{
	/* define the event handlers for the events */
	DACallbacks eventCallbacks = {
		destroy, /* destroy */
		buttonPress, /* buttonPress */
		buttonRelease, /* buttonRelease */
		mouseMove, /* motion (mouse) */
		mouseEnter, /* mouse enters window */
		mouseLeave, /* mouse leaves window */
		NULL    /* timeout */
	};

	/* define regions (x, y, width, height) that need event-handling */
	Region clipRegion = XCreateRegion();

	DARect btn = {0, 0, 16, 16},
	       square = {0, 25, 22, 22},
	       slider = {24, 0, 23, 48};

	/* define what to do if an event occurs in a rectangle */
	DAActionRect *buttonPressRects, *buttonReleaseRects,
	*mouseMoveRects, *mouseEnterRects, *mouseLeaveRects;

	buttonPressRects = malloc(3 * sizeof(DAActionRect));
	buttonPressRects[0] = setRectAction(btn, btnDown);
	buttonPressRects[1] = setRectAction(square, squareDown);
	buttonPressRects[2] = setRectAction(slider, sliderDown);

	buttonReleaseRects = malloc(2 * sizeof(DAActionRect));
	buttonReleaseRects[0] = setRectAction(btn, btnUp);
	buttonReleaseRects[1] = setRectAction(slider, sliderUp);

	mouseMoveRects = malloc(sizeof(DAActionRect));
	mouseMoveRects[0] = setRectAction(slider, sliderMove);

	mouseEnterRects = malloc(sizeof(DAActionRect));
	mouseEnterRects[0] = setRectAction(slider, sliderEnter);

	mouseLeaveRects = malloc(2 * sizeof(DAActionRect));
	mouseLeaveRects[0] = setRectAction(btn, btnLeave);
	mouseLeaveRects[1] = setRectAction(slider, sliderLeave);


	/* XXX: make action rectangles available outside main()
	 * ...libDockapp should be able to do this... (reminder)
	 */
	actionRects = malloc(6 * sizeof(DAActionRect *));
	actionRects[0] = buttonPressRects;
	actionRects[1] = buttonReleaseRects;
	actionRects[2] = mouseMoveRects;
	actionRects[3] = mouseEnterRects;
	actionRects[4] = mouseLeaveRects;
	actionRects[5] = NULL;

	/* provide standard command-line options */
	DAParseArguments(
		argc, argv, /* Where the options come from */
		NULL, 0, /* Our list with options - none as you can see */
		"This is the help text for the rectangle example of how to "
		"use libDockapp.\n",
		"Rectangle example version 1.0");

	/* Tell libdockapp what version we expect it to be, so that you can use
	 * older programs with newer versions of libdockapp with less risc for
	 * compatibility problems.
	 */
	DASetExpectedVersion(20030126);

	/* Initialize a dockapp */
	DAInitialize(
		"",             /* Use default display */
		"daRectangleExample",   /* WM_CLASS hint; don't use chars in [.?*: ] */
		48, 48,         /* geometry of dockapp internals */
		argc, argv      /* (needed internally) */
		);

	/* Create a pixmap to draw on, and to display */
	pixmap = DAMakePixmap(); /* size == dockapp geometry (48,48) */

	colors = setGCs(pixmap);

	XFillRectangle(DADisplay, pixmap, DAClearGC, 0, 0, 48, 48);
	XClearWindow(DADisplay, DAWindow);

	/* Make a "Region" from the shapes we have */
	XUnionRectWithRegion(&btn, clipRegion, clipRegion);
	XUnionRectWithRegion(&square, clipRegion, clipRegion);
	XUnionRectWithRegion(&slider, clipRegion, clipRegion);

	/* Make this region a window shape mask */
	XShapeCombineRegion(DADisplay, DAWindow, ShapeBounding,
			    0, 0, clipRegion, ShapeSet);

	/* We don't need the region anymore (it is copied by XShapeCombineRegion).
	 * XXX: That's not certain, it is not documented. XSetRegion does so,
	 * though, so it is a likely assumption that it does copy.
	 */
	XDestroyRegion(clipRegion);

	/* The cursor we want to use.
	 * Specify 'None' for the default,
	 * or one from X11/cursorfont.h
	 */
	pointer = XCreateFontCursor(DADisplay, XC_based_arrow_up);
	XDefineCursor(DADisplay, DAWindow, pointer);

	/* a square with an image that changes when clicked (A button). */
	createBtn(btn);

	/* a square that shows the number of the mouse-button pressed on click. */
	createSquare(square);

	/* a slider a using two dashed line GC's. */
	createSlider(slider);

	/* Tell libdockapp this is the pixmap that we want to show */
	DASetPixmap(pixmap);

	/* Process events every 100ms */
	DASetTimeout(100);

	/* set event callbacks */
	DASetCallbacks(&eventCallbacks);

	/* Display the pixmap we said it to show */
	DAShow();

	/* Process events and keep the dockapp running */
	DAEventLoop();

	return 0;
}


/* Create our GC's to draw colored lines and such */
struct Colors *
setGCs(Drawable d)
{
	struct Colors *colors;
	XGCValues gcv;
	unsigned long origColor;
	char dashList[2] = {3, 1};

	colors = malloc(sizeof(struct Colors));
	if (colors == NULL)
		return NULL;

	/* Get the GC-values of the default GC */
	XGetGCValues(DADisplay, DAGC,
		     GCForeground | GCBackground | GCGraphicsExposures, &gcv);

	origColor = gcv.foreground;

	/* don't send expose events */
	gcv.graphics_exposures = False;

	/* GC for white color */
	gcv.foreground = WhitePixel(DADisplay, DefaultScreen(DADisplay));
	colors->white = XCreateGC(DADisplay, d,
				  GCForeground | GCGraphicsExposures, &gcv);

	/* GC for dark blue color */
#if 1
	gcv.foreground = DAGetColor("navy");
#else
	gcv.foreground = 0;
#endif
	colors->black = XCreateGC(DADisplay, d,
				  GCForeground | GCGraphicsExposures, &gcv);

	/* GC for light borders */
	gcv.foreground = DAGetColor("lightGray");
	colors->lightGray = XCreateGC(DADisplay, d,
				      GCForeground | GCGraphicsExposures, &gcv);

	/* GC for dark borders (note re-use of gcv-values) */
	gcv.foreground = DAGetColor("#222222");
	colors->darkGray =  XCreateGC(DADisplay, d,
				      GCForeground | GCGraphicsExposures, &gcv);

	/* GC for the un-/highlighted colors and dashed line of the slider */
	gcv.foreground = origColor;
	gcv.line_width = 9;
	gcv.line_style = LineOnOffDash;

	colors->slider = XCreateGC(DADisplay, d,
				   GCForeground | GCBackground | GCGraphicsExposures |
				   GCLineWidth | GCLineStyle, &gcv);

	XSetDashes(DADisplay, colors->slider, 1, dashList, 2);

	/* light slider GC */
	gcv.foreground = adjustColor(origColor, +0x40);

	colors->sliderLight = XCreateGC(DADisplay, d,
					GCForeground | GCBackground | GCGraphicsExposures |
					GCLineWidth | GCLineStyle, &gcv);

	XSetDashes(DADisplay, colors->sliderLight, 1, dashList, 2);

	/* dark slider GC */
	gcv.foreground = adjustColor(origColor, -0x40);

	colors->sliderDark = XCreateGC(DADisplay, d,
				       GCForeground | GCBackground | GCGraphicsExposures |
				       GCLineWidth | GCLineStyle, &gcv);

	XSetDashes(DADisplay, colors->sliderDark, 1, dashList, 2);

	return colors;
}


/* Make a (GC) color lighter or darker */
unsigned long
adjustColor(unsigned long color, signed int adjustment)
{
	signed long r, g, b;

	r = color >> 16;
	g = (color - (r << 16)) >> 8;
	b = (color - (g << 8) - (r << 16));

	r += adjustment;
	g += adjustment;
	b += adjustment;

	if (r > 0xff)
		r = 0xff;

	if (g > 0xff)
		g = 0xff;

	if (b > 0xff)
		b = 0xff;

	if (r < 0)
		r = 0;

	if (g < 0)
		g = 0;

	if (b < 0)
		b = 0;

	return ((unsigned short)r << 16) +
	       ((unsigned short)g << 8) +
	       (unsigned short)b;
}


void
setPointerColor(GC color)
{
	XGCValues gcv;
	XColor fcolor, bcolor;

	XGetGCValues(DADisplay, color, GCForeground, &gcv);

	fcolor.pixel = gcv.foreground;
	fcolor.flags = DoRed | DoGreen | DoBlue;

	bcolor.red = 0;
	bcolor.green = 0;
	bcolor.blue = 0;

	XRecolorCursor(DADisplay, pointer, &fcolor, &bcolor);
}


/* event handlers functions */
void destroy(void)
{
}

void buttonPress(int button, int state, int x, int y)
{
	int *data = malloc(sizeof(int *));

	*data = button;

	DAProcessActionRects(x, y, actionRects[0], 3, (void *)data);

	free(data);
}

void buttonRelease(int button, int state, int x, int y)
{
	DAProcessActionRects(x, y, actionRects[1], 2, NULL);

}

void
mouseMove(int x, int y)
{
	DAProcessActionRects(x, y, actionRects[2], 1, NULL);
}

void
mouseEnter(void)
{
	mouseIn = 1;

	drawSlider(actionRects[3][0].rect);
}


void
mouseLeave(void)
{
	mouseIn = 0;

	/* mouse pointer left the dockapp window */
	DAProcessActionRects(0, 0, actionRects[4], 2, NULL);

	/* if the button is still depressed, make it go up again. */
/* TODO: Use data in actionRects[4] here instead of below check */
	if (buttonDown != NULL)
		btnUp(0, 0, *buttonDown, NULL);

	drawSlider(actionRects[4][1].rect);
}

/* what to do for a specific event for every 'item' in the dockapp */
/*	Button that can be pressed "down" and jumps back "up" again */
void
btnDown(int x, int y, DARect rect, void *data)
{
	buttonDown = &rect;
	drawSunkenFrame(rect);
}


void
btnUp(int x, int y, DARect rect, void *data)
{
	buttonDown = NULL;
	drawRaisedFrame(rect);
}


void
btnLeave(int x, int y, DARect rect, void *data)
{
	if (buttonDown == NULL)
		return;

	drawRaisedFrame(rect);
}


/*	Square that tells which button was pressed (number) */
void
squareDown(int x, int y, DARect rect, void *data)
{
	int button;

	if (data) {
		int *tmp = (int *)data;

		button = *tmp;
	} else
		button = 0;

	drawSquare(rect, button);
}


/*	A draggable slider that highlights when the mouse is over it */
void
sliderDown(int x, int y, DARect rect, void *data)
{
	buttonDown = &rect;
	setPointerColor(colors->sliderDark);
}


void
sliderUp(int x, int y, DARect rect, void *data)
{
	buttonDown = NULL;
	setPointerColor(colors->black);
}


void
sliderMove(int x, int y, DARect rect, void *data)
{
	if (buttonDown == NULL /* ||
				  rect.x != buttonDown->x ||
				  rect.y != buttonDown->y ||
				  rect.width != buttonDown->width ||
				  rect.height != buttonDown->height */)
		return;

	sliderPos = (float)(rect.height - y) / (float)rect.height;
	if (sliderPos > 1.0)
		sliderPos = 1.0;

	if (sliderPos < 0.0)
		sliderPos = 0.0;

	drawSlider(rect);
}

void sliderEnter(int x, int y, DARect rect, void *data)
{
}
void sliderLeave(int x, int y, DARect rect, void *data)
{
}



/*
 * Drawing functions
 */
void
createBtn(DARect rect)
{
	/* fill square excluding borders */
	XFillRectangle(DADisplay, pixmap, colors->lightGray,
		       rect.x + 1, rect.y + 1, rect.width - 2, rect.height - 2);

	drawRaisedFrame(rect);
}


void
drawRaisedFrame(DARect rect)
{
	/* left border */
	XDrawLine(DADisplay, pixmap, colors->white,
		  rect.x, rect.y, rect.x, rect.y + rect.height - 2);
	/* top border */
	XDrawLine(DADisplay, pixmap, colors->white,
		  rect.x + 1, rect.y, rect.width - 1, rect.y);
	/* bottom border */
	XDrawLine(DADisplay, pixmap, colors->darkGray,
		  rect.x,                 rect.y + rect.height - 1,
		  rect.x + rect.width - 1, rect.y + rect.height - 1);
	/* right border */
	XDrawLine(DADisplay, pixmap, colors->darkGray,
		  rect.x + rect.width - 1, rect.y + 1,
		  rect.x + rect.width - 1, rect.y + rect.height - 2);

	DASetPixmap(pixmap);
}


void
drawSunkenFrame(DARect rect)
{
	/* left border */
	XDrawLine(DADisplay, pixmap, colors->darkGray,
		  rect.x, rect.y, rect.x, rect.y + rect.height - 2);
	/* top border */
	XDrawLine(DADisplay, pixmap, colors->darkGray,
		  rect.x + 1, rect.y, rect.width - 1, rect.y);
	/* bottom border */
	XDrawLine(DADisplay, pixmap, colors->white,
		  rect.x,                 rect.y + rect.height - 1,
		  rect.x + rect.width - 1, rect.y + rect.height - 1);
	/* right border */
	XDrawLine(DADisplay, pixmap, colors->white,
		  rect.x + rect.width - 1, rect.y + 1,
		  rect.x + rect.width - 1, rect.y + rect.height - 2);

	DASetPixmap(pixmap);
}


void
createSquare(DARect rect)
{
	/* fill square excluding borders */
	XFillRectangle(DADisplay, pixmap, colors->black,
		       rect.x + 1, rect.y + 1, rect.width - 2, rect.height - 2);

	XDrawRectangle(DADisplay, pixmap, colors->lightGray,
		       rect.x, rect.y, rect.width - 1, rect.height - 1);

	drawSquare(rect, 0);
}


void
drawSquare(DARect rect, int button)
{
	char label[3];

	XFillRectangle(DADisplay, pixmap, colors->black,
		       rect.x + 1, rect.y + 1, rect.width - 2, rect.height - 2);

	snprintf(label, 3, "%2d", button);
	XDrawString(DADisplay, pixmap, colors->white,
		    rect.x + 3, rect.y + rect.height - 5, label, 2);

	DASetPixmap(pixmap);
}


void
createSlider(DARect rect)
{
	/* fill square excluding borders */
	XFillRectangle(DADisplay, pixmap, colors->black,
		       rect.x + 1, rect.y + 1, rect.width - 2, rect.height - 2);

	drawSunkenFrame(rect);

	drawSlider(rect);
}


void
drawSlider(DARect rect)
{
	GC highColor, lowColor; /* determine colors to use */

	if (mouseIn) {
		highColor = colors->sliderLight;
		lowColor = colors->slider;
	} else {
		highColor = colors->slider;
		lowColor = colors->sliderDark;
	}

	/* Draw two lines from bottom to sliderPos fraction of height */
	if (sliderPos < 1.0) {
		XDrawLine(DADisplay, pixmap, highColor,
			  rect.x + 6, rect.y + rect.height - 2,
			  rect.x + 6, rect.y + (1.0 - sliderPos) * (rect.height - 2));

		XDrawLine(DADisplay, pixmap, highColor,
			  rect.x + 17, rect.y + rect.height - 2,
			  rect.x + 17, rect.y + (1.0 - sliderPos) * (rect.height - 2));
	}

	if (sliderPos > 0.0) {
		XDrawLine(DADisplay, pixmap, lowColor,
			  rect.x + 6, rect.y + 1,
			  rect.x + 6, rect.y + (1.0 - sliderPos) * (rect.height - 2));

		XDrawLine(DADisplay, pixmap, lowColor,
			  rect.x + 17, rect.y + 1,
			  rect.x + 17, rect.y + (1.0 - sliderPos) * (rect.height - 2));
	}

	DASetPixmap(pixmap);
}


DAActionRect
setRectAction(DARect rect, DARectCallback action)
{
	DAActionRect ar;

	ar.rect     = rect;
	ar.action   = action;

	return ar;
}