[go: up one dir, main page]

Menu

[dbd395]: / button.c  Maximize  Restore  History

Download this file

172 lines (152 with data), 4.0 kB

  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
/*
* Copyright 2010 Johan Veenhuizen
*/
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include "wind.h"
struct button {
struct eventlistener listener;
void (*function)(struct client *, Time);
struct client *client;
struct bitmap *bitmap;
Pixmap pixmap;
int x;
int y;
int width;
int height;
Window window;
Bool pressed;
Bool entered;
};
static void update(struct button *);
static void buttonpress(struct button *, XButtonEvent *);
static void buttonrelease(struct button *, XButtonEvent *);
static void enternotify(struct button *, XCrossingEvent *);
static void leavenotify(struct button *, XCrossingEvent *);
static void expose(struct button *, XExposeEvent *);
static void unmapnotify(struct button *, XUnmapEvent *);
static void eventlistener(void *, XEvent *);
static void update(struct button *b)
{
Bool invert = b->pressed && b->entered;
XFillRectangle(dpy, b->pixmap, invert ? foreground : background,
0, 0, b->width, b->height);
if (!invert)
XDrawRectangle(dpy, b->pixmap, foreground,
0, 0, b->width - 1, b->height - 1);
if (!b->pressed && b->entered)
XDrawRectangle(dpy, b->pixmap, foreground,
1, 1, b->width - 3, b->height - 3);
drawbitmap(b->pixmap, invert ? background : foreground, b->bitmap,
(b->width - b->bitmap->width) / 2,
(b->height - b->bitmap->height) / 2);
XCopyArea(dpy, b->pixmap, b->window, foreground,
0, 0, b->width, b->height, 0, 0);
}
static void buttonpress(struct button *b, XButtonEvent *e)
{
if (e->button == Button1) {
b->pressed = True;
update(b);
}
}
static void buttonrelease(struct button *b, XButtonEvent *e)
{
if (e->button == Button1) {
if (b->pressed && b->entered)
b->function(b->client, e->time);
b->pressed = False;
update(b);
}
}
static void enternotify(struct button *b, XCrossingEvent *e)
{
b->entered = True;
update(b);
}
static void leavenotify(struct button *b, XCrossingEvent *e)
{
if (b->entered) {
b->entered = False;
update(b);
}
}
static void unmapnotify(struct button *b, XUnmapEvent *e)
{
if (b->pressed) {
b->pressed = False;
update(b);
}
}
static void expose(struct button *b, XExposeEvent *e)
{
XCopyArea(dpy, b->pixmap, b->window, foreground,
e->x, e->y, e->width, e->height, e->x, e->y);
}
static void eventlistener(void *self, XEvent *e)
{
struct button *b = self;
switch (e->type) {
case Expose:
expose(b, &e->xexpose);
break;
case EnterNotify:
enternotify(b, &e->xcrossing);
break;
case LeaveNotify:
leavenotify(b, &e->xcrossing);
break;
case ButtonPress:
buttonpress(b, &e->xbutton);
break;
case ButtonRelease:
buttonrelease(b, &e->xbutton);
break;
case UnmapNotify:
unmapnotify(b, &e->xunmap);
break;
}
}
struct button *bcreate(void (*function)(struct client *, Time),
struct client *client, struct bitmap *bitmap,
Window parent, int x, int y, int width,
int height, int gravity)
{
struct button *b = xmalloc(sizeof *b);
b->function = function;
b->client = client;
b->bitmap = bitmap;
b->x = x;
b->y = y;
b->width = width;
b->height = height;
b->pixmap = XCreatePixmap(dpy, root, width, height,
DefaultDepth(dpy, scr));
b->pressed = False;
b->entered = False;
b->window = XCreateWindow(dpy, parent, b->x, b->y, b->width, b->height,
0, CopyFromParent, InputOutput, CopyFromParent,
CWOverrideRedirect | CWBackPixel | CWWinGravity,
&(XSetWindowAttributes){
.override_redirect = True,
.background_pixel = backgroundpixel,
.win_gravity = gravity });
b->listener.function = eventlistener;
b->listener.pointer = b;
seteventlistener(b->window, &b->listener);
XSelectInput(dpy, b->window,
ButtonPressMask | ButtonReleaseMask |
EnterWindowMask | LeaveWindowMask |
StructureNotifyMask | ExposureMask);
update(b);
XMapWindow(dpy, b->window);
return b;
}
void bdestroy(struct button *b)
{
seteventlistener(b->window, NULL);
XFreePixmap(dpy, b->pixmap);
XDestroyWindow(dpy, b->window);
free(b);
}