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 *******************************************************************************/
11package org.eclipse.wb.internal.core.utils.ui;
12
13import org.eclipse.jface.dialogs.Dialog;
14import org.eclipse.swt.graphics.FontMetrics;
15import org.eclipse.swt.graphics.GC;
16import 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 */
25public class PixelConverter {
26  private final FontMetrics fFontMetrics;
27
28  ////////////////////////////////////////////////////////////////////////////
29  //
30  // Constructors
31  //
32  ////////////////////////////////////////////////////////////////////////////
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   */
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   */
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   */
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   */
69  public int convertWidthInCharsToPixels(int chars) {
70    return Dialog.convertWidthInCharsToPixels(fFontMetrics, chars);
71  }
72}
73