File: mouse.c

package info (click to toggle)
lwm 1.2.2-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 396 kB
  • sloc: ansic: 5,013; makefile: 59
file content (317 lines) | stat: -rw-r--r-- 7,388 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
/*
 * lwm, a window manager for X11
 * Copyright (C) 1997-2003 Elliott Hughes, James Carter
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#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"
#include "ewmh.h"

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

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

static menuitem * hidden_menu = 0;

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 = titleWidth(popup_font_set, m->client) + 4;
		if (tw > w) w = tw;
	}
	
	*width = w + border;
	*height = popupHeight();
	*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;

	Client_ResetAllCursors();
	
	current_screen = getScreenFromRoot(e->root);
	
	getMenuDimensions(&width, &height, &length);
	
	/*
	 * Arrange for centre of first menu item to be under pointer,
	 * unless that would put the menu offscreen.
	 */
	start_x = e->x - width / 2;
	start_y = e->y - height / 2;
	
	if (start_x + width > current_screen->display_width)
		start_x = current_screen->display_width - width;
	if (start_x < 0)
		start_x = 0;
	if (start_y + (height * length) > current_screen->display_height)
		start_y = current_screen->display_height - (height * length);
	if (start_y < 0)
		start_y = 0;

	
	current_item = menu_whichitem(e->x_root, e->y_root);
	
	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->client = c;
	newitem->next = hidden_menu;
	hidden_menu = newitem;

	/* 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... */
	if (c == current) Client_Focus(NULL, CurrentTime);
	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) {
		XMapWindow(dpy, c->parent);
		XMapWindow(dpy, c->window);
		Client_Raise(c);
		Client_SetState(c, NormalState);

		if (focus_mode == focus_click) {
			/* it feels right that the unhidden window gets focus*/
			Client_Focus(c, CurrentTime);
		}
	}
}

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
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 - titleWidth(popup_font_set, m->client)) / 2;
		int ty = i * height + ascent(popup_font_set_ext);
		char *name;
		int namelen;

		if (m->client->menu_name == NULL) {
			name = m->client->name;
			namelen = m->client->namelen;
		} else {
			name = m->client->menu_name;
			namelen = m->client->menu_namelen;
		}

#ifdef X_HAVE_UTF8_STRING
		if (m->client->name_utf8 == True)
			Xutf8DrawString(dpy, m->client->screen->popup,
				popup_font_set, 
				current_screen->menu_gc, tx, ty,
				name, namelen);
		else
#endif
			XmbDrawString(dpy, m->client->screen->popup,
				popup_font_set,
				current_screen->menu_gc, tx, ty,
				name, namelen);
	}

	/* 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 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;
	
	getMenuDimensions(&width, &height, &length);
	
	old = current_item;
	current_item = menu_whichitem(e->x_root, e->y_root);
	
	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 n;		/* Menu item. */

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

	/* 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);
	}
}