summaryrefslogtreecommitdiff
path: root/zwgc/xcut.c
blob: ce698b6ec0df18f01d485179f7867eeee97b5265 (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
/* 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>
 *
 *      $Source$
 *      $Author$
 *
 *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
 *      For copying and distribution information, see the file
 *      "mit-copyright.h".
 */

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

#include <zephyr/mit-copyright.h>

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

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

/*
 *
 */

extern char *xmarkGetText();

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

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

#ifdef notdef
static string x_gram_to_string(gram)
     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(dpy,event,arg)
     Display *dpy;
     XEvent *event;
     char *arg;
{
   return(event->xbutton.state & (ShiftMask|Button1Mask));
}

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

void getLastEvent(dpy,state,event)
     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(dpy,w,gram,desc_context)
     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);
}
     

void xcut(dpy,event,desc_context)
     Display *dpy;
     XEvent *event;
     XContext desc_context;
{
    x_gram *gram;
    Window w = event->xany.window;
    static int current_window_in = -1;
    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 EnterNotify:
       current_window_in = w;
       break;

     case LeaveNotify:
       current_window_in = -1;
       break;

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

      case ButtonPress:
	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;
	      } else {
		 selecting_in = w;
		 xmarkStart(gram,event->xbutton.x,event->xbutton.y);
	      }
	   }
	   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();
	   }
	} else {
	   current_window_in = w;
	}
	break;

      case ButtonRelease:
	if (w == current_window_in && !((event->xbutton.state)&ShiftMask)) {
	   if (w == selecting_in) {
	      selecting_in = 0;
	      xmarkClear();
	   }
	   if (reverse_stack && (gram == bottom_gram))
	      bottom_gram = gram;
	   XDeleteContext(dpy, w, desc_context);
	   XDestroyWindow(dpy, w);
	   if (reverse_stack)
	     delete_gram(gram);
	   free(gram->text);
	   free(gram->blocks);
	   free(gram);
	} else if (w == selecting_in) {
	   if (selected_text) free(selected_text);
	   selected_text = xmarkGetText();
	}
	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
    }

    XFlush(dpy);
}