KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > forms > widgets > PixelConverter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
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  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.forms.widgets;
12
13 import org.eclipse.swt.graphics.FontMetrics;
14 import org.eclipse.swt.graphics.GC;
15 import org.eclipse.swt.widgets.Control;
16
17 public class PixelConverter {
18     /**
19      * Number of horizontal dialog units per character, value <code>4</code>.
20      */

21     private static final int HORIZONTAL_DIALOG_UNIT_PER_CHAR = 4;
22
23     private FontMetrics fFontMetrics;
24
25     public PixelConverter(Control control) {
26         GC gc = new GC(control);
27         gc.setFont(control.getFont());
28         fFontMetrics = gc.getFontMetrics();
29         gc.dispose();
30     }
31
32     public int convertHorizontalDLUsToPixels(int dlus) {
33         // round to the nearest pixel
34
return (fFontMetrics.getAverageCharWidth() * dlus + HORIZONTAL_DIALOG_UNIT_PER_CHAR / 2)
35                 / HORIZONTAL_DIALOG_UNIT_PER_CHAR;
36     }
37 }
38
Popular Tags