236 }
237
238 // We want to paint the icon after the text color is set since some icon painting depends on the correct
239 // graphics color being set
240 // See <rdar://problem/3792383> Menu icons missing in Java2D's Lines.Joins demo
241 // Paint the Icon
242 if (b.getIcon() != null) {
243 paintIcon(g, b, iconRect, isEnabled);
244 }
245
246 // Paint the Check using the current text color
247 if (checkIcon != null) {
248 paintCheck(g, b, checkIcon, checkIconRect);
249 }
250
251 // Draw the accelerator first in case the HTML renderer changes the color
252 if (keyString != null && !keyString.equals("")) {
253 final int yAccel = acceleratorRect.y + fm.getAscent();
254 if (modifiersString.equals("")) {
255 // just draw the keyString
256 SwingUtilities2.drawString(c, g, keyString, acceleratorRect.x, yAccel);
257 } else {
258 final int modifiers = accelerator.getModifiers();
259 int underlinedChar = 0;
260 if ((modifiers & ALT_GRAPH_MASK) > 0) underlinedChar = kUOptionGlyph; // This is a Java2 thing, we won't be getting kOptionGlyph
261 // The keyStrings should all line up, so always adjust the width by the same amount
262 // (if they're multi-char, they won't line up but at least they won't be cut off)
263 final int emWidth = Math.max(fm.charWidth('M'), SwingUtilities.computeStringWidth(fm, keyString));
264
265 if (leftToRight) {
266 g.setFont(acceleratorFont);
267 drawString(g, c, modifiersString, underlinedChar, acceleratorRect.x, yAccel, isEnabled, isSelected);
268 g.setFont(f);
269 SwingUtilities2.drawString(c, g, keyString, acceleratorRect.x + acceleratorRect.width - emWidth, yAccel);
270 } else {
271 final int xAccel = acceleratorRect.x + emWidth;
272 g.setFont(acceleratorFont);
273 drawString(g, c, modifiersString, underlinedChar, xAccel, yAccel, isEnabled, isSelected);
274 g.setFont(f);
275 SwingUtilities2.drawString(c, g, keyString, xAccel - fm.stringWidth(keyString), yAccel);
276 }
277 }
278 }
279
280 // Draw the Text
281 if (text != null && !text.equals("")) {
282 final View v = (View)c.getClientProperty(BasicHTML.propertyKey);
283 if (v != null) {
284 v.paint(g, textRect);
285 } else {
286 final int mnemonic = (AquaMnemonicHandler.isMnemonicHidden() ? -1 : model.getMnemonic());
287 drawString(g, c, text, mnemonic, textRect.x, textRect.y + fm.getAscent(), isEnabled, isSelected);
288 }
289 }
290
291 // Paint the Arrow
292 if (arrowIcon != null) {
293 paintArrow(g, b, model, arrowIcon, arrowIconRect);
294 }
295
408 /** Draw a string with the graphics g at location (x,y) just like g.drawString() would.
409 * The first occurrence of underlineChar in text will be underlined. The matching is
410 * not case sensitive.
411 */
412 public void drawString(final Graphics g, final JComponent c, final String text, final int underlinedChar, final int x, final int y, final boolean isEnabled, final boolean isSelected) {
413 char lc, uc;
414 int index = -1, lci, uci;
415
416 if (underlinedChar != '\0') {
417 uc = Character.toUpperCase((char)underlinedChar);
418 lc = Character.toLowerCase((char)underlinedChar);
419
420 uci = text.indexOf(uc);
421 lci = text.indexOf(lc);
422
423 if (uci == -1) index = lci;
424 else if (lci == -1) index = uci;
425 else index = (lci < uci) ? lci : uci;
426 }
427
428 SwingUtilities2.drawStringUnderlineCharAt(c, g, text, index, x, y);
429 }
430
431 /*
432 * Returns false if the component is a JMenu and it is a top
433 * level menu (on the menubar).
434 */
435 private static boolean isTopLevelMenu(final JMenuItem menuItem) {
436 return (menuItem instanceof JMenu) && (((JMenu)menuItem).isTopLevelMenu());
437 }
438
439 private String layoutMenuItem(final JMenuItem menuItem, final FontMetrics fm, final String text, final FontMetrics fmAccel, String keyString, final String modifiersString, final Icon icon, final Icon checkIcon, final Icon arrowIcon, final int verticalAlignment, final int horizontalAlignment, final int verticalTextPosition, final int horizontalTextPosition, final Rectangle viewR, final Rectangle iconR, final Rectangle textR, final Rectangle acceleratorR, final Rectangle checkIconR, final Rectangle arrowIconR, final int textIconGap, final int menuItemGap) {
440 // Force it to do "LEFT", then flip the rects if we're right-to-left
441 SwingUtilities.layoutCompoundLabel(menuItem, fm, text, icon, verticalAlignment, SwingConstants.LEFT, verticalTextPosition, horizontalTextPosition, viewR, iconR, textR, textIconGap);
442
443 final boolean acceleratorTextIsEmpty = (keyString == null) || keyString.equals("");
444
445 if (acceleratorTextIsEmpty) {
446 acceleratorR.width = acceleratorR.height = 0;
447 keyString = "";
448 } else {
|
236 }
237
238 // We want to paint the icon after the text color is set since some icon painting depends on the correct
239 // graphics color being set
240 // See <rdar://problem/3792383> Menu icons missing in Java2D's Lines.Joins demo
241 // Paint the Icon
242 if (b.getIcon() != null) {
243 paintIcon(g, b, iconRect, isEnabled);
244 }
245
246 // Paint the Check using the current text color
247 if (checkIcon != null) {
248 paintCheck(g, b, checkIcon, checkIconRect);
249 }
250
251 // Draw the accelerator first in case the HTML renderer changes the color
252 if (keyString != null && !keyString.equals("")) {
253 final int yAccel = acceleratorRect.y + fm.getAscent();
254 if (modifiersString.equals("")) {
255 // just draw the keyString
256 SwingUtilities2.getTextUIDrawing(c).drawString(c, g, keyString, acceleratorRect.x, yAccel);
257 } else {
258 final int modifiers = accelerator.getModifiers();
259 int underlinedChar = 0;
260 if ((modifiers & ALT_GRAPH_MASK) > 0) underlinedChar = kUOptionGlyph; // This is a Java2 thing, we won't be getting kOptionGlyph
261 // The keyStrings should all line up, so always adjust the width by the same amount
262 // (if they're multi-char, they won't line up but at least they won't be cut off)
263 final int emWidth = Math.max(fm.charWidth('M'), SwingUtilities.computeStringWidth(fm, keyString));
264
265 if (leftToRight) {
266 g.setFont(acceleratorFont);
267 drawString(g, c, modifiersString, underlinedChar, acceleratorRect.x, yAccel, isEnabled, isSelected);
268 g.setFont(f);
269 SwingUtilities2.getTextUIDrawing(c)
270 .drawString(c, g, keyString,
271 acceleratorRect.x + acceleratorRect.width - emWidth,
272 yAccel);
273 } else {
274 final int xAccel = acceleratorRect.x + emWidth;
275 g.setFont(acceleratorFont);
276 drawString(g, c, modifiersString, underlinedChar, xAccel, yAccel, isEnabled, isSelected);
277 g.setFont(f);
278 SwingUtilities2.getTextUIDrawing(c)
279 .drawString(c, g, keyString,
280 xAccel - fm.stringWidth(keyString),
281 yAccel);
282 }
283 }
284 }
285
286 // Draw the Text
287 if (text != null && !text.equals("")) {
288 final View v = (View)c.getClientProperty(BasicHTML.propertyKey);
289 if (v != null) {
290 v.paint(g, textRect);
291 } else {
292 final int mnemonic = (AquaMnemonicHandler.isMnemonicHidden() ? -1 : model.getMnemonic());
293 drawString(g, c, text, mnemonic, textRect.x, textRect.y + fm.getAscent(), isEnabled, isSelected);
294 }
295 }
296
297 // Paint the Arrow
298 if (arrowIcon != null) {
299 paintArrow(g, b, model, arrowIcon, arrowIconRect);
300 }
301
414 /** Draw a string with the graphics g at location (x,y) just like g.drawString() would.
415 * The first occurrence of underlineChar in text will be underlined. The matching is
416 * not case sensitive.
417 */
418 public void drawString(final Graphics g, final JComponent c, final String text, final int underlinedChar, final int x, final int y, final boolean isEnabled, final boolean isSelected) {
419 char lc, uc;
420 int index = -1, lci, uci;
421
422 if (underlinedChar != '\0') {
423 uc = Character.toUpperCase((char)underlinedChar);
424 lc = Character.toLowerCase((char)underlinedChar);
425
426 uci = text.indexOf(uc);
427 lci = text.indexOf(lc);
428
429 if (uci == -1) index = lci;
430 else if (lci == -1) index = uci;
431 else index = (lci < uci) ? lci : uci;
432 }
433
434 SwingUtilities2.getTextUIDrawing(c)
435 .drawStringUnderlineCharAt(c, g, text, index, x, y);
436 }
437
438 /*
439 * Returns false if the component is a JMenu and it is a top
440 * level menu (on the menubar).
441 */
442 private static boolean isTopLevelMenu(final JMenuItem menuItem) {
443 return (menuItem instanceof JMenu) && (((JMenu)menuItem).isTopLevelMenu());
444 }
445
446 private String layoutMenuItem(final JMenuItem menuItem, final FontMetrics fm, final String text, final FontMetrics fmAccel, String keyString, final String modifiersString, final Icon icon, final Icon checkIcon, final Icon arrowIcon, final int verticalAlignment, final int horizontalAlignment, final int verticalTextPosition, final int horizontalTextPosition, final Rectangle viewR, final Rectangle iconR, final Rectangle textR, final Rectangle acceleratorR, final Rectangle checkIconR, final Rectangle arrowIconR, final int textIconGap, final int menuItemGap) {
447 // Force it to do "LEFT", then flip the rects if we're right-to-left
448 SwingUtilities.layoutCompoundLabel(menuItem, fm, text, icon, verticalAlignment, SwingConstants.LEFT, verticalTextPosition, horizontalTextPosition, viewR, iconR, textR, textIconGap);
449
450 final boolean acceleratorTextIsEmpty = (keyString == null) || keyString.equals("");
451
452 if (acceleratorTextIsEmpty) {
453 acceleratorR.width = acceleratorR.height = 0;
454 keyString = "";
455 } else {
|