summaryrefslogtreecommitdiffstats
path: root/src/bltGrBind.C
blob: 362bfe91772e31f72e021e8c09ba236fba242973 (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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * Smithsonian Astrophysical Observatory, Cambridge, MA, USA
 * This code has been modified under the terms listed below and is made
 * available under the same terms.
 */

/*
 *	Copyright 1998 George A Howlett.
 *
 *	Permission is hereby granted, free of charge, to any person obtaining
 *	a copy of this software and associated documentation files (the
 *	"Software"), to deal in the Software without restriction, including
 *	without limitation the rights to use, copy, modify, merge, publish,
 *	distribute, sublicense, and/or sell copies of the Software, and to
 *	permit persons to whom the Software is furnished to do so, subject to
 *	the following conditions:
 *
 *	The above copyright notice and this permission notice shall be
 *	included in all copies or substantial portions of the Software.
 *
 *	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *	LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *	OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *	WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <stdlib.h>

#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

#include "bltGrBind.h"

static Tk_EventProc BindProc;

BindTable::BindTable(Tcl_Interp* interp, Tk_Window tkwinn, 
		     ClientData clientDataa,
		     Blt_BindPickProc* pickProcc)
{
  flags =0;
  bindingTable = Tk_CreateBindingTable(interp);
  currentItem =NULL;
  currentContext =NULL;
  newItem =NULL;
  newContext =NULL;
  focusItem =NULL;
  focusContext =NULL;
  //  pickEvent =NULL;
  state =0;
  clientData = clientDataa;
  tkwin = tkwinn;
  pickProc = pickProcc;
  unsigned int mask = (KeyPressMask | KeyReleaseMask | ButtonPressMask |
		       ButtonReleaseMask | EnterWindowMask | LeaveWindowMask |
		       PointerMotionMask);
  Tk_CreateEventHandler(tkwin, mask, BindProc, this);
}

BindTable::~BindTable()
{
  Tk_DeleteBindingTable(bindingTable);
  unsigned int mask = (KeyPressMask | KeyReleaseMask | ButtonPressMask |
		       ButtonReleaseMask | EnterWindowMask | LeaveWindowMask |
		       PointerMotionMask);
  Tk_DeleteEventHandler(tkwin, mask, BindProc, this);
}

int BindTable::configure(Tcl_Interp* interp, ClientData item, 
			 int objc, Tcl_Obj* const objv[])
{
  if (objc == 0) {
    Tk_GetAllBindings(interp, bindingTable, item);
    return TCL_OK;
  }

  const char *string = Tcl_GetString(objv[0]);
  if (objc == 1) {
    const char* command = Tk_GetBinding(interp, bindingTable, item, string);
    if (!command) {
      Tcl_ResetResult(interp);
      Tcl_AppendResult(interp, "invalid binding event \"", string, "\"", NULL);
      return TCL_ERROR;
    }
    Tcl_SetStringObj(Tcl_GetObjResult(interp), command, -1);
    return TCL_OK;
  }

  const char* seq = string;
  const char* command = Tcl_GetString(objv[1]);
  if (command[0] == '\0')
    return Tk_DeleteBinding(interp, bindingTable, item, seq);

  unsigned long mask;
  if (command[0] == '+')
    mask = Tk_CreateBinding(interp, bindingTable, item, seq, command+1, 1);
  else
    mask = Tk_CreateBinding(interp, bindingTable, item, seq, command, 0);
  if (!mask)
    return TCL_ERROR;

  if (mask & (unsigned) ~(ButtonMotionMask|Button1MotionMask
			  |Button2MotionMask|Button3MotionMask|Button4MotionMask
			  |Button5MotionMask|ButtonPressMask|ButtonReleaseMask
			  |EnterWindowMask|LeaveWindowMask|KeyPressMask
			  |KeyReleaseMask|PointerMotionMask|VirtualEventMask)) {
    Tk_DeleteBinding(interp, bindingTable, item, seq);
    Tcl_ResetResult(interp);
    Tcl_AppendResult(interp, "requested illegal events; ",
		     "only key, button, motion, enter, leave, and virtual ",
		     "events may be used", (char *)NULL);
    return TCL_ERROR;
  }

  return TCL_OK;
}

void BindTable::deleteBindings(ClientData object)
{
  Tk_DeleteAllBindings(bindingTable, object);

  // If this is the object currently picked, we need to repick one.
  if (currentItem == object) {
    currentItem =NULL;
    currentContext =NULL;
  }

  if (newItem == object) {
    newItem =NULL;
    newContext =NULL;
  }

  if (focusItem == object) {
    focusItem =NULL;
    focusContext =NULL;
  }
}

#define REPICK_IN_PROGRESS (1<<0)
#define LEFT_GRABBED_ITEM  (1<<1)

const char** BltGraphTags(BindTable* table, ClientData object, ClientData context, int* num);

static void BltDoEvent(BindTable* bindPtr, XEvent* eventPtr, 
		       ClientData item, ClientData context)
{
  if (!bindPtr->tkwin || !bindPtr->bindingTable)
    return;

  if ((eventPtr->type == KeyPress) || (eventPtr->type == KeyRelease)) {
    item = bindPtr->focusItem;
    context = bindPtr->focusContext;
  }
  if (!item)
    return;

  int nTags;
  const char** tagArray = BltGraphTags(bindPtr, item, context, &nTags);
  Tk_BindEvent(bindPtr->bindingTable, eventPtr, bindPtr->tkwin, nTags, 
	       (void**)tagArray);

  if (tagArray)
    delete [] tagArray;
}

static void PickCurrentItem(BindTable* bindPtr, XEvent* eventPtr)
{
  // Check whether or not a button is down.  If so, we'll log entry and exit
  // into and out of the current item, but not entry into any other item.
  // This implements a form of grabbing equivalent to what the X server does
  // for windows.

  int buttonDown = (bindPtr->state & (Button1Mask | Button2Mask | Button3Mask | Button4Mask | Button5Mask));
  if (!buttonDown)
    bindPtr->flags &= ~LEFT_GRABBED_ITEM;

  // Save information about this event in the widget.  The event in the
  // widget is used for two purposes:
  // 1. Event bindings: if the current item changes, fake events are
  //    generated to allow item-enter and item-leave bindings to trigger.
  // 2. Reselection: if the current item gets deleted, can use the
  //    saved event to find a new current item.
  // Translate MotionNotify events into EnterNotify events, since that's
  // what gets reported to item handlers.

  if (eventPtr != &bindPtr->pickEvent) {
    if ((eventPtr->type == MotionNotify) || (eventPtr->type == ButtonRelease)) {
      bindPtr->pickEvent.xcrossing.type = EnterNotify;
      bindPtr->pickEvent.xcrossing.serial = eventPtr->xmotion.serial;
      bindPtr->pickEvent.xcrossing.send_event =	eventPtr->xmotion.send_event;
      bindPtr->pickEvent.xcrossing.display = eventPtr->xmotion.display;
      bindPtr->pickEvent.xcrossing.window = eventPtr->xmotion.window;
      bindPtr->pickEvent.xcrossing.root = eventPtr->xmotion.root;
      bindPtr->pickEvent.xcrossing.subwindow = None;
      bindPtr->pickEvent.xcrossing.time = eventPtr->xmotion.time;
      bindPtr->pickEvent.xcrossing.x = eventPtr->xmotion.x;
      bindPtr->pickEvent.xcrossing.y = eventPtr->xmotion.y;
      bindPtr->pickEvent.xcrossing.x_root = eventPtr->xmotion.x_root;
      bindPtr->pickEvent.xcrossing.y_root = eventPtr->xmotion.y_root;
      bindPtr->pickEvent.xcrossing.mode = NotifyNormal;
      bindPtr->pickEvent.xcrossing.detail = NotifyNonlinear;
      bindPtr->pickEvent.xcrossing.same_screen = eventPtr->xmotion.same_screen;
      bindPtr->pickEvent.xcrossing.focus = False;
      bindPtr->pickEvent.xcrossing.state = eventPtr->xmotion.state;
    }
    else
      bindPtr->pickEvent = *eventPtr;
  }

  // If this is a recursive call (there's already a partially completed call
  // pending on the stack; it's in the middle of processing a Leave event
  // handler for the old current item) then just return; the pending call
  // will do everything that's needed.
  if (bindPtr->flags & REPICK_IN_PROGRESS)
    return;

  // A LeaveNotify event automatically means that there's no current item,
  // so the check for closest item can be skipped.
  ClientData newContext =NULL;
  ClientData newItem =NULL;
  if (bindPtr->pickEvent.type != LeaveNotify) {
    int x = bindPtr->pickEvent.xcrossing.x;
    int y = bindPtr->pickEvent.xcrossing.y;
    newItem = (*bindPtr->pickProc)(bindPtr->clientData, x, y, &newContext);
  }

  // Nothing to do:  the current item hasn't changed.
  if (((newItem == bindPtr->currentItem) && (newContext == bindPtr->currentContext)) && ((bindPtr->flags & LEFT_GRABBED_ITEM) == 0))
    return;

  // Simulate a LeaveNotify event on the previous current item and an
  // EnterNotify event on the new current item.  Remove the "current" tag
  // from the previous current item and place it on the new current item.
  ClientData oldItem = bindPtr->currentItem;
  Tcl_Preserve(oldItem);
  Tcl_Preserve(newItem);

  if ((bindPtr->currentItem != NULL) && ((newItem != bindPtr->currentItem) || (newContext != bindPtr->currentContext)) && ((bindPtr->flags & LEFT_GRABBED_ITEM) == 0)) {
    XEvent event = bindPtr->pickEvent;
    event.type = LeaveNotify;

    // If the event's detail happens to be NotifyInferior the binding
    // mechanism will discard the event.  To be consistent, always use
    // NotifyAncestor.
    event.xcrossing.detail = NotifyAncestor;

    bindPtr->flags |= REPICK_IN_PROGRESS;
    BltDoEvent(bindPtr, &event, bindPtr->currentItem, bindPtr->currentContext);
    bindPtr->flags &= ~REPICK_IN_PROGRESS;

    // Note: during DoEvent above, it's possible that bindPtr->newItem got
    // reset to NULL because the item was deleted.
  }

  if (((newItem != bindPtr->currentItem) || (newContext != bindPtr->currentContext)) && (buttonDown)) {
    bindPtr->flags |= LEFT_GRABBED_ITEM;
    XEvent event = bindPtr->pickEvent;
    if ((newItem != bindPtr->newItem) || 
	(newContext != bindPtr->newContext)) {

      // Generate <Enter> and <Leave> events for objects during button
      // grabs. This isn't standard. But for example, it allows one to
      // provide balloon help on the individual entries of the Hierbox
      // widget.
      ClientData savedItem = bindPtr->currentItem;
      ClientData savedContext = bindPtr->currentContext;
      if (bindPtr->newItem != NULL) {
	event.type = LeaveNotify;
	event.xcrossing.detail = NotifyVirtual; // Ancestor
	bindPtr->currentItem = bindPtr->newItem;
	BltDoEvent(bindPtr, &event, bindPtr->newItem, bindPtr->newContext);
      }

      bindPtr->newItem = newItem;
      bindPtr->newContext = newContext;
      if (newItem != NULL) {
	event.type = EnterNotify;
	event.xcrossing.detail = NotifyVirtual; // Ancestor
	bindPtr->currentItem = newItem;
	BltDoEvent(bindPtr, &event, newItem, newContext);
      }

      bindPtr->currentItem = savedItem;
      bindPtr->currentContext = savedContext;
    }
    goto done;
  }

  // Special note:  it's possible that bindPtr->newItem == bindPtr->currentItem
  // here. This can happen, for example, if LEFT_GRABBED_ITEM was set.
  bindPtr->flags &= ~LEFT_GRABBED_ITEM;
  bindPtr->currentItem = bindPtr->newItem = newItem;
  bindPtr->currentContext = bindPtr->newContext = newContext;
  if (bindPtr->currentItem != NULL) {
    XEvent event = bindPtr->pickEvent;
    event.type = EnterNotify;
    event.xcrossing.detail = NotifyAncestor;
    BltDoEvent(bindPtr, &event, newItem, newContext);
  }

 done:
  Tcl_Release(newItem);
  Tcl_Release(oldItem);
}

static void BindProc(ClientData clientData, XEvent* eventPtr)
{
  // This code below keeps track of the current modifier state in
  // bindPtr->state.  This information is used to defer repicks of the
  // current item while buttons are down.

  BindTable* bindPtr = (BindTable*)clientData;

  Tcl_Preserve(bindPtr->clientData);

  switch (eventPtr->type) {
  case ButtonPress:
  case ButtonRelease:
    {
      int mask = 0;
      switch (eventPtr->xbutton.button) {
      case Button1:
	mask = Button1Mask;
	break;
      case Button2:
	mask = Button2Mask;
	break;
      case Button3:
	mask = Button3Mask;
	break;
      case Button4:
	mask = Button4Mask;
	break;
      case Button5:
	mask = Button5Mask;
	break;
      default:
	break;
      }

      // For button press events, repick the current item using the button
      // state before the event, then process the event.  For button release
      // events, first process the event, then repick the current item using
      // the button state *after* the event (the button has logically gone
      // up before we change the current item).

      if (eventPtr->type == ButtonPress) {
	// On a button press, first repick the current item using the
	// button state before the event, the process the event.
	bindPtr->state = eventPtr->xbutton.state;
	PickCurrentItem(bindPtr, eventPtr);
	bindPtr->state ^= mask;
	BltDoEvent(bindPtr, eventPtr, bindPtr->currentItem,
		   bindPtr->currentContext);
      }
      else {
	// Button release: first process the event, with the button still
	// considered to be down.  Then repick the current item under the
	// assumption that the button is no longer down.
	bindPtr->state = eventPtr->xbutton.state;
	BltDoEvent(bindPtr, eventPtr, bindPtr->currentItem, 
		   bindPtr->currentContext);
	eventPtr->xbutton.state ^= mask;
	bindPtr->state = eventPtr->xbutton.state;
	PickCurrentItem(bindPtr, eventPtr);
	eventPtr->xbutton.state ^= mask;
      }
    }
    break;

  case EnterNotify:
  case LeaveNotify:
    bindPtr->state = eventPtr->xcrossing.state;
    PickCurrentItem(bindPtr, eventPtr);
    break;

  case MotionNotify:
    bindPtr->state = eventPtr->xmotion.state;
    PickCurrentItem(bindPtr, eventPtr);
    BltDoEvent(bindPtr, eventPtr, bindPtr->currentItem, 
	       bindPtr->currentContext);
    break;

  case KeyPress:
  case KeyRelease:
    bindPtr->state = eventPtr->xkey.state;
    PickCurrentItem(bindPtr, eventPtr);
    BltDoEvent(bindPtr, eventPtr, bindPtr->currentItem, 
	       bindPtr->currentContext);
    break;
  }

  Tcl_Release(bindPtr->clientData);
}