1 /******************************************************************************* 2 * Copyright (c) 2011 Google, Inc. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/epl-v10.html 7 * 8 * Contributors: 9 * Google, Inc. - initial API and implementation 10 *******************************************************************************/ 11 package org.eclipse.wb.internal.core.utils.ui; 12 13 import org.eclipse.jface.dialogs.Dialog; 14 import org.eclipse.swt.graphics.FontMetrics; 15 import org.eclipse.swt.graphics.GC; 16 import org.eclipse.swt.widgets.Control; 17 18 /** 19 * Helper class for converting DLU and char size into pixels. 20 * 21 * Based on code from JDT UI. 22 * 23 * @author scheglov_ke 24 */ 25 public class PixelConverter { 26 private final FontMetrics fFontMetrics; 27 28 //////////////////////////////////////////////////////////////////////////// 29 // 30 // Constructors 31 // 32 //////////////////////////////////////////////////////////////////////////// PixelConverter(Control control)33 public PixelConverter(Control control) { 34 GC gc = new GC(control); 35 gc.setFont(control.getFont()); 36 fFontMetrics = gc.getFontMetrics(); 37 gc.dispose(); 38 } 39 40 //////////////////////////////////////////////////////////////////////////// 41 // 42 // Conversions 43 // 44 //////////////////////////////////////////////////////////////////////////// 45 /** 46 * see org.eclipse.jface.dialogs.DialogPage#convertHeightInCharsToPixels(int) 47 */ convertHeightInCharsToPixels(int chars)48 public int convertHeightInCharsToPixels(int chars) { 49 return Dialog.convertHeightInCharsToPixels(fFontMetrics, chars); 50 } 51 52 /** 53 * see org.eclipse.jface.dialogs.DialogPage#convertHorizontalDLUsToPixels(int) 54 */ convertHorizontalDLUsToPixels(int dlus)55 public int convertHorizontalDLUsToPixels(int dlus) { 56 return Dialog.convertHorizontalDLUsToPixels(fFontMetrics, dlus); 57 } 58 59 /** 60 * see org.eclipse.jface.dialogs.DialogPage#convertVerticalDLUsToPixels(int) 61 */ convertVerticalDLUsToPixels(int dlus)62 public int convertVerticalDLUsToPixels(int dlus) { 63 return Dialog.convertVerticalDLUsToPixels(fFontMetrics, dlus); 64 } 65 66 /** 67 * see org.eclipse.jface.dialogs.DialogPage#convertWidthInCharsToPixels(int) 68 */ convertWidthInCharsToPixels(int chars)69 public int convertWidthInCharsToPixels(int chars) { 70 return Dialog.convertWidthInCharsToPixels(fFontMetrics, chars); 71 } 72 } 73