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"
48 static char *selected_text=NULL;
49 static Window selecting_in = 0;
54 return(selected_text);
59 x_gram_to_string(x_gram *gram)
64 string text_so_far = string_Copy("");
68 for (i=0; i<gram->numblocks; i++) {
69 if (last_y != -1 && last_y != gram->blocks[i].y)
70 text_so_far = string_Concat2(text_so_far, "\n");
71 index = gram->blocks[i].strindex;
72 len = gram->blocks[i].strlen;
73 temp = string_CreateFromData(text+index, len);
74 text_so_far = string_Concat2(text_so_far, temp);
76 last_y = gram->blocks[i].y;
79 text_so_far = string_Concat2(text_so_far, "\n");
90 isShiftButton1(Display *dpy,
94 return(event->xbutton.state & (ShiftMask|Button1Mask));
99 isShiftButton3(Display *dpy,
103 return(event->xbutton.state & (ShiftMask|Button3Mask));
107 getLastEvent(Display *dpy,
113 if (state & Button1Mask) {
114 while(XCheckIfEvent(dpy,&xev,isShiftButton1,NULL))
116 } else if (state & Button3Mask) {
117 while(XCheckIfEvent(dpy,&xev,isShiftButton3,NULL))
123 xunmark(Display *dpy,
126 XContext desc_context)
129 if (XFindContext(dpy, w, desc_context, (caddr_t *) &gram))
133 xmarkRedraw(dpy,w,gram,XMARK_REDRAW_OLD);
136 /* This is out here so xdestroygram can get at it */
138 #define PRESSOP_NONE 0 /* nothing */
139 #define PRESSOP_KILL 1 /* normal click */
140 #define PRESSOP_SEL 2 /* shift left */
141 #define PRESSOP_EXT 3 /* shift right */
142 #define PRESSOP_NUKE 4 /* ctrl */
143 #define PRESSOP_STOP 5 /* pressop cancelled by moving out of window */
145 static int current_pressop = PRESSOP_NONE;
148 xdestroygram(Display *dpy,
150 XContext desc_context,
155 gettimeofday(&now,NULL);
156 if ((gram->can_die.tv_sec == 0) ||
157 (gram->can_die.tv_sec > now.tv_sec) ||
158 ((gram->can_die.tv_sec == now.tv_sec) &&
159 (gram->can_die.tv_usec > now.tv_usec)))
162 if (w == selecting_in) {
166 current_pressop = PRESSOP_NONE;
167 XDeleteContext(dpy, w, desc_context);
168 XDestroyWindow(dpy, w);
174 if (bottom_gram == NULL && unlinked == NULL) {
175 /* flush colormap here */
182 XContext desc_context)
185 Window w = event->xany.window;
189 * If event is for a window that's not ours anymore (say we're
190 * in the process of deleting it...), ignore it:
192 if (XFindContext(dpy, w, desc_context, (caddr_t *) &gram))
196 * Dispatch on the event type:
198 switch(event->type) {
200 if ((event->xclient.message_type == XA_WM_PROTOCOLS) &&
201 (event->xclient.format == 32) &&
202 (event->xclient.data.l[0] == XA_WM_DELETE_WINDOW))
203 xdestroygram(dpy,w,desc_context,gram);
207 /* I don't like using the local time, but MapNotify events don't
208 * come with a timestamp, and there's no way to query the server
211 if (gram->can_die.tv_sec == 0) {
212 gettimeofday(&(gram->can_die),NULL);
213 gram->can_die.tv_sec += (int) (ttl/1000);
214 gram->can_die.tv_usec += (ttl%1000)*1000;
223 if (current_pressop == PRESSOP_KILL ||
224 current_pressop == PRESSOP_NUKE)
225 current_pressop = PRESSOP_STOP;
229 if (current_pressop == PRESSOP_SEL) {
230 /* getLastEvent(dpy,Button1Mask,event); */
231 changedbound=xmarkExtendFromFirst(gram,event->xmotion.x,
233 xmarkRedraw(dpy,w,gram,changedbound);
234 } else if (current_pressop == PRESSOP_EXT) {
235 /* getLastEvent(dpy,Button3Mask,event); */
236 changedbound=xmarkExtendFromNearest(gram,event->xmotion.x,
238 xmarkRedraw(dpy,w,gram,changedbound);
243 if (current_pressop != PRESSOP_NONE) {
244 current_pressop = PRESSOP_STOP;
245 } else if ((event->xbutton.button==Button4 ||
246 event->xbutton.button==Button5) &&
247 !get_bool_resource("scrollDelete","ScrollDelete",0)) {
248 /* Ignore scroll wheel movement. */
250 } else if ( (event->xbutton.state)&ShiftMask ) {
251 if (event->xbutton.button==Button1) {
253 xunmark(dpy,selecting_in,NULL,desc_context);
254 if (selected_text) free(selected_text);
255 selected_text = NULL;
256 if (! xselGetOwnership(dpy,w,event->xbutton.time)) {
258 ERROR("Unable to get ownership of PRIMARY selection.\n");
260 current_pressop = PRESSOP_STOP;
263 xmarkStart(gram,event->xbutton.x,event->xbutton.y);
264 current_pressop = PRESSOP_SEL;
266 } else if ((event->xbutton.button==Button3) &&
267 (w == selecting_in)) {
268 if (selected_text) free(selected_text);
269 selected_text = NULL;
270 changedbound=xmarkExtendFromNearest(gram,event->xbutton.x,
272 xmarkRedraw(dpy,w,gram,changedbound);
273 selected_text = xmarkGetText();
274 /* this is ok, since to get here, the selection must be owned */
275 current_pressop = PRESSOP_EXT;
277 } else if ( (event->xbutton.state)&ControlMask ) {
278 current_pressop = PRESSOP_NUKE;
280 current_pressop = PRESSOP_KILL;
285 if (current_pressop == PRESSOP_KILL) {
286 xdestroygram(dpy,w,desc_context,gram);
287 } else if (current_pressop == PRESSOP_SEL ||
288 current_pressop == PRESSOP_EXT) {
289 if (selected_text) free(selected_text);
290 selected_text = xmarkGetText();
291 } else if (current_pressop == PRESSOP_NUKE) {
292 XWindowAttributes wa;
297 for (gram = bottom_gram ; gram ; gram = next) {
298 XGetWindowAttributes(dpy,gram->w,&wa);
299 XTranslateCoordinates(dpy,gram->w,wa.root,0,0,&gx,&gy,
304 if ((wa.map_state == IsViewable) &&
305 (gx <= event->xbutton.x_root) &&
306 (event->xbutton.x_root < gx+wa.width) &&
307 (gy <= event->xbutton.y_root) &&
308 (event->xbutton.y_root < gy+wa.height)) {
309 xdestroygram(dpy,gram->w,desc_context,gram);
312 for (gram = unlinked ; gram ; gram = next) {
313 XGetWindowAttributes(dpy,gram->w,&wa);
314 XTranslateCoordinates(dpy,gram->w,wa.root,0,0,&gx,&gy,
319 if ((wa.map_state == IsViewable) &&
320 (gx <= event->xbutton.x_root) &&
321 (event->xbutton.x_root < gx+wa.width) &&
322 (gy <= event->xbutton.y_root) &&
323 (event->xbutton.y_root < gy+wa.height)) {
324 xdestroygram(dpy,gram->w,desc_context,gram);
328 current_pressop = PRESSOP_NONE;
331 case SelectionRequest:
332 xselProcessSelection(dpy,w,event);
336 xselOwnershipLost(event->xselectionclear.time);
337 if (w == selecting_in) {
339 xunmark(dpy,w,gram,desc_context);
340 if (selected_text) free(selected_text);
341 selected_text = NULL;
346 case ConfigureNotify:
349 printf("ConfigureNotify received for wid %lx above wid %lx\n",
350 (long) w,(long) event->xconfigure.above);
352 if (gram->above==gram) {
353 /* a new zgram. Straight to the bottom! */
355 } else if (event->xconfigure.above) {
356 /* some zgram was pulled to the top */
359 /* Some zgram was pushed to the bottom */
360 push_to_bottom(gram);
362 /* Note that there is no option to configure a zgram to the middle */