aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.6/libjava/gnu/awt/LightweightRedirector.java
blob: 41dc124279cf9736034e864e0a8206d85340986f (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
/* Copyright (C) 2000  Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package gnu.awt;

import java.awt.AWTEvent;
import java.awt.AWTError;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.MouseEvent;
import java.awt.event.InputEvent;

/**
 * Encapsulates the logic required to dispatch events to the correct
 * component in a component tree that may contain lightweight
 * components. Toolkits typically only identify heavyweight components
 * as the source of events. This class redirects the events to the
 * appropriate lightweight children of the heavyweight component.
 */
public class LightweightRedirector
{
  final static int LAST_BUTTON_NUMBER = 3;

  /* We sacrifice one array element to allow the button number to 
     match the index of this array. */
  Component[] releaseTargets = new Component[LAST_BUTTON_NUMBER+1];

  /** 
   *
   * Modifies or replaces the given event with an event that has been
   * properly redirected.  State of button presses are kept so that
   * button releases can be redirected to the same component as the
   * button press.  It is required that all events are sent through
   * this method in chronological order.
   */
  public AWTEvent redirect(AWTEvent event)
  {
    if (event instanceof MouseEvent)
      return redirectMouse((MouseEvent) event);

    /* In case we don't know how to redirect the event, simply return
       the event unchanged. */
    return event;
  }

  MouseEvent redirectMouse(MouseEvent event)
  {
    int button = getButtonNumber(event);
    int id = event.getID();

    Component heavySource = (Component) event.getSource();
    Component source = heavySource;
    int x = event.getX();
    int y = event.getY();

    if (id == MouseEvent.MOUSE_RELEASED)
      {
	Component target = releaseTargets[button];

	if (target != null)
	  {
	    releaseTargets[button] = null;
	    source = target;

	    Component child = source;
	    while (child != heavySource)
	      {
		x -= child.getX();
		y -= child.getY();
		child = child.getParent();
		if (child == null)
		  System.err.println("warning, orphaned release target");
	      }
	  }
      }
    else
      {
	/* Find real component, and adjust source, x and y
	   accordingly. */
	
	while (true)
	  {
	    Component parent = source;
	    
	    Component child = parent.getComponentAt(x, y);
	    
	    if (parent == child)
	      break;
	    
	    // maybe ignoring would be better?
	    if (child == null)
	      {
		String msg = "delivered event not within component. " +
		  "Heavyweight source was " + heavySource + ". " +
		  "Component was " + parent;
		throw new AWTError(msg);
	      }
	    if (child.isLightweight())
	      {
		// descend down to child
		source = child;
		x -= child.getX();
		y -= child.getY();
	      }
	    else
	      {
		System.err.println("warning: event delivered to wrong " +
				   "heavyweight component. Was " +
				   "delivered to " + source + ". " +
				   "Should have been delivered to " +
				   child + ". Maybe the native window " +
				   "system is bubbling events up the " +
				   "containment hierarchy.");
		break;
	      }
	  }
	
	/* ensure that the release event is delivered to the same
	   component as the press event. For most toolkits this is
	   only necessary for lightweight components, since the
	   underlying windowing system takes care of its heavyweight
	   components. */
	if (id == MouseEvent.MOUSE_PRESSED)
	  releaseTargets[button] = source;
      }
    
    
    if (source == heavySource)
      return event; // no change in event
    
    // print warning for heavyweights
    /* this warning can safely be removed if a toolkit that
       needs heavyweight redirection support is ever created. */
    if (!source.isLightweight())
      System.err.println("warning: redirecting to heavyweight");
    
    MouseEvent redirected = new MouseEvent(source, event.getID(),
					   event.getWhen(),
					   event.getModifiersEx(),
					   x, y,
					   event.getClickCount(),
					   event.isPopupTrigger());
    
    return redirected;
  }
  
  /**
   * Identifies the button number for an input event.
   * 
   * @returns the button number, or 0 if no button modifier was set
   * for the event.
   */
  int getButtonNumber(InputEvent event)
  {
    int modifiers = event.getModifiersEx();
    
    modifiers &=
      InputEvent.BUTTON1_DOWN_MASK |
      InputEvent.BUTTON2_DOWN_MASK |
      InputEvent.BUTTON3_DOWN_MASK;
    
    switch (modifiers)
      {
      case InputEvent.BUTTON1_DOWN_MASK:
	return 1;
      case InputEvent.BUTTON2_DOWN_MASK:
	return 2;
      case InputEvent.BUTTON3_DOWN_MASK:
	return 3;
      case 0:
	return 0;

      default:
	System.err.println("FIXME: multibutton event");
	return 0;
      }
  }
}