Mercurial > projects > dwt-addons
view dwtx/ui/internal/forms/widgets/FormFonts.d @ 85:56fea7e5f0f9
Fix some runtime errors
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 20 Jun 2008 14:48:17 +0200 |
parents | e193036d82c9 |
children | 04b47443bb01 |
line wrap: on
line source
/******************************************************************************* * Copyright (c) 2007 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation * Port to the D programming language: * Frank Benoit <benoit@tionex.de> *******************************************************************************/ module dwtx.ui.internal.forms.widgets.FormFonts; import tango.util.collection.HashMap; import dwt.DWT; import dwt.graphics.Font; import dwt.graphics.FontData; import dwt.widgets.Display; import dwt.dwthelper.utils; public class FormFonts { alias HashMap!(Object,Object) HashMapStrToObj; private static FormFonts instance; public static FormFonts getInstance() { if (instance is null) instance = new FormFonts(); return instance; } private HashMapStrToObj fonts; private HashMapStrToObj ids; private this() { } private class FontIdentifier { private Display fDisplay; private Font fFont; this (Display display, Font font) { fDisplay = display; fFont = font; } public bool equals(Object obj) { if (auto id = cast(FontIdentifier)obj ) { return id.fDisplay.opEquals(fDisplay) && id.fFont.opEquals(fFont); } return false; } public override hash_t toHash() { return fDisplay.toHash() * 7 + fFont.toHash(); } } private class FontReference { private Font fFont; private int fCount; public this(Font font) { fFont = font; fCount = 1; } public Font getFont() { return fFont; } // returns a bool indicating if all clients of this font are finished // a true result indicates the underlying image should be disposed public bool decCount() { return --fCount is 0; } public void incCount() { fCount++; } } public Font getBoldFont(Display display, Font font) { checkHashMaps(); FontIdentifier fid = new FontIdentifier(display, font); FontReference result = fonts.containsKey(fid) ? cast(FontReference) fonts.get(fid) : null; if (result !is null && !result.getFont().isDisposed()) { result.incCount(); return result.getFont(); } Font boldFont = createBoldFont(display, font); fonts.add(fid, new FontReference(boldFont)); ids.add(boldFont, fid); return boldFont; } public bool markFinished(Font boldFont) { checkHashMaps(); FontIdentifier id = cast(FontIdentifier)ids.get(boldFont); if (id !is null) { FontReference ref_ = cast(FontReference) fonts.get(id); if (ref_ !is null) { if (ref_.decCount()) { fonts.remove(id); ids.remove(ref_.getFont()); ref_.getFont().dispose(); validateHashMaps(); } return true; } } // if the image was not found, dispose of it for the caller boldFont.dispose(); return false; } private Font createBoldFont(Display display, Font regularFont) { FontData[] fontDatas = regularFont.getFontData(); for (int i = 0; i < fontDatas.length; i++) { fontDatas[i].setStyle(fontDatas[i].getStyle() | DWT.BOLD); } return new Font(display, fontDatas); } private void checkHashMaps() { if (fonts is null) fonts = new HashMapStrToObj(); if (ids is null) ids = new HashMapStrToObj(); } private void validateHashMaps() { if (fonts.size() is 0) fonts = null; if (ids.size() is 0) ids = null; } }