1/* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22/* 23 * ident "%Z%%M% %I% %E% SMI" 24 * 25 * Copyright (c) 2000 by Sun Microsystems, Inc. 26 * All rights reserved. 27 */ 28 29/* 30 * Copyright (C) 1996 Active Software, Inc. 31 * All rights reserved. 32 * 33 * @(#) ImageButton.java 1.16 - last change made 07/23/97 34 */ 35 36package sunsoft.jws.visual.rt.awt; 37 38import sunsoft.jws.visual.rt.base.DesignerAccess; 39import sunsoft.jws.visual.rt.base.Global; 40import java.awt.*; 41 42/** 43 * An image button (a 3D rect around an image.) It greys itself out 44 * when disabled and inverts the 3D rect and 45 * moves its image when depressed. 46 * 47 * @(#) @(#) ImageButton.java 1.16 - last change made 07/23/97 48 */ 49public class ImageButton extends ImageLabel { 50 private int lineWidth = 2; // thickness of 3D line around button 51 private int pressMovement = 1; // distance image moves 52 // when button pressed 53 protected boolean depressed; 54 55 public ImageButton(Image img) { 56 this(img, 20, 20); 57 } 58 59 public ImageButton(Image img, int w, int h) { 60 super(img, w, h); 61 depressed = false; 62 padWidth = lineWidth + 2; 63 } 64 65 public void setPadWidth(int w) { 66 super.setPadWidth(w + lineWidth); 67 } 68 69 public int getPadWidth() { 70 return (super.getPadWidth() - lineWidth); 71 } 72 73 public void setLineWidth(int w) { 74 int oldPadWidth = getPadWidth(); 75 lineWidth = w; 76 setPadWidth(oldPadWidth); 77 } 78 79 public int getLineWidth() { 80 return (lineWidth); 81 } 82 83 public void setPressMovement(int p) { 84 if (p != pressMovement) { 85 pressMovement = p; 86 if (depressed) { 87 Graphics g = getGraphics(); 88 if (g != null) { 89 Dimension d = size(); 90 g.setColor(getBackground()); 91 g.fillRect(0, 0, d.width, d.height); 92 repaint(); 93 } 94 } 95 } 96 } 97 98 public int getPressMovement() { 99 return (pressMovement); 100 } 101 102 /** 103 * Paint the image button with a 3D border 104 */ 105 public void paint(Graphics g) { 106 Color bg = getBackground(); 107 108 if (imgWidth >= 0 && imgHeight >= 0) { 109 synchronized (DesignerAccess.mutex) { 110 Dimension d = size(); 111 Image img = isEnabled() ? upImg : disImg; 112 int x = (d.width - imgWidth) / 2; 113 int y = (d.height - imgHeight) / 2; 114 int offset = (depressed ? pressMovement : 0); 115 116 if (pressMovement != 0) { 117 // clear the area needed to accomodate press movement 118 g.setColor(bg); 119 int m = (pressMovement < 0 ? -1 : 1); 120 for (int i = 0; i < pressMovement * m; i++) 121 g.drawRect(x + i * m + (m < 0 ? -1 : 0), 122 y + i * m + (m < 0 ? -1 : 0), 123 imgWidth, imgHeight); 124 } 125 126 // draw the image and the 3D border 127 if (upImg != null) { 128 // Bug workaround: If a SystemColor 129 // is used as the background color 130 // then Win32 JDK code loses the reference 131 // and the bg of a 132 // transparent gif will be black. 133 if (bg instanceof SystemColor) 134 bg = new Color(bg.getRGB()); 135 g.drawImage(img, x + offset, y + offset, bg, this); 136 } 137 g.setColor(bg); 138 for (int i = 0; i < lineWidth; i++) { 139 g.draw3DRect(i, i, 140 d.width - i*2 - 1, 141 d.height - i*2 - 1, 142 !depressed); 143 } 144 } 145 } 146 } 147 148 public boolean mouseDown(Event e, int x, int y) { 149 depressed = true; 150 repaint(); 151 return true; 152 } 153 154 public boolean mouseDrag(Event e, int x, int y) { 155 if (depressed != inside(x, y)) { 156 depressed = !depressed; 157 repaint(); 158 } 159 return true; 160 } 161 162 public boolean mouseUp(Event evt, int x, int y) { 163 if (depressed) { 164 action(); 165 depressed = false; 166 repaint(); 167 } 168 return true; 169 } 170 171 public void action() { 172 postEvent(new Event(this, Event.ACTION_EVENT, null)); 173 } 174} 175