diff options
author | Till Brychcy | 2017-04-18 21:39:32 +0000 |
---|---|---|
committer | Lakshmi Shanmugam | 2017-09-07 10:40:09 +0000 |
commit | 0650943303854f471afb27787b61aa951ffc6d5e (patch) | |
tree | ae230daae5dae0c52827d223ecb47037d57ad5cf /bundles/org.eclipse.swt/Eclipse SWT | |
parent | bb0fbf37910ce31e91c0b4095fcfe53b905817bc (diff) | |
download | eclipse.platform.swt-0650943303854f471afb27787b61aa951ffc6d5e.tar.gz eclipse.platform.swt-0650943303854f471afb27787b61aa951ffc6d5e.tar.xz eclipse.platform.swt-0650943303854f471afb27787b61aa951ffc6d5e.zip |
Bug 505835 - [Cocoa] Button, do not respect foreground and background
color on MAC
Perform custom coloring for Button background.
Change-Id: I34084136e206e08e8a97fe8198720a093abdd589
Signed-off-by: Till Brychcy <register.eclipse@brychcy.de>
Diffstat (limited to 'bundles/org.eclipse.swt/Eclipse SWT')
4 files changed, 155 insertions, 3 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Button.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Button.java index c570ace79c..8055cfef8b 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Button.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Button.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2012 IBM Corporation and others. + * Copyright (c) 2000, 2017 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -54,6 +54,8 @@ public class Button extends Control { */ private boolean lastRadioState; + private static final double /*float*/ [] DEFAULT_DISABLED_FOREGROUND = new double /*float*/ [] { 0.6745f, 0.6745f, 0.6745f, 1.0f }; + static final int EXTRA_HEIGHT = 2; static final int EXTRA_WIDTH = 6; static final int IMAGE_GAP = 2; @@ -329,6 +331,108 @@ void drawImageWithFrameInView (long /*int*/ id, long /*int*/ sel, long /*int*/ i super.drawImageWithFrameInView(id, sel, image, rect, view); } +private static NSRect smallerRect (NSRect cellFrame, double dx, double dy1, double dy2, float lineWidth) { + if (lineWidth == 2) { + dx -= 0.5; + dy1 -= 0.5; + dy2 -= 0.5; + } else if (lineWidth == 0.5) { + dx += 0.25; + dy1 += 0.25; + dy2 += 0.25; + } else if (lineWidth == 0.75) { + dx += 0.125; + dy1 += 0.125; + dy2 += 0.125; + } + NSRect result = new NSRect(); + result.x = cellFrame.x + dx; + result.y = cellFrame.y + dy1; + result.width = cellFrame.width - 2 * dx; + result.height = cellFrame.height - dy1 - dy2; + return result; +} + +@Override +void drawBezelWithFrame_inView (long /*int*/ id, long /*int*/ sel, NSRect cellFrame, long /*int*/ viewid) { + if (this.background != null) { + NSButton button = (NSButton) view; + + final boolean isHighlighted = (style & SWT.TOGGLE) == 0 ? button.isHighlighted() : ((NSButton) view).state() == OS.NSOnState; + + final NSWindow window = button.window(); + final NSButtonCell defaultButtonCell = window == null ? null : window.defaultButtonCell(); + final boolean isDefault = defaultButtonCell != null && defaultButtonCell.id == id; + + double /*float*/ [] borderRGB = getLighterOrDarkerColor(this.background, 0.3, luma(background) >= 0.5); + if (isHighlighted && (style & SWT.FLAT) != 0) { + borderRGB = getLighterOrDarkerColor(borderRGB, 0.2, true); + } + + NSGraphicsContext gc = NSGraphicsContext.currentContext(); + gc.saveGraphicsState(); + + NSBezierPath path; + final float lineWidth; + if (isDefault) { + lineWidth = 2f; + } else if ((style & SWT.FLAT) == 0) { + lineWidth = 0.75f; + } else { + lineWidth = 1f; + } + final long bezelStyle = button.bezelStyle(); + if (bezelStyle == OS.NSRoundedBezelStyle) { + NSRect rect2 = smallerRect(cellFrame, 6.5f, 4.5f, 7.5f, lineWidth); + path = NSBezierPath.bezierPathWithRoundedRect(rect2, 3, 3); + } else if (bezelStyle == OS.NSRegularSquareBezelStyle) { + NSRect rect2 = smallerRect(cellFrame, 2.5f, 2.5f, 3.5f, lineWidth); + path = NSBezierPath.bezierPathWithRoundedRect(rect2, 3, 3); + } else { + NSRect rect2 = smallerRect(cellFrame, 0.5f, 0.5f, 0.5f, lineWidth); + path = NSBezierPath.bezierPathWithRect(rect2); + } + if (!isHighlighted) { + double /*float*/ [] backgroundRGB = this.background; + NSColor backgroundNSColor = NSColor.colorWithDeviceRed(backgroundRGB[0], backgroundRGB[1], backgroundRGB[2], 1f); + + if((style & SWT.FLAT) == 0) { + double /*float*/ [] topRGB = getLighterOrDarkerColor(this.background, 0.2, false); + NSColor topColor = NSColor.colorWithDeviceRed(topRGB[0], topRGB[1], topRGB[2], 1f); + double /*float*/ [] bottomRGB = getLighterOrDarkerColor(this.background, 0.1, true); + NSColor bottomColor = NSColor.colorWithDeviceRed(bottomRGB[0], bottomRGB[1], bottomRGB[2], 1f); + + NSMutableArray ma = NSMutableArray.arrayWithCapacity(4); + ma.addObject(topColor); + ma.addObject(backgroundNSColor); + ma.addObject(backgroundNSColor); + ma.addObject(bottomColor); + NSGradient gradient = ((NSGradient) new NSGradient().alloc()).initWithColors(ma); + gradient.drawInBezierPath(path, 90); + gradient.release(); + } else { + backgroundNSColor.setFill(); + path.fill(); + } + } else { + double /*float*/ [] colorRGB0 = getLighterOrDarkerColor(this.background, 0.1f, true); + NSColor color0 = NSColor.colorWithDeviceRed(colorRGB0[0], colorRGB0[1], colorRGB0[2], 1f); + color0.setFill(); + path.fill(); + } + + // draw border + path.setLineWidth(lineWidth); + NSColor borderNSColor = NSColor.colorWithDeviceRed(borderRGB[0], borderRGB[1], borderRGB[2], 1f); + borderNSColor.setStroke(); + path.stroke(); + + gc.restoreGraphicsState(); + return; + } + super.drawBezelWithFrame_inView(id, sel, cellFrame, viewid); +} + @Override void drawInteriorWithFrame_inView (long /*int*/ id, long /*int*/ sel, NSRect cellRect, long /*int*/ viewid) { if ((style & (SWT.CHECK|SWT.RADIO)) != 0 && backgroundImage != null) { @@ -372,11 +476,20 @@ void drawInteriorWithFrame_inView (long /*int*/ id, long /*int*/ sel, NSRect cel @Override NSRect drawTitleWithFrameInView (long /*int*/ id, long /*int*/ sel, long /*int*/ title, NSRect titleRect, long /*int*/ view) { boolean wrap = (style & SWT.WRAP) != 0 && text.length() != 0; + boolean isEnabled = isEnabled(); if (wrap) { NSSize wrapSize = new NSSize(); wrapSize.width = titleRect.width; wrapSize.height = MAX_SIZE; - NSAttributedString attribStr = createString(text, null, foreground, style, true, true, true); + final double[] foreground2; + if (isEnabled) { + foreground2 = foreground; + } else if (foreground == null) { + foreground2 = DEFAULT_DISABLED_FOREGROUND; + } else { + foreground2 = getLighterOrDarkerColor(foreground, 0.5, luma(foreground) >= 0.5); + } + NSAttributedString attribStr = createString(text, null, foreground2, style, true, true, true); NSRect rect = attribStr.boundingRectWithSize(wrapSize, OS.NSStringDrawingUsesLineFragmentOrigin); switch (style & (SWT.LEFT | SWT.RIGHT | SWT.CENTER)) { case SWT.LEFT: @@ -394,6 +507,12 @@ NSRect drawTitleWithFrameInView (long /*int*/ id, long /*int*/ sel, long /*int*/ attribStr.release (); return rect; } + if (!isEnabled && foreground != null) { + NSAttributedString attribStr = createString(text, null, getLighterOrDarkerColor(foreground, 0.5, luma(foreground) >= 0.5), style, false, true, true); + final NSRect result = super.drawTitleWithFrameInView(id, sel, attribStr.id, titleRect, view); + attribStr.release(); + return result; + } return super.drawTitleWithFrameInView(id, sel, title, titleRect, view); } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Control.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Control.java index 9cc52654fe..fc65b4ba6a 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Control.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Control.java @@ -5206,4 +5206,28 @@ void updateLayout (boolean all) { /* Do nothing */ } +static double /*float*/ calcDiff (double /*float*/ component, double /*float*/ factor, boolean wantDarker) { + if (wantDarker) { + return component * -1 * factor; + } else { + return (1f - component) * factor; + } +} + +static double /*float*/ [] getLighterOrDarkerColor (double /*float*/ [] pixel, double /*float*/ factor, boolean wantDarker) { + double /*float*/ red = pixel[0]; + double /*float*/ green = pixel[1]; + double /*float*/ blue = pixel[2]; + red += calcDiff(red, factor, wantDarker); + green += calcDiff(green, factor, wantDarker); + blue += calcDiff(blue, factor, wantDarker); + return new double /*float*/ [] { red, green, blue, pixel[3] }; +} + +/** + * @return luma according to ITU BT.709: Y = 0.2126 R + 0.7152 G + 0.0722 B + */ +static double luma (double[] rgbColor) { + return 0.2126f * rgbColor[0] + 0.7152f * rgbColor[1] + 0.0722f * rgbColor[2]; +} } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java index e9f51eac3a..d02bab3441 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2016 IBM Corporation and others. + * Copyright (c) 2000, 2017 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -2447,6 +2447,7 @@ void initClasses () { long /*int*/ isFlippedProc = OS.isFlipped_CALLBACK(); long /*int*/ drawRectProc = OS.CALLBACK_drawRect_(proc3); long /*int*/ drawInteriorWithFrameInViewProc = OS.CALLBACK_drawInteriorWithFrame_inView_ (proc4); + long /*int*/ drawBezelWithFrameInViewProc = OS.CALLBACK_drawBezelWithFrame_inView_ (proc4); long /*int*/ drawWithExpansionFrameProc = OS.CALLBACK_drawWithExpansionFrame_inView_ (proc4); long /*int*/ imageRectForBoundsProc = OS.CALLBACK_imageRectForBounds_ (proc3); long /*int*/ titleRectForBoundsProc = OS.CALLBACK_titleRectForBounds_ (proc3); @@ -2514,6 +2515,7 @@ void initClasses () { OS.class_addMethod (cls, OS.sel_drawImage_withFrame_inView_, drawImageWithFrameInViewProc, "@:@{NSRect}@"); OS.class_addMethod (cls, OS.sel_drawTitle_withFrame_inView_, drawTitleWithFrameInViewProc, "@:@{NSRect}@"); OS.class_addMethod(cls, OS.sel_drawInteriorWithFrame_inView_, drawInteriorWithFrameInViewProc, "@:{NSRect}@"); + OS.class_addMethod(cls, OS.sel_drawBezelWithFrame_inView_, drawBezelWithFrameInViewProc, "@:{NSRect}@"); OS.class_addMethod(cls, OS.sel_titleRectForBounds_, titleRectForBoundsProc, "@:{NSRect}"); OS.class_addMethod(cls, OS.sel_cellSizeForBounds_, cellSizeForBoundsProc, "@:{NSRect}"); if (OS.VERSION_MMB >= OS.VERSION_MMB(10, 10, 0)) { @@ -6061,6 +6063,10 @@ static long /*int*/ windowProc(long /*int*/ id, long /*int*/ sel, long /*int*/ a NSRect rect = new NSRect(); OS.memmove(rect, arg0, NSRect.sizeof); widget.drawWithExpansionFrame_inView (id, sel, rect, arg1); + } else if (sel == OS.sel_drawBezelWithFrame_inView_) { + NSRect rect = new NSRect (); + OS.memmove (rect, arg0, NSRect.sizeof); + widget.drawBezelWithFrame_inView (id, sel, rect, arg1); } else if (sel == OS.sel_accessibilityAttributeValue_forParameter_) { return widget.accessibilityAttributeValue_forParameter(id, sel, arg0, arg1); } else if (sel == OS.sel_tableView_didClickTableColumn_) { diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Widget.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Widget.java index 8b54afb794..9d14565027 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Widget.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Widget.java @@ -732,6 +732,9 @@ void drawInteriorWithFrame_inView (long /*int*/ id, long /*int*/ sel, NSRect cel callSuper(id, sel, cellFrame, view); } +void drawBezelWithFrame_inView (long /*int*/ id, long /*int*/ sel, NSRect cellFrame, long /*int*/ view) { + callSuper(id, sel, cellFrame, view); +} void drawLabelInRect(long /*int*/ id, long /*int*/ sel, boolean shouldTruncateLabel, NSRect rect) { objc_super super_struct = new objc_super(); |