[go: up one dir, main page]

Menu

[1d9733]: / dragger.c  Maximize  Restore  History

Download this file

135 lines (122 with data), 2.7 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
/*
* Copyright 2010 Johan Veenhuizen
*/
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include "wind.h"
struct dragger {
struct listener listener;
void (*dragnotify)(void *, int, int, unsigned long, Time);
void *arg;
unsigned long counter;
Window window;
int x0;
int y0;
int x;
int y;
};
static void event(void *, XEvent *);
static void buttonpress(struct dragger *, XButtonEvent *);
static void motionnotify(struct dragger *, XMotionEvent *);
struct dragger *dcreate(Window parent, int x, int y,
int width, int height, int gravity, Cursor cursor,
void (*dragnotify)(void *, int, int, unsigned long, Time),
void *arg)
{
struct dragger *d = xmalloc(sizeof *d);
d->window = XCreateWindow(dpy, parent, x, y, width, height, 0,
CopyFromParent, InputOnly, CopyFromParent,
CWWinGravity | CWCursor,
&(XSetWindowAttributes){
.win_gravity = gravity,
.cursor = cursor });
d->listener.function = event;
d->listener.pointer = d;
setlistener(d->window, &d->listener);
d->counter = 0;
switch (gravity) {
case NorthWestGravity:
case WestGravity:
case SouthWestGravity:
d->x0 = 0;
break;
case NorthGravity:
case CenterGravity:
case SouthGravity:
d->x0 = width / 2;
break;
case NorthEastGravity:
case EastGravity:
case SouthEastGravity:
d->x0 = width - 1;
break;
default:
d->x0 = 0;
break;
}
switch (gravity) {
case NorthWestGravity:
case NorthGravity:
case NorthEastGravity:
d->y0 = 0;
break;
case WestGravity:
case CenterGravity:
case EastGravity:
d->y0 = height / 2;
break;
case SouthWestGravity:
case SouthGravity:
case SouthEastGravity:
d->y0 = height - 1;
break;
default:
d->y0 = 0;
break;
}
d->x = 0;
d->y = 0;
d->dragnotify = dragnotify;
d->arg = arg;
XGrabButton(dpy, Button1, AnyModifier, d->window, False,
Button1MotionMask, GrabModeAsync, GrabModeAsync,
None, cursor);
XMapWindow(dpy, d->window);
return d;
}
void ddestroy(struct dragger *d)
{
setlistener(d->window, NULL);
XDestroyWindow(dpy, d->window);
free(d);
}
static void event(void *self, XEvent *e)
{
switch (e->type) {
case MotionNotify:
motionnotify(self, &e->xmotion);
break;
case ButtonPress:
buttonpress(self, &e->xbutton);
break;
}
}
static void buttonpress(struct dragger *d, XButtonEvent *e)
{
d->counter = 0;
d->x = e->x - d->x0;
d->y = e->y - d->y0;
if (d->dragnotify != NULL)
d->dragnotify(d->arg,
e->x_root - d->x,
e->y_root - d->y,
d->counter++, e->time);
}
static void motionnotify(struct dragger *d, XMotionEvent *e)
{
if (d->dragnotify != NULL)
d->dragnotify(d->arg,
e->x_root - d->x, e->y_root - d->y,
d->counter++, e->time);
}