1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It is one of the source files comprising zwgc, the Zephyr WindowGram
5 * Created by: Marc Horowitz <marc@athena.mit.edu>
9 * Copyright (c) 1989 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
16 #if (!defined(lint) && !defined(SABER))
17 static const char rcsid_xcut_c[] = "$Id$";
20 #include <zephyr/mit-copyright.h>
22 /****************************************************************************/
24 /* Code to deal with handling X events: */
26 /****************************************************************************/
28 #ifndef X_DISPLAY_MISSING
31 #include <X11/Xutil.h>
32 #include "new_memory.h"
33 #include "new_string.h"
39 #include "xrevstack.h"
47 static char *selected_text=NULL;
48 static Window selecting_in = 0;
53 return(selected_text);
58 x_gram_to_string(x_gram *gram)
63 string text_so_far = string_Copy("");
67 for (i=0; i<gram->numblocks; i++) {
68 if (last_y != -1 && last_y != gram->blocks[i].y)
69 text_so_far = string_Concat2(text_so_far, "\n");
70 index = gram->blocks[i].strindex;
71 len = gram->blocks[i].strlen;
72 temp = string_CreateFromData(text+index, len);
73 text_so_far = string_Concat2(text_so_far, temp);
75 last_y = gram->blocks[i].y;
78 text_so_far = string_Concat2(text_so_far, "\n");
89 isShiftButton1(Display *dpy,
93 return(event->xbutton.state & (ShiftMask|Button1Mask));
98 isShiftButton3(Display *dpy,
102 return(event->xbutton.state & (ShiftMask|Button3Mask));
106 getLastEvent(Display *dpy,
112 if (state & Button1Mask) {
113 while(XCheckIfEvent(dpy,&xev,isShiftButton1,NULL))
115 } else if (state & Button3Mask) {
116 while(XCheckIfEvent(dpy,&xev,isShiftButton3,NULL))
122 xunmark(Display *dpy,
125 XContext desc_context)
128 if (XFindContext(dpy, w, desc_context, (caddr_t *) &gram))
132 xmarkRedraw(dpy,w,gram,XMARK_REDRAW_OLD);
135 /* This is out here so xdestroygram can get at it */
137 #define PRESSOP_NONE 0 /* nothing */
138 #define PRESSOP_KILL 1 /* normal click */
139 #define PRESSOP_SEL 2 /* shift left */
140 #define PRESSOP_EXT 3 /* shift right */
141 #define PRESSOP_NUKE 4 /* ctrl */
142 #define PRESSOP_STOP 5 /* pressop cancelled by moving out of window */
144 static int current_pressop = PRESSOP_NONE;
147 xdestroygram(Display *dpy,
149 XContext desc_context,
154 gettimeofday(&now,NULL);
155 if ((gram->can_die.tv_sec == 0) ||
156 (gram->can_die.tv_sec > now.tv_sec) ||
157 ((gram->can_die.tv_sec == now.tv_sec) &&
158 (gram->can_die.tv_usec > now.tv_usec)))
161 if (w == selecting_in) {
165 current_pressop = PRESSOP_NONE;
166 XDeleteContext(dpy, w, desc_context);
167 XDestroyWindow(dpy, w);
173 if (bottom_gram == NULL && unlinked == NULL) {
174 /* flush colormap here */
181 XContext desc_context)
184 Window w = event->xany.window;
188 * If event is for a window that's not ours anymore (say we're
189 * in the process of deleting it...), ignore it:
191 if (XFindContext(dpy, w, desc_context, (caddr_t *) &gram))
195 * Dispatch on the event type:
197 switch(event->type) {
199 if ((event->xclient.message_type == XA_WM_PROTOCOLS) &&
200 (event->xclient.format == 32) &&
201 (event->xclient.data.l[0] == XA_WM_DELETE_WINDOW))
202 xdestroygram(dpy,w,desc_context,gram);
206 /* I don't like using the local time, but MapNotify events don't
207 * come with a timestamp, and there's no way to query the server
210 if (gram->can_die.tv_sec == 0) {
211 gettimeofday(&(gram->can_die),NULL);
212 gram->can_die.tv_sec += (int) (ttl/1000);
213 gram->can_die.tv_usec += (ttl%1000)*1000;
222 if (current_pressop == PRESSOP_KILL ||
223 current_pressop == PRESSOP_NUKE)
224 current_pressop = PRESSOP_STOP;
228 if (current_pressop == PRESSOP_SEL) {
229 /* getLastEvent(dpy,Button1Mask,event); */
230 changedbound=xmarkExtendFromFirst(gram,event->xmotion.x,
232 xmarkRedraw(dpy,w,gram,changedbound);
233 } else if (current_pressop == PRESSOP_EXT) {
234 /* getLastEvent(dpy,Button3Mask,event); */
235 changedbound=xmarkExtendFromNearest(gram,event->xmotion.x,
237 xmarkRedraw(dpy,w,gram,changedbound);
242 if (current_pressop != PRESSOP_NONE) {
243 current_pressop = PRESSOP_STOP;
244 } else if ((event->xbutton.button==Button4 ||
245 event->xbutton.button==Button5) &&
246 !get_bool_resource("scrollDelete","ScrollDelete",0)) {
247 /* Ignore scroll wheel movement. */
249 } else if ( (event->xbutton.state)&ShiftMask ) {
250 if (event->xbutton.button==Button1) {
252 xunmark(dpy,selecting_in,NULL,desc_context);
253 if (selected_text) free(selected_text);
254 selected_text = NULL;
255 if (! xselGetOwnership(dpy,w,event->xbutton.time)) {
257 ERROR("Unable to get ownership of PRIMARY selection.\n");
259 current_pressop = PRESSOP_STOP;
262 xmarkStart(gram,event->xbutton.x,event->xbutton.y);
263 current_pressop = PRESSOP_SEL;
265 } else if ((event->xbutton.button==Button3) &&
266 (w == selecting_in)) {
267 if (selected_text) free(selected_text);
268 selected_text = NULL;
269 changedbound=xmarkExtendFromNearest(gram,event->xbutton.x,
271 xmarkRedraw(dpy,w,gram,changedbound);
272 selected_text = xmarkGetText();
273 /* this is ok, since to get here, the selection must be owned */
274 current_pressop = PRESSOP_EXT;
276 } else if ( (event->xbutton.state)&ControlMask ) {
277 current_pressop = PRESSOP_NUKE;
279 current_pressop = PRESSOP_KILL;
284 if (current_pressop == PRESSOP_KILL) {
285 xdestroygram(dpy,w,desc_context,gram);
286 } else if (current_pressop == PRESSOP_SEL ||
287 current_pressop == PRESSOP_EXT) {
288 if (selected_text) free(selected_text);
289 selected_text = xmarkGetText();
290 } else if (current_pressop == PRESSOP_NUKE) {
291 XWindowAttributes wa;
296 for (gram = bottom_gram ; gram ; gram = next) {
297 XGetWindowAttributes(dpy,gram->w,&wa);
298 XTranslateCoordinates(dpy,gram->w,wa.root,0,0,&gx,&gy,
303 if ((wa.map_state == IsViewable) &&
304 (gx <= event->xbutton.x_root) &&
305 (event->xbutton.x_root < gx+wa.width) &&
306 (gy <= event->xbutton.y_root) &&
307 (event->xbutton.y_root < gy+wa.height)) {
308 xdestroygram(dpy,gram->w,desc_context,gram);
311 for (gram = unlinked ; gram ; gram = next) {
312 XGetWindowAttributes(dpy,gram->w,&wa);
313 XTranslateCoordinates(dpy,gram->w,wa.root,0,0,&gx,&gy,
318 if ((wa.map_state == IsViewable) &&
319 (gx <= event->xbutton.x_root) &&
320 (event->xbutton.x_root < gx+wa.width) &&
321 (gy <= event->xbutton.y_root) &&
322 (event->xbutton.y_root < gy+wa.height)) {
323 xdestroygram(dpy,gram->w,desc_context,gram);
327 current_pressop = PRESSOP_NONE;
330 case SelectionRequest:
331 xselProcessSelection(dpy,w,event);
335 xselOwnershipLost(event->xselectionclear.time);
336 if (w == selecting_in) {
338 xunmark(dpy,w,gram,desc_context);
339 if (selected_text) free(selected_text);
340 selected_text = NULL;
345 case ConfigureNotify:
348 printf("ConfigureNotify received for wid %lx above wid %lx\n",
349 (long) w,(long) event->xconfigure.above);
351 if (gram->above==gram) {
352 /* a new zgram. Straight to the bottom! */
354 } else if (event->xconfigure.above) {
355 /* some zgram was pulled to the top */
358 /* Some zgram was pushed to the bottom */
359 push_to_bottom(gram);
361 /* Note that there is no option to configure a zgram to the middle */