0N/A/*
2362N/A * Copyright (c) 2002, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.awt.X11;
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.peer.*;
0N/Aimport java.awt.event.*;
0N/Aimport java.awt.image.BufferedImage;
0N/Aimport javax.swing.plaf.basic.BasicGraphicsUtils;
0N/Aimport java.awt.geom.AffineTransform;
0N/A
1696N/Aimport sun.util.logging.PlatformLogger;
0N/A
0N/Aclass XCheckboxPeer extends XComponentPeer implements CheckboxPeer {
0N/A
1696N/A private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XCheckboxPeer");
0N/A
0N/A private static final Insets focusInsets = new Insets(0,0,0,0);
0N/A private static final Insets borderInsets = new Insets(2,2,2,2);
0N/A private static final int checkBoxInsetFromText = 2;
0N/A
0N/A //The check mark is less common than a plain "depressed" button,
0N/A //so don't use the checkmark.
0N/A // The checkmark shape:
0N/A private static final double MASTER_SIZE = 128.0;
0N/A private static final Polygon MASTER_CHECKMARK = new Polygon(
0N/A new int[] {1, 25,56,124,124,85, 64}, // X-coords
0N/A new int[] {59,35,67, 0, 12,66,123}, // Y-coords
0N/A 7);
0N/A
0N/A private Shape myCheckMark;
0N/A
0N/A private Color focusColor = SystemColor.windowText;
0N/A
0N/A private boolean pressed;
0N/A private boolean armed;
0N/A private boolean selected;
0N/A
0N/A private Rectangle textRect;
0N/A private Rectangle focusRect;
0N/A private int checkBoxSize;
0N/A private int cbX;
0N/A private int cbY;
0N/A
0N/A String label;
0N/A CheckboxGroup checkBoxGroup;
0N/A
0N/A XCheckboxPeer(Checkbox target) {
0N/A super(target);
0N/A pressed = false;
0N/A armed = false;
0N/A selected = target.getState();
0N/A label = target.getLabel();
0N/A if ( label == null ) {
0N/A label = "";
0N/A }
0N/A checkBoxGroup = target.getCheckboxGroup();
0N/A updateMotifColors(getPeerBackground());
0N/A }
0N/A
0N/A public void preInit(XCreateWindowParams params) {
0N/A // Put this here so it is executed before layout() is called from
0N/A // setFont() in XComponent.postInit()
0N/A textRect = new Rectangle();
0N/A focusRect = new Rectangle();
0N/A super.preInit(params);
0N/A }
0N/A
0N/A public boolean isFocusable() { return true; }
0N/A
0N/A public void focusGained(FocusEvent e) {
0N/A // TODO: only need to paint the focus bit
0N/A super.focusGained(e);
0N/A repaint();
0N/A }
0N/A
0N/A public void focusLost(FocusEvent e) {
0N/A // TODO: only need to paint the focus bit?
0N/A super.focusLost(e);
0N/A repaint();
0N/A }
0N/A
0N/A
0N/A void handleJavaKeyEvent(KeyEvent e) {
0N/A int i = e.getID();
0N/A switch (i) {
0N/A case KeyEvent.KEY_PRESSED:
0N/A keyPressed(e);
0N/A break;
0N/A case KeyEvent.KEY_RELEASED:
0N/A keyReleased(e);
0N/A break;
0N/A case KeyEvent.KEY_TYPED:
0N/A keyTyped(e);
0N/A break;
0N/A }
0N/A }
0N/A
0N/A public void keyTyped(KeyEvent e) {}
0N/A
0N/A public void keyPressed(KeyEvent e) {
0N/A if (e.getKeyCode() == KeyEvent.VK_SPACE)
0N/A {
0N/A //pressed=true;
0N/A //armed=true;
0N/A //selected=!selected;
0N/A action(!selected);
0N/A //repaint(); // Gets the repaint from action()
0N/A }
0N/A
0N/A }
0N/A
0N/A public void keyReleased(KeyEvent e) {}
0N/A
0N/A public void setLabel(java.lang.String label) {
0N/A if ( label == null ) {
0N/A this.label = "";
0N/A } else {
0N/A this.label = label;
0N/A }
0N/A layout();
0N/A repaint();
0N/A }
0N/A
0N/A void handleJavaMouseEvent(MouseEvent e) {
0N/A super.handleJavaMouseEvent(e);
0N/A int i = e.getID();
0N/A switch (i) {
0N/A case MouseEvent.MOUSE_PRESSED:
0N/A mousePressed(e);
0N/A break;
0N/A case MouseEvent.MOUSE_RELEASED:
0N/A mouseReleased(e);
0N/A break;
0N/A case MouseEvent.MOUSE_ENTERED:
0N/A mouseEntered(e);
0N/A break;
0N/A case MouseEvent.MOUSE_EXITED:
0N/A mouseExited(e);
0N/A break;
0N/A case MouseEvent.MOUSE_CLICKED:
0N/A mouseClicked(e);
0N/A break;
0N/A }
0N/A }
0N/A
0N/A public void mousePressed(MouseEvent e) {
0N/A if (XToolkit.isLeftMouseButton(e)) {
0N/A Checkbox cb = (Checkbox) e.getSource();
0N/A
0N/A if (cb.contains(e.getX(), e.getY())) {
1696N/A if (log.isLoggable(PlatformLogger.FINER)) {
0N/A log.finer("mousePressed() on " + target.getName() + " : armed = " + armed + ", pressed = " + pressed
0N/A + ", selected = " + selected + ", enabled = " + isEnabled());
0N/A }
0N/A if (!isEnabled()) {
0N/A // Disabled buttons ignore all input...
0N/A return;
0N/A }
0N/A if (!armed) {
0N/A armed = true;
0N/A }
0N/A pressed = true;
0N/A repaint();
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void mouseReleased(MouseEvent e) {
1696N/A if (log.isLoggable(PlatformLogger.FINER)) {
0N/A log.finer("mouseReleased() on " + target.getName() + ": armed = " + armed + ", pressed = " + pressed
0N/A + ", selected = " + selected + ", enabled = " + isEnabled());
0N/A }
0N/A boolean sendEvent = false;
0N/A if (XToolkit.isLeftMouseButton(e)) {
0N/A // TODO: Multiclick Threshold? - see BasicButtonListener.java
0N/A if (armed) {
0N/A //selected = !selected;
0N/A // send action event
0N/A //action(e.getWhen(),e.getModifiers());
0N/A sendEvent = true;
0N/A }
0N/A pressed = false;
0N/A armed = false;
0N/A if (sendEvent) {
0N/A action(!selected); // Also gets repaint in action()
0N/A }
0N/A else {
0N/A repaint();
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void mouseEntered(MouseEvent e) {
1696N/A if (log.isLoggable(PlatformLogger.FINER)) {
0N/A log.finer("mouseEntered() on " + target.getName() + ": armed = " + armed + ", pressed = " + pressed
0N/A + ", selected = " + selected + ", enabled = " + isEnabled());
0N/A }
0N/A if (pressed) {
0N/A armed = true;
0N/A repaint();
0N/A }
0N/A }
0N/A
0N/A public void mouseExited(MouseEvent e) {
1696N/A if (log.isLoggable(PlatformLogger.FINER)) {
0N/A log.finer("mouseExited() on " + target.getName() + ": armed = " + armed + ", pressed = " + pressed
0N/A + ", selected = " + selected + ", enabled = " + isEnabled());
0N/A }
0N/A if (armed) {
0N/A armed = false;
0N/A repaint();
0N/A }
0N/A }
0N/A
0N/A public void mouseClicked(MouseEvent e) {}
0N/A
0N/A public Dimension getMinimumSize() {
0N/A /*
0N/A * Spacing (number of pixels between check mark and label text) is
0N/A * currently set to 0, but in case it ever changes we have to add
0N/A * it. 8 is a heuristic number. Indicator size depends on font
0N/A * height, so we don't need to include it in checkbox's height
0N/A * calculation.
0N/A */
0N/A FontMetrics fm = getFontMetrics(getPeerFont());
0N/A
0N/A int wdth = fm.stringWidth(label) + getCheckboxSize(fm) + (2 * checkBoxInsetFromText) + 8;
0N/A int hght = Math.max(fm.getHeight() + 8, 15);
0N/A
0N/A return new Dimension(wdth, hght);
0N/A }
0N/A
0N/A private int getCheckboxSize(FontMetrics fm) {
0N/A // the motif way of sizing is a bit inscutible, but this
0N/A // is a fair approximation
0N/A return (fm.getHeight() * 76 / 100) - 1;
0N/A }
0N/A
0N/A public void setBackground(Color c) {
0N/A updateMotifColors(c);
0N/A super.setBackground(c);
0N/A }
0N/A
0N/A /*
0N/A * Layout the checkbox/radio button and text label
0N/A */
0N/A public void layout() {
0N/A Dimension size = getPeerSize();
0N/A Font f = getPeerFont();
0N/A FontMetrics fm = getFontMetrics(f);
0N/A String text = label;
0N/A
0N/A checkBoxSize = getCheckboxSize(fm);
0N/A
0N/A // Note - Motif appears to use an left inset that is slightly
0N/A // scaled to the checkbox/font size.
0N/A cbX = borderInsets.left + checkBoxInsetFromText;
0N/A cbY = size.height / 2 - checkBoxSize / 2;
0N/A int minTextX = borderInsets.left + 2 * checkBoxInsetFromText + checkBoxSize;
0N/A // FIXME: will need to account for alignment?
0N/A // FIXME: call layout() on alignment changes
0N/A //textRect.width = fm.stringWidth(text);
0N/A textRect.width = fm.stringWidth(text == null ? "" : text);
0N/A textRect.height = fm.getHeight();
0N/A
0N/A textRect.x = Math.max(minTextX, size.width / 2 - textRect.width / 2);
0N/A textRect.y = (size.height - textRect.height) / 2;
0N/A
0N/A focusRect.x = focusInsets.left;
0N/A focusRect.y = focusInsets.top;
0N/A focusRect.width = size.width-(focusInsets.left+focusInsets.right)-1;
0N/A focusRect.height = size.height-(focusInsets.top+focusInsets.bottom)-1;
0N/A
0N/A double fsize = (double) checkBoxSize;
0N/A myCheckMark = AffineTransform.getScaleInstance(fsize / MASTER_SIZE, fsize / MASTER_SIZE).createTransformedShape(MASTER_CHECKMARK);
0N/A
0N/A }
0N/A
0N/A public void paint(Graphics g) {
0N/A if (g != null) {
0N/A //layout();
0N/A Dimension size = getPeerSize();
0N/A Font f = getPeerFont();
0N/A
0N/A flush();
0N/A g.setColor(getPeerBackground()); // erase the existing button
0N/A g.fillRect(0,0, size.width, size.height);
0N/A
0N/A if (label != null) {
0N/A g.setFont(f);
0N/A paintText(g, textRect, label);
0N/A }
0N/A
0N/A if (hasFocus()) {
0N/A paintFocus(g,
0N/A focusRect.x,
0N/A focusRect.y,
0N/A focusRect.width,
0N/A focusRect.height);
0N/A }
0N/A
0N/A // Paint the checkbox or radio button
0N/A if (checkBoxGroup == null) {
0N/A paintCheckbox(g, cbX, cbY, checkBoxSize, checkBoxSize);
0N/A }
0N/A else {
0N/A paintRadioButton(g, cbX, cbY, checkBoxSize, checkBoxSize);
0N/A }
0N/A
0N/A }
0N/A flush();
0N/A }
0N/A
0N/A // You'll note this looks suspiciously like paintBorder
0N/A public void paintCheckbox(Graphics g,
0N/A int x, int y, int w, int h) {
0N/A boolean useBufferedImage = false;
0N/A BufferedImage buffer = null;
0N/A Graphics2D g2 = null;
0N/A int rx = x;
0N/A int ry = y;
0N/A if (!(g instanceof Graphics2D)) {
0N/A // Fix for 5045936. While printing, g is an instance of
0N/A // sun.print.ProxyPrintGraphics which extends Graphics. So
0N/A // we use a separate buffered image and its graphics is
0N/A // always Graphics2D instance
0N/A buffer = graphicsConfig.createCompatibleImage(w, h);
0N/A g2 = buffer.createGraphics();
0N/A useBufferedImage = true;
0N/A rx = 0;
0N/A ry = 0;
0N/A }
0N/A else {
0N/A g2 = (Graphics2D)g;
0N/A }
0N/A try {
0N/A drawMotif3DRect(g2, rx, ry, w-1, h-1, armed | selected);
0N/A
0N/A // then paint the check
0N/A g2.setColor((armed | selected) ? selectColor : getPeerBackground());
0N/A g2.fillRect(rx+1, ry+1, w-2, h-2);
0N/A
0N/A if (armed | selected) {
0N/A //Paint the check
0N/A
0N/A // FIXME: is this the right color?
0N/A g2.setColor(getPeerForeground());
0N/A
0N/A AffineTransform af = g2.getTransform();
0N/A g2.setTransform(AffineTransform.getTranslateInstance(rx,ry));
0N/A g2.fill(myCheckMark);
0N/A g2.setTransform(af);
0N/A }
0N/A } finally {
0N/A if (useBufferedImage) {
0N/A g2.dispose();
0N/A }
0N/A }
0N/A if (useBufferedImage) {
0N/A g.drawImage(buffer, x, y, null);
0N/A }
0N/A }
0N/A public void setFont(Font f) {
0N/A super.setFont(f);
0N/A target.repaint();
0N/A }
0N/A
0N/A public void paintRadioButton(Graphics g, int x, int y, int w, int h) {
0N/A
0N/A g.setColor((armed | selected) ? darkShadow : lightShadow);
0N/A g.drawArc(x-1, y-1, w+2, h+2, 45, 180);
0N/A
0N/A g.setColor((armed | selected) ? lightShadow : darkShadow);
0N/A g.drawArc(x-1, y-1, w+2, h+2, 45, -180);
0N/A
0N/A if (armed | selected) {
0N/A g.setColor(selectColor);
0N/A g.fillArc(x+1, y+1, w-1, h-1, 0, 360);
0N/A }
0N/A }
0N/A
0N/A protected void paintText(Graphics g, Rectangle textRect, String text) {
0N/A FontMetrics fm = g.getFontMetrics();
0N/A
0N/A int mnemonicIndex = -1;
0N/A
0N/A if(isEnabled()) {
0N/A /*** paint the text normally */
0N/A g.setColor(getPeerForeground());
0N/A BasicGraphicsUtils.drawStringUnderlineCharAt(g,text,mnemonicIndex , textRect.x , textRect.y + fm.getAscent() );
0N/A }
0N/A else {
0N/A /*** paint the text disabled ***/
0N/A g.setColor(getPeerBackground().brighter());
0N/A
0N/A BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
0N/A textRect.x, textRect.y + fm.getAscent());
0N/A g.setColor(getPeerBackground().darker());
0N/A BasicGraphicsUtils.drawStringUnderlineCharAt(g,text, mnemonicIndex,
0N/A textRect.x - 1, textRect.y + fm.getAscent() - 1);
0N/A }
0N/A }
0N/A
0N/A // TODO: copied directly from XButtonPeer. Should probabaly be shared
0N/A protected void paintFocus(Graphics g, int x, int y, int w, int h) {
0N/A g.setColor(focusColor);
0N/A g.drawRect(x,y,w,h);
0N/A }
0N/A
0N/A public void setState(boolean state) {
0N/A if (selected != state) {
0N/A selected = state;
0N/A repaint();
0N/A }
0N/A }
0N/A public void setCheckboxGroup(CheckboxGroup g) {
0N/A // If changed from grouped/ungrouped, need to repaint()
0N/A checkBoxGroup = g;
0N/A repaint();
0N/A }
0N/A
0N/A // NOTE: This method is called by privileged threads.
0N/A // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
0N/A // From MCheckboxPeer
0N/A void action(boolean state) {
0N/A final Checkbox cb = (Checkbox)target;
0N/A final boolean newState = state;
0N/A XToolkit.executeOnEventHandlerThread(cb, new Runnable() {
0N/A public void run() {
0N/A CheckboxGroup cbg = checkBoxGroup;
0N/A // Bugid 4039594. If this is the current Checkbox in
0N/A // a CheckboxGroup, then return to prevent deselection.
0N/A // Otherwise, it's logical state will be turned off,
0N/A // but it will appear on.
0N/A if ((cbg != null) && (cbg.getSelectedCheckbox() == cb) &&
0N/A cb.getState()) {
0N/A //inUpCall = false;
0N/A cb.setState(true);
0N/A return;
0N/A }
0N/A // All clear - set the new state
0N/A cb.setState(newState);
0N/A notifyStateChanged(newState);
0N/A }
0N/A });
0N/A }
0N/A
0N/A void notifyStateChanged(boolean state) {
0N/A Checkbox cb = (Checkbox) target;
0N/A ItemEvent e = new ItemEvent(cb,
0N/A ItemEvent.ITEM_STATE_CHANGED,
0N/A cb.getLabel(),
0N/A state ? ItemEvent.SELECTED : ItemEvent.DESELECTED);
0N/A postEvent(e);
0N/A }
0N/A}