File: mouse.c

package info (click to toggle)
lwm 1.01-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 228 kB
  • ctags: 592
  • sloc: ansic: 1,884; makefile: 495; sh: 6; tcl: 6
file content (299 lines) | stat: -rw-r--r-- 6,298 bytes parent folder | download | duplicates (2)
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
#include <stdio.h>
#include <stdlib.h>

#include <X11/X.h>
#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

#include "lwm.h"

static int current_item;	/* Last known selected menu item. -1 if none. */

typedef struct menuitem menuitem;
struct menuitem {
	char * label;
	Client * client;
	menuitem * next;
};

static menuitem * hidden_menu = 0;

static char * chooseLabel(Client *);
static void getMenuDimensions(int *, int *, int *);

void
getMousePosition(int * x, int * y) {
	Window root, child;
	int t1, t2;
	unsigned int b;
	
	/* It doesn't matter which root window we give this call. */
	XQueryPointer(dpy, screens[0].root, &root, &child, x, y, &t1, &t2, &b);
	current_screen = getScreenFromRoot(root);
}

int
menu_whichitem(int x, int y) {
	int width;	/* Width of menu. */
	int height;	/* Height of each menu item. */
	int length;	/* Number of items on the menu. */
	
	getMenuDimensions(&width, &height, &length);
	
	/*
	 * Translate to popup window coordinates. We do this ourselves to avoid
	 * a round trip to the server.
	 */
	x -= start_x;
	y -= start_y;
	
	/*
	 * Are we outside the menu?
	 */
	if (x < 0 || x > width || y < 0 || y >= length * height)
		return -1;
	
	return y / height;
}

static void
getMenuDimensions(int *width, int *height, int *length) {
	int w;	/* Widest string so far. */
	int i;	/* Menu item. */
	
	menuitem *m = hidden_menu;
	
	w = 0;
	for (i = 0; m != 0; m = m->next, i++) {
		int tw = XTextWidth(popup_font, m->label, strlen(m->label)) + 4;
		if (tw > w) w = tw;
	}
	
	*width = w + border;
	*height = popup_font->ascent + popup_font->descent + 1;
	*length = i;
}

void
menuhit(XButtonEvent *e) {
	int width;	/* Width of menu. */
	int height;	/* Height of each menu item. */
	int length;	/* Number of menu items. */
	
	if (hidden_menu == 0)
		return;
	
	current_screen = getScreenFromRoot(e->root);
	
	getMenuDimensions(&width, &height, &length);
	
	/*
	 * Arrange for centre of first menu item to be under pointer.
	 */
	start_x = e->x - width / 2;
	start_y = e->y - height / 2;
	
	if (start_x < 0)
		start_x = 0;
	if (start_x + width > current_screen->display_width)
		start_x = current_screen->display_width - width;
	if (start_y < 0)
		start_y = 0;
	
	current_item = menu_whichitem(e->x, e->y);
	
	XMoveResizeWindow(dpy, current_screen->popup, start_x, start_y,
		width, length * height);
	XMapRaised(dpy, current_screen->popup);
	XChangeActivePointerGrab(dpy, ButtonMask | ButtonMotionMask |
		OwnerGrabButtonMask, None, CurrentTime);
	
	mode = wm_menu_up;
}

void 
hide(Client *c) {
	menuitem *newitem;

	if (c == 0)
		return;

	/* Create new menu item, and thread it on the menu. */
	newitem = (menuitem *) malloc(sizeof(menuitem));
	if (newitem == 0)
		return;
	newitem->label = chooseLabel(c);
	newitem->client = c;
	newitem->next = hidden_menu;
	hidden_menu = newitem;

	if (current == c)
		current = 0;

	/* Actually hide the window. */
	XUnmapWindow(dpy, c->parent);
	XUnmapWindow(dpy, c->window);

	c->hidden = True;

	/* If the window was the current window, it isn't any more... */
	setactive(c, 0, 0L);
	Client_SetState(c, IconicState);
}

void
unhide(int n, int map) {
	Client *c;
	menuitem *prev = 0;
	menuitem *m = hidden_menu;

	/* Find the nth client. */
	if (n < 0)
		return;

	while (n-- > 0 && m != 0) {
		prev = m;
		m = m->next;
	}

	if (m == 0)
		return;

	c = m->client;

	/* Remove the item from the menu, and dispose of it. */
	if (prev == 0) {
		hidden_menu = m->next;
	} else {
		prev->next = m->next;
	}
	free(m);

	c->hidden = False;

	/* Unhide it. */
	if (map) {
		XMapRaised(dpy, c->parent);
		XMapWindow(dpy, c->window);
		Client_SetState(c, NormalState);
	}
}

void
unhidec(Client *c, int map) {
	int i = 0;
	menuitem *m = hidden_menu;

	if (c == 0)
		return;

	/* My goodness, how the world sucks. */
	while (m != 0) {
		if (m->client == c) {
			unhide(i, map);
			return;
		}
		m = m->next;
		i++;
	}
}

void
update_menu(Client *c) {
	menuitem *m;

	if (c == 0)
		return;

	for (m = hidden_menu; m != 0; m = m->next) {
		if (m->client == c) {
			m->label = chooseLabel(c);
			return;
		}
	}
}

char *
chooseLabel(Client * c) {
	return (c->name != 0) ? c->name :
		((c->fallback_name != 0) ? c->fallback_name : "<unknown>");
}

void
menu_expose(void) {
	int i;		/* Menu item being drawn. */
	int width;	/* Width of each item. */
	int height;	/* Height of each item. */
	int length;	/* Number of menu items. */
	menuitem *m;

	getMenuDimensions(&width, &height, &length);

	/* Redraw the labels. */
	for (m = hidden_menu, i = 0; m != 0; m = m->next, i++) {
		int tx = (width - XTextWidth(popup_font, m->label,
			strlen(m->label))) / 2;
		int ty = i * height + popup_font->ascent + 1;

		XDrawString(dpy, current_screen->popup, current_screen->menu_gc,
			tx, ty, m->label, strlen(m->label));
	}

	/* Highlight current item if there is one. */
	if (current_item >= 0 && current_item < length)
		XFillRectangle(dpy, current_screen->popup, current_screen->menu_gc,
			0, current_item * height, width, height);
}

void
menu_motionnotify(XEvent* ev) {
	int x, y;		/* Event position. */
	int old;			/* Old menu position. */
	int width;		/* Width of menu. */
	int height;		/* Height of each menu item. */
	int length;		/* Number of menu items. */
	XButtonEvent *e = &ev->xbutton;
	
	x = e->x;
	y = e->y;
	
	getMenuDimensions(&width, &height, &length);
	
	old = current_item;
	current_item = menu_whichitem(x, y);
	
	if (current_item == old) return;
	
	/* Unhighlight the old position, if it was on the menu. */
	if (old >= 0 && old < length)
		XFillRectangle(dpy, current_screen->popup, current_screen->menu_gc,
			0, old * height, width, height);
	
	/* Highlight the new position, if it's on the menu. */
	if (current_item >= 0 && current_item < length)
		XFillRectangle(dpy, current_screen->popup, current_screen->menu_gc,
			0, current_item * height, width, height);
}

void
menu_buttonrelease(XEvent *ev) {
	int x,y;		/* Pointer position. */
	int n;		/* Menu item. */

	/*
	 * Work out which menu item the button was released over.
	 */
	x = ev->xbutton.x;
	y = ev->xbutton.y;
	n = menu_whichitem(x, y);

	/* Hide the menu until it's needed again. */
	XUnmapWindow(dpy, current_screen->popup);/*BUG?*/

	/* Do the menu thing (of unhiding windows). */
	unhide(n, 1);

	if (current)
		cmapfocus(current);
}