summaryrefslogtreecommitdiff
path: root/zwgc/xcut.c
blob: 4fad2a71fa74bc32a992afa417033c3f3d25718a (plain)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/* This file is part of the Project Athena Zephyr Notification System.
 * It is one of the source files comprising zwgc, the Zephyr WindowGram
 * client.
 *
 *      Created by:     Marc Horowitz <marc@athena.mit.edu>
 *
 *      $Id$
 *
 *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
 *      For copying and distribution information, see the file
 *      "mit-copyright.h".
 */

#include <sysdep.h>

#if (!defined(lint) && !defined(SABER))
static const char rcsid_xcut_c[] = "$Id$";
#endif

#include <zephyr/mit-copyright.h>

/****************************************************************************/
/*                                                                          */
/*                    Code to deal with handling X events:                  */
/*                                                                          */
/****************************************************************************/

#ifndef X_DISPLAY_MISSING

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include "new_memory.h"
#include "new_string.h"
#include "X_gram.h"
#include "zwgc.h"
#include "xselect.h"
#include "xmark.h"
#include "error.h"
#include "xrevstack.h"
#include "X_driver.h"

/*
 *
 */

extern long ttl;

static char *selected_text=NULL;
static Window selecting_in = 0;

char *
getSelectedText(void)
{
   return(selected_text);
}

#ifdef notdef
static string
x_gram_to_string(x_gram *gram)
{
    int i, index, len;
    int last_y = -1;
    string temp;
    string text_so_far = string_Copy("");
    char *text;

    text = gram->text;
    for (i=0; i<gram->numblocks; i++) {
	if (last_y != -1 && last_y != gram->blocks[i].y)
	  text_so_far = string_Concat2(text_so_far, "\n");
	index = gram->blocks[i].strindex;
	len = gram->blocks[i].strlen;
	temp = string_CreateFromData(text+index, len);
	text_so_far = string_Concat2(text_so_far, temp);
	free(temp);
	last_y = gram->blocks[i].y;
    }

    text_so_far = string_Concat2(text_so_far, "\n");
    return(text_so_far);
}
#endif

/*
 *
 */

/*ARGSUSED*/
Bool
isShiftButton1(Display *dpy,
	       XEvent *event,
	       char *arg)
{
   return(event->xbutton.state & (ShiftMask|Button1Mask));
}

/*ARGSUSED*/
Bool
isShiftButton3(Display *dpy,
	       XEvent *event,
	       char *arg)
{
   return(event->xbutton.state & (ShiftMask|Button3Mask));
}

void
getLastEvent(Display *dpy,
	     unsigned int state,
	     XEvent *event)
{
   XEvent xev;

   if (state & Button1Mask) {
      while(XCheckIfEvent(dpy,&xev,isShiftButton1,NULL))
	 *event=xev;
   } else if (state & Button3Mask) {
      while(XCheckIfEvent(dpy,&xev,isShiftButton3,NULL))
	 *event=xev;
   }
}

void
xunmark(Display *dpy,
	Window w,
	x_gram *gram,
	XContext desc_context)
{
   if (gram == NULL)
     if (XFindContext(dpy, w, desc_context, (caddr_t *) &gram))
       return;

   xmarkClear();
   xmarkRedraw(dpy,w,gram,XMARK_REDRAW_OLD);
}

/* This is out here so xdestroygram can get at it */

#define PRESSOP_NONE 0	/* nothing */
#define PRESSOP_KILL 1	/* normal click */
#define PRESSOP_SEL  2	/* shift left */
#define PRESSOP_EXT  3  /* shift right */
#define PRESSOP_NUKE 4	/* ctrl */
#define PRESSOP_STOP 5  /* pressop cancelled by moving out of window */

static int current_pressop = PRESSOP_NONE;

void
xdestroygram(Display *dpy,
	     Window w,
	     XContext desc_context,
	     x_gram *gram)
{
    struct timeval now;

    gettimeofday(&now,NULL);
    if ((gram->can_die.tv_sec == 0) ||
	(gram->can_die.tv_sec > now.tv_sec) ||
	((gram->can_die.tv_sec == now.tv_sec) &&
	 (gram->can_die.tv_usec > now.tv_usec)))
	return;

    if (w == selecting_in) {
	selecting_in = 0;
	xmarkClear();
    }
    current_pressop = PRESSOP_NONE;
    XDeleteContext(dpy, w, desc_context);
    XDestroyWindow(dpy, w);
    delete_gram(gram);
    free(gram->text);
    free(gram->blocks);
    free(gram);

    if (bottom_gram == NULL && unlinked == NULL) {
       /* flush colormap here */
    }
}

void
xcut(Display *dpy,
     XEvent *event,
     XContext desc_context)
{
    x_gram *gram;
    Window w = event->xany.window;
    int changedbound;

    /*
     * If event is for a window that's not ours anymore (say we're
     * in the process of deleting it...), ignore it:
     */
    if (XFindContext(dpy, w, desc_context, (caddr_t *) &gram))
      return;

    /*
     * Dispatch on the event type:
     */
    switch(event->type) {
      case ClientMessage:
	if ((event->xclient.message_type == XA_WM_PROTOCOLS) &&
	    (event->xclient.format == 32) &&
	    (event->xclient.data.l[0] == XA_WM_DELETE_WINDOW))
	    xdestroygram(dpy,w,desc_context,gram);
	break;

      case MapNotify:
	/* I don't like using the local time, but MapNotify events don't
	 * come with a timestamp, and there's no way to query the server
	 */

	if (gram->can_die.tv_sec == 0) {
	    gettimeofday(&(gram->can_die),NULL);
	    gram->can_die.tv_sec += (int) (ttl/1000);
	    gram->can_die.tv_usec += (ttl%1000)*1000;
	}
	break;

      case UnmapNotify:
	unlink_gram(gram);
	break;

      case LeaveNotify:
	if (current_pressop == PRESSOP_KILL ||
	    current_pressop == PRESSOP_NUKE)
	   current_pressop = PRESSOP_STOP;
	break;

      case MotionNotify:
	if (current_pressop == PRESSOP_SEL) {
	   /*	  getLastEvent(dpy,Button1Mask,event); */
	   changedbound=xmarkExtendFromFirst(gram,event->xmotion.x,
					     event->xmotion.y);
	   xmarkRedraw(dpy,w,gram,changedbound);
	} else if (current_pressop == PRESSOP_EXT) {
	   /*	  getLastEvent(dpy,Button3Mask,event); */
	   changedbound=xmarkExtendFromNearest(gram,event->xmotion.x,
					       event->xmotion.y);
	   xmarkRedraw(dpy,w,gram,changedbound);
	} 
	break;

      case ButtonPress:
	if (current_pressop != PRESSOP_NONE) {
	   current_pressop = PRESSOP_STOP;
	} else if ((event->xbutton.button==Button4 ||
		    event->xbutton.button==Button5) &&
		   !get_bool_resource("scrollDelete","ScrollDelete",0)) {
	   /* Ignore scroll wheel movement. */
	   break;
	} else if ( (event->xbutton.state)&ShiftMask ) {
	   if (event->xbutton.button==Button1) {
	      if (selecting_in)
		 xunmark(dpy,selecting_in,NULL,desc_context);
	      if (selected_text) free(selected_text);
	      selected_text = NULL;
	      if (! xselGetOwnership(dpy,w,event->xbutton.time)) {
		 XBell(dpy,0);
		 ERROR("Unable to get ownership of PRIMARY selection.\n");
		 selecting_in = 0;
		 current_pressop = PRESSOP_STOP;
	      } else {
		 selecting_in = w;
		 xmarkStart(gram,event->xbutton.x,event->xbutton.y);
		 current_pressop = PRESSOP_SEL;
	      }
	   } else if ((event->xbutton.button==Button3) &&
		      (w == selecting_in)) {
	      if (selected_text) free(selected_text);
	      selected_text = NULL;
	      changedbound=xmarkExtendFromNearest(gram,event->xbutton.x,
						  event->xbutton.y);
	      xmarkRedraw(dpy,w,gram,changedbound);
	      selected_text = xmarkGetText();
	      /* this is ok, since to get here, the selection must be owned */
	      current_pressop = PRESSOP_EXT;
	   }
	} else if ( (event->xbutton.state)&ControlMask ) {
	   current_pressop = PRESSOP_NUKE;
	} else {
	   current_pressop = PRESSOP_KILL;
	}
	break;

      case ButtonRelease:
	if (current_pressop == PRESSOP_KILL) {
	   xdestroygram(dpy,w,desc_context,gram);
	} else if (current_pressop == PRESSOP_SEL ||
		   current_pressop == PRESSOP_EXT) {
	   if (selected_text) free(selected_text);
	   selected_text = xmarkGetText();
	} else if (current_pressop == PRESSOP_NUKE) {
	   XWindowAttributes wa;
	   int gx,gy;
	   Window temp;
	   x_gram *next;

	   for (gram = bottom_gram ; gram ; gram = next) {
	      XGetWindowAttributes(dpy,gram->w,&wa);
	      XTranslateCoordinates(dpy,gram->w,wa.root,0,0,&gx,&gy,
				    &temp);

	      next = gram->above;

	      if ((wa.map_state == IsViewable) &&
		  (gx <= event->xbutton.x_root) &&
		  (event->xbutton.x_root < gx+wa.width) &&
		  (gy <= event->xbutton.y_root) &&
		  (event->xbutton.y_root < gy+wa.height)) {
		 xdestroygram(dpy,gram->w,desc_context,gram);
	      }
	   }
	   for (gram = unlinked ; gram ; gram = next) {
	      XGetWindowAttributes(dpy,gram->w,&wa);
	      XTranslateCoordinates(dpy,gram->w,wa.root,0,0,&gx,&gy,
				    &temp);

	      next = gram->above;

	      if ((wa.map_state == IsViewable) &&
		  (gx <= event->xbutton.x_root) &&
		  (event->xbutton.x_root < gx+wa.width) &&
		  (gy <= event->xbutton.y_root) &&
		  (event->xbutton.y_root < gy+wa.height)) {
		 xdestroygram(dpy,gram->w,desc_context,gram);
	      }
	   }
	}
	current_pressop = PRESSOP_NONE;
	break;

     case SelectionRequest:
       xselProcessSelection(dpy,w,event);
       break;

     case SelectionClear:
       xselOwnershipLost(event->xselectionclear.time);
       if (w == selecting_in) {
	  selecting_in = 0;
	  xunmark(dpy,w,gram,desc_context);
	  if (selected_text) free(selected_text);
	  selected_text = NULL;
       }
       break;

#ifdef notdef
      case ConfigureNotify:
#ifdef DEBUG
	if (zwgc_debug)
	  printf("ConfigureNotify received for wid %lx above wid %lx\n",
		 (long) w,(long) event->xconfigure.above);
#endif
	if (gram->above==gram) {
	   /* a new zgram.  Straight to the bottom! */
	   add_to_bottom(gram);
	} else if (event->xconfigure.above)  {
	   /* some zgram was pulled to the top */
	   pull_to_top(gram);
	} else {
	   /* Some zgram was pushed to the bottom */
	   push_to_bottom(gram);
	}
	/* Note that there is no option to configure a zgram to the middle */
	break;
#endif
      default:
	break;
    }

    XFlush(dpy);
}

#endif