1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.eclipse.org/org/documents/epl-v10.php 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ide.eclipse.adt.internal.editors.layout.gle2; 18 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.custom.CLabel; 21 import org.eclipse.swt.events.MouseEvent; 22 import org.eclipse.swt.events.MouseTrackListener; 23 import org.eclipse.swt.events.PaintEvent; 24 import org.eclipse.swt.events.PaintListener; 25 import org.eclipse.swt.graphics.Color; 26 import org.eclipse.swt.graphics.GC; 27 import org.eclipse.swt.graphics.Image; 28 import org.eclipse.swt.graphics.Point; 29 import org.eclipse.swt.graphics.Rectangle; 30 import org.eclipse.swt.widgets.Canvas; 31 import org.eclipse.swt.widgets.Composite; 32 33 /** 34 * An ImageControl which simply renders an image, with optional margins and tooltips. This 35 * is useful since a {@link CLabel}, even without text, will hide the image when there is 36 * not enough room to fully fit it. 37 * <p> 38 * The image is always rendered left and top aligned. 39 */ 40 public class ImageControl extends Canvas implements MouseTrackListener { 41 private Image mImage; 42 private int mLeftMargin; 43 private int mTopMargin; 44 private int mRightMargin; 45 private int mBottomMargin; 46 private boolean mDisposeImage = true; 47 private boolean mMouseIn; 48 private Color mHoverColor; 49 private float mScale = 1.0f; 50 51 /** 52 * Creates an ImageControl rendering the given image, which will be disposed when this 53 * control is disposed (unless the {@link #setDisposeImage} method is called to turn 54 * off auto dispose). 55 * 56 * @param parent the parent to add the image control to 57 * @param style the SWT style to use 58 * @param image the image to be rendered, which must not be null and should be unique 59 * for this image control since it will be disposed by this control when 60 * the control is disposed (unless the {@link #setDisposeImage} method is 61 * called to turn off auto dispose) 62 */ ImageControl(Composite parent, int style, Image image)63 public ImageControl(Composite parent, int style, Image image) { 64 super(parent, style | SWT.NO_FOCUS | SWT.DOUBLE_BUFFERED); 65 this.mImage = image; 66 67 addPaintListener(new PaintListener() { 68 @Override 69 public void paintControl(PaintEvent event) { 70 onPaint(event); 71 } 72 }); 73 } 74 setImage(Image image)75 public void setImage(Image image) { 76 if (mDisposeImage) { 77 mImage.dispose(); 78 } 79 mImage = image; 80 } 81 setScale(float scale)82 public void setScale(float scale) { 83 mScale = scale; 84 } 85 getScale()86 public float getScale() { 87 return mScale; 88 } 89 setHoverColor(Color hoverColor)90 public void setHoverColor(Color hoverColor) { 91 mHoverColor = hoverColor; 92 if (hoverColor != null) { 93 addMouseTrackListener(this); 94 } 95 } 96 getHoverColor()97 public Color getHoverColor() { 98 return mHoverColor; 99 } 100 101 @Override dispose()102 public void dispose() { 103 super.dispose(); 104 105 if (mDisposeImage) { 106 mImage.dispose(); 107 } 108 mImage = null; 109 } 110 setDisposeImage(boolean mDisposeImage)111 public void setDisposeImage(boolean mDisposeImage) { 112 this.mDisposeImage = mDisposeImage; 113 } 114 getDisposeImage()115 public boolean getDisposeImage() { 116 return mDisposeImage; 117 } 118 119 @Override computeSize(int wHint, int hHint, boolean changed)120 public Point computeSize(int wHint, int hHint, boolean changed) { 121 checkWidget(); 122 Point e = new Point(0, 0); 123 if (mImage != null) { 124 Rectangle r = mImage.getBounds(); 125 if (mScale != 1.0f) { 126 e.x += mScale * r.width; 127 e.y += mScale * r.height; 128 } else { 129 e.x += r.width; 130 e.y += r.height; 131 } 132 } 133 if (wHint == SWT.DEFAULT) { 134 e.x += mLeftMargin + mRightMargin; 135 } else { 136 e.x = wHint; 137 } 138 if (hHint == SWT.DEFAULT) { 139 e.y += mTopMargin + mBottomMargin; 140 } else { 141 e.y = hHint; 142 } 143 144 return e; 145 } 146 onPaint(PaintEvent event)147 private void onPaint(PaintEvent event) { 148 Rectangle rect = getClientArea(); 149 if (mImage == null || rect.width == 0 || rect.height == 0) { 150 return; 151 } 152 153 GC gc = event.gc; 154 Rectangle imageRect = mImage.getBounds(); 155 int imageHeight = imageRect.height; 156 int imageWidth = imageRect.width; 157 int destWidth = imageWidth; 158 int destHeight = imageHeight; 159 160 int oldGcAlias = gc.getAntialias(); 161 int oldGcInterpolation = gc.getInterpolation(); 162 if (mScale != 1.0f) { 163 destWidth = (int) (mScale * destWidth); 164 destHeight = (int) (mScale * destHeight); 165 gc.setAntialias(SWT.ON); 166 gc.setInterpolation(SWT.HIGH); 167 } 168 169 gc.drawImage(mImage, 0, 0, imageWidth, imageHeight, rect.x + mLeftMargin, rect.y 170 + mTopMargin, destWidth, destHeight); 171 172 gc.setAntialias(oldGcAlias); 173 gc.setInterpolation(oldGcInterpolation); 174 175 if (mHoverColor != null && mMouseIn) { 176 gc.setAlpha(60); 177 gc.setBackground(mHoverColor); 178 gc.setLineWidth(1); 179 gc.fillRectangle(0, 0, destWidth, destHeight); 180 } 181 } 182 setMargins(int leftMargin, int topMargin, int rightMargin, int bottomMargin)183 public void setMargins(int leftMargin, int topMargin, int rightMargin, int bottomMargin) { 184 checkWidget(); 185 mLeftMargin = Math.max(0, leftMargin); 186 mTopMargin = Math.max(0, topMargin); 187 mRightMargin = Math.max(0, rightMargin); 188 mBottomMargin = Math.max(0, bottomMargin); 189 redraw(); 190 } 191 192 // ---- Implements MouseTrackListener ---- 193 194 @Override mouseEnter(MouseEvent e)195 public void mouseEnter(MouseEvent e) { 196 mMouseIn = true; 197 if (mHoverColor != null) { 198 redraw(); 199 } 200 } 201 202 @Override mouseExit(MouseEvent e)203 public void mouseExit(MouseEvent e) { 204 mMouseIn = false; 205 if (mHoverColor != null) { 206 redraw(); 207 } 208 } 209 210 @Override mouseHover(MouseEvent e)211 public void mouseHover(MouseEvent e) { 212 } 213 } 214