author | yan |
Mon, 06 Oct 2008 16:45:00 +0400 | |
changeset 1966 | 12a51fb0db0d |
parent 116 | 9c43d9eb1029 |
child 3938 | ef327bd847c0 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
116
9c43d9eb1029
6645856: static field XWindowPeer.defaultFont hides XWindow.defaultFont
son
parents:
2
diff
changeset
|
2 |
* Copyright 2002-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
package sun.awt.X11; |
|
26 |
||
27 |
import java.awt.*; |
|
28 |
import java.awt.peer.*; |
|
29 |
import java.awt.event.*; |
|
30 |
||
31 |
import java.lang.reflect.Field; |
|
32 |
import java.lang.reflect.Method; |
|
33 |
import java.lang.reflect.InvocationTargetException; |
|
34 |
||
35 |
import java.util.Vector; |
|
36 |
import java.util.logging.*; |
|
37 |
||
38 |
import sun.awt.SunToolkit; |
|
39 |
||
40 |
public class XPopupMenuPeer extends XMenuWindow implements PopupMenuPeer { |
|
41 |
||
42 |
/************************************************ |
|
43 |
* |
|
44 |
* Data members |
|
45 |
* |
|
46 |
************************************************/ |
|
47 |
private static Logger log = Logger.getLogger("sun.awt.X11.XBaseMenuWindow"); |
|
48 |
||
49 |
/* |
|
50 |
* Primary members |
|
51 |
*/ |
|
52 |
private XComponentPeer componentPeer; |
|
53 |
private PopupMenu popupMenuTarget; |
|
54 |
||
55 |
/* |
|
56 |
* If mouse button is clicked on item showing submenu |
|
57 |
* we have to hide its submenu. |
|
58 |
* This member saves the submenu under cursor |
|
59 |
* Only if it's showing |
|
60 |
*/ |
|
61 |
private XMenuPeer showingMousePressedSubmenu = null; |
|
62 |
||
63 |
/* |
|
64 |
* Painting constants |
|
65 |
*/ |
|
66 |
private final static int CAPTION_MARGIN_TOP = 4; |
|
67 |
private final static int CAPTION_SEPARATOR_HEIGHT = 6; |
|
68 |
||
69 |
/* |
|
70 |
* Menu's fields & methods |
|
71 |
*/ |
|
72 |
//Fix for 6184485: Popup menu is not disabled on XToolkit even when calling setEnabled (false) |
|
73 |
private final static Field f_enabled; |
|
74 |
//Fix for 6267144: PIT: Popup menu label is not shown, XToolkit |
|
75 |
private final static Field f_label; |
|
76 |
private final static Method m_getFont; |
|
77 |
private final static Field f_items; |
|
78 |
||
79 |
static { |
|
80 |
f_enabled = SunToolkit.getField(MenuItem.class, "enabled"); |
|
81 |
f_label = SunToolkit.getField(MenuItem.class, "label"); |
|
82 |
f_items = SunToolkit.getField(Menu.class, "items"); |
|
83 |
m_getFont = SunToolkit.getMethod(MenuComponent.class, "getFont_NoClientCode", null); |
|
84 |
} |
|
85 |
||
86 |
||
87 |
/************************************************ |
|
88 |
* |
|
89 |
* Construction |
|
90 |
* |
|
91 |
************************************************/ |
|
92 |
XPopupMenuPeer(PopupMenu target) { |
|
93 |
super(null); |
|
94 |
this.popupMenuTarget = target; |
|
95 |
} |
|
96 |
||
97 |
/************************************************ |
|
98 |
* |
|
99 |
* Implementaion of interface methods |
|
100 |
* |
|
101 |
************************************************/ |
|
102 |
/* |
|
103 |
* From MenuComponentPeer |
|
104 |
*/ |
|
105 |
public void setFont(Font f) { |
|
106 |
resetMapping(); |
|
107 |
setItemsFont(f); |
|
108 |
postPaintEvent(); |
|
109 |
} |
|
110 |
||
111 |
/* |
|
112 |
* From MenuItemPeer |
|
113 |
*/ |
|
114 |
public void setLabel(String label) { |
|
115 |
resetMapping(); |
|
116 |
postPaintEvent(); |
|
117 |
} |
|
118 |
||
119 |
||
120 |
public void setEnabled(boolean enabled) { |
|
121 |
postPaintEvent(); |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* DEPRECATED: Replaced by setEnabled(boolean). |
|
126 |
* @see java.awt.peer.MenuItemPeer |
|
127 |
*/ |
|
128 |
public void enable() { |
|
129 |
setEnabled( true ); |
|
130 |
} |
|
131 |
||
132 |
/** |
|
133 |
* DEPRECATED: Replaced by setEnabled(boolean). |
|
134 |
* @see java.awt.peer.MenuItemPeer |
|
135 |
*/ |
|
136 |
public void disable() { |
|
137 |
setEnabled( false ); |
|
138 |
} |
|
139 |
||
140 |
/* |
|
141 |
* From MenuPeer |
|
142 |
*/ |
|
143 |
/** |
|
144 |
* addSeparator routines are not used |
|
145 |
* in peers. Shared code invokes addItem("-") |
|
146 |
* for adding separators |
|
147 |
*/ |
|
148 |
public void addSeparator() { |
|
149 |
if (log.isLoggable(Level.FINER)) log.finer("addSeparator is not implemented"); |
|
150 |
} |
|
151 |
||
152 |
/* |
|
153 |
* From PopupMenuPeer |
|
154 |
*/ |
|
155 |
public void show(Event e) { |
|
156 |
target = (Component)e.target; |
|
157 |
// Get menus from the target. |
|
158 |
Vector targetItemVector = getMenuTargetItems(); |
|
159 |
if (targetItemVector != null) { |
|
160 |
reloadItems(targetItemVector); |
|
161 |
//Fix for 6287092: JCK15a: api/java_awt/interactive/event/EventTests.html#EventTest0015 fails, mustang |
|
162 |
Point tl = target.getLocationOnScreen(); |
|
163 |
Point pt = new Point(tl.x + e.x, tl.y + e.y); |
|
164 |
//Fixed 6266513: Incorrect key handling in XAWT popup menu |
|
165 |
//No item should be selected when showing popup menu |
|
166 |
if (!ensureCreated()) { |
|
167 |
return; |
|
168 |
} |
|
169 |
Dimension dim = getDesiredSize(); |
|
170 |
//Fix for 6267162: PIT: Popup Menu gets hidden below the screen when opened |
|
171 |
//near the periphery of the screen, XToolkit |
|
172 |
Rectangle bounds = getWindowBounds(pt, dim); |
|
173 |
reshape(bounds); |
|
174 |
xSetVisible(true); |
|
175 |
toFront(); |
|
176 |
selectItem(null, false); |
|
177 |
grabInput(); |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
/************************************************ |
|
182 |
* |
|
183 |
* Access to target's fields |
|
184 |
* |
|
185 |
************************************************/ |
|
186 |
||
187 |
//Fix for 6267144: PIT: Popup menu label is not shown, XToolkit |
|
188 |
Font getTargetFont() { |
|
189 |
if (popupMenuTarget == null) { |
|
116
9c43d9eb1029
6645856: static field XWindowPeer.defaultFont hides XWindow.defaultFont
son
parents:
2
diff
changeset
|
190 |
return XWindow.getDefaultFont(); |
2 | 191 |
} |
192 |
try { |
|
193 |
return (Font)m_getFont.invoke(popupMenuTarget, new Object[0]); |
|
194 |
} catch (IllegalAccessException e) { |
|
195 |
e.printStackTrace(); |
|
196 |
} catch (InvocationTargetException e) { |
|
197 |
e.printStackTrace(); |
|
198 |
} |
|
116
9c43d9eb1029
6645856: static field XWindowPeer.defaultFont hides XWindow.defaultFont
son
parents:
2
diff
changeset
|
199 |
return XWindow.getDefaultFont(); |
2 | 200 |
} |
201 |
||
202 |
String getTargetLabel() { |
|
203 |
if (target == null) { |
|
204 |
return ""; |
|
205 |
} |
|
206 |
try { |
|
207 |
String label = (String)f_label.get(popupMenuTarget); |
|
208 |
return (label == null) ? "" : label; |
|
209 |
} catch (IllegalAccessException e) { |
|
210 |
e.printStackTrace(); |
|
211 |
} |
|
212 |
return ""; |
|
213 |
} |
|
214 |
||
215 |
//Fix for 6184485: Popup menu is not disabled on XToolkit even when calling setEnabled (false) |
|
216 |
boolean isTargetEnabled() { |
|
217 |
if (popupMenuTarget == null) { |
|
218 |
return false; |
|
219 |
} |
|
220 |
try { |
|
221 |
return f_enabled.getBoolean(popupMenuTarget); |
|
222 |
} catch (IllegalAccessException e) { |
|
223 |
e.printStackTrace(); |
|
224 |
} |
|
225 |
return false; |
|
226 |
} |
|
227 |
||
228 |
Vector getMenuTargetItems() { |
|
229 |
try { |
|
230 |
return (Vector)f_items.get(popupMenuTarget); |
|
231 |
} catch (IllegalAccessException iae) { |
|
232 |
iae.printStackTrace(); |
|
233 |
return null; |
|
234 |
} |
|
235 |
} |
|
236 |
||
237 |
/************************************************ |
|
238 |
* |
|
239 |
* Utility functions |
|
240 |
* |
|
241 |
************************************************/ |
|
242 |
||
243 |
//Fix for 6267162: PIT: Popup Menu gets hidden below the screen when opened |
|
244 |
//near the periphery of the screen, XToolkit |
|
245 |
||
246 |
/** |
|
247 |
* Calculates placement of popup menu window |
|
248 |
* given origin in global coordinates and |
|
249 |
* size of menu window. Returns suggested |
|
250 |
* rectangle for menu window in global coordinates |
|
251 |
* @param origin the origin point specified in show() |
|
252 |
* function converted to global coordinates |
|
253 |
* @param windowSize the desired size of menu's window |
|
254 |
*/ |
|
255 |
protected Rectangle getWindowBounds(Point origin, Dimension windowSize) { |
|
256 |
Rectangle globalBounds = new Rectangle(origin.x, origin.y, 0, 0); |
|
257 |
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
|
258 |
Rectangle res; |
|
259 |
res = fitWindowRight(globalBounds, windowSize, screenSize); |
|
260 |
if (res != null) { |
|
261 |
return res; |
|
262 |
} |
|
263 |
res = fitWindowLeft(globalBounds, windowSize, screenSize); |
|
264 |
if (res != null) { |
|
265 |
return res; |
|
266 |
} |
|
267 |
res = fitWindowBelow(globalBounds, windowSize, screenSize); |
|
268 |
if (res != null) { |
|
269 |
return res; |
|
270 |
} |
|
271 |
res = fitWindowAbove(globalBounds, windowSize, screenSize); |
|
272 |
if (res != null) { |
|
273 |
return res; |
|
274 |
} |
|
275 |
return fitWindowToScreen(windowSize, screenSize); |
|
276 |
} |
|
277 |
||
278 |
/************************************************ |
|
279 |
* |
|
280 |
* Overriden XMenuWindow caption-painting functions |
|
281 |
* Necessary to fix 6267144: PIT: Popup menu label is not shown, XToolkit |
|
282 |
* |
|
283 |
************************************************/ |
|
284 |
/** |
|
285 |
* Returns height of menu window's caption. |
|
286 |
* Can be overriden for popup menus and tear-off menus |
|
287 |
*/ |
|
288 |
protected Dimension getCaptionSize() { |
|
289 |
String s = getTargetLabel(); |
|
290 |
if (s.equals("")) { |
|
291 |
return null; |
|
292 |
} |
|
293 |
Graphics g = getGraphics(); |
|
294 |
if (g == null) { |
|
295 |
return null; |
|
296 |
} |
|
297 |
try { |
|
298 |
g.setFont(getTargetFont()); |
|
299 |
FontMetrics fm = g.getFontMetrics(); |
|
300 |
String str = getTargetLabel(); |
|
301 |
int width = fm.stringWidth(str); |
|
302 |
int height = CAPTION_MARGIN_TOP + fm.getHeight() + CAPTION_SEPARATOR_HEIGHT; |
|
303 |
Dimension textDimension = new Dimension(width, height); |
|
304 |
return textDimension; |
|
305 |
} finally { |
|
306 |
g.dispose(); |
|
307 |
} |
|
308 |
} |
|
309 |
||
310 |
/** |
|
311 |
* Paints menu window's caption. |
|
312 |
* Can be overriden for popup menus and tear-off menus. |
|
313 |
* Default implementation does nothing |
|
314 |
*/ |
|
315 |
protected void paintCaption(Graphics g, Rectangle rect) { |
|
316 |
String s = getTargetLabel(); |
|
317 |
if (s.equals("")) { |
|
318 |
return; |
|
319 |
} |
|
320 |
g.setFont(getTargetFont()); |
|
321 |
FontMetrics fm = g.getFontMetrics(); |
|
322 |
String str = getTargetLabel(); |
|
323 |
int width = fm.stringWidth(str); |
|
324 |
int textx = rect.x + (rect.width - width) / 2; |
|
325 |
int texty = rect.y + CAPTION_MARGIN_TOP + fm.getAscent(); |
|
326 |
int sepy = rect.y + rect.height - CAPTION_SEPARATOR_HEIGHT / 2; |
|
327 |
g.setColor(isTargetEnabled() ? getForegroundColor() : getDisabledColor()); |
|
328 |
g.drawString(s, textx, texty); |
|
329 |
draw3DRect(g, rect.x, sepy, rect.width, 2, false); |
|
330 |
} |
|
331 |
||
332 |
/************************************************ |
|
333 |
* |
|
334 |
* Overriden XBaseMenuWindow functions |
|
335 |
* |
|
336 |
************************************************/ |
|
337 |
protected void doDispose() { |
|
338 |
super.doDispose(); |
|
339 |
XToolkit.targetDisposedPeer(popupMenuTarget, this); |
|
340 |
} |
|
341 |
||
342 |
protected void handleEvent(AWTEvent event) { |
|
343 |
switch(event.getID()) { |
|
344 |
case MouseEvent.MOUSE_PRESSED: |
|
345 |
case MouseEvent.MOUSE_RELEASED: |
|
346 |
case MouseEvent.MOUSE_CLICKED: |
|
347 |
case MouseEvent.MOUSE_MOVED: |
|
348 |
case MouseEvent.MOUSE_ENTERED: |
|
349 |
case MouseEvent.MOUSE_EXITED: |
|
350 |
case MouseEvent.MOUSE_DRAGGED: |
|
351 |
doHandleJavaMouseEvent((MouseEvent)event); |
|
352 |
break; |
|
353 |
case KeyEvent.KEY_PRESSED: |
|
354 |
case KeyEvent.KEY_RELEASED: |
|
355 |
doHandleJavaKeyEvent((KeyEvent)event); |
|
356 |
break; |
|
357 |
default: |
|
358 |
super.handleEvent(event); |
|
359 |
break; |
|
360 |
} |
|
361 |
} |
|
362 |
||
363 |
/************************************************ |
|
364 |
* |
|
365 |
* Overriden XWindow general-purpose functions |
|
366 |
* |
|
367 |
************************************************/ |
|
368 |
void ungrabInputImpl() { |
|
369 |
hide(); |
|
370 |
} |
|
371 |
||
372 |
/************************************************ |
|
373 |
* |
|
374 |
* Overriden XWindow keyboard processing |
|
375 |
* |
|
376 |
************************************************/ |
|
377 |
||
378 |
/* |
|
379 |
* In previous version keys were handled in handleKeyPress. |
|
380 |
* Now we override this function do disable F10 explicit |
|
381 |
* processing. All processing is done using KeyEvent. |
|
382 |
*/ |
|
383 |
public void handleKeyPress(XEvent xev) { |
|
384 |
XKeyEvent xkey = xev.get_xkey(); |
|
385 |
if (log.isLoggable(Level.FINE)) { |
|
386 |
log.fine(xkey.toString()); |
|
387 |
} |
|
388 |
if (isEventDisabled(xev)) { |
|
389 |
return; |
|
390 |
} |
|
391 |
final Component currentSource = (Component)getEventSource(); |
|
392 |
handleKeyPress(xkey); |
|
393 |
} |
|
394 |
||
395 |
} |