1 11 package org.eclipse.swt.widgets; 12 13 14 import org.eclipse.swt.internal.win32.*; 15 import org.eclipse.swt.*; 16 import org.eclipse.swt.graphics.*; 17 18 32 public class FontDialog extends Dialog { 33 FontData fontData; 34 RGB rgb; 35 36 49 public FontDialog (Shell parent) { 50 this (parent, SWT.PRIMARY_MODAL); 51 } 52 53 77 public FontDialog (Shell parent, int style) { 78 super (parent, style); 79 checkSubclass (); 80 } 81 82 89 public FontData getFontData () { 90 return fontData; 91 } 92 93 100 public FontData [] getFontList () { 101 if (fontData == null) return null; 102 FontData [] result = new FontData [1]; 103 result [0] = fontData; 104 return result; 105 } 106 107 117 public RGB getRGB () { 118 return rgb; 119 } 120 121 133 public FontData open () { 134 if (OS.IsWinCE) SWT.error (SWT.ERROR_NOT_IMPLEMENTED); 135 136 137 int hwndOwner = 0; 138 if (parent != null) hwndOwner = parent.handle; 139 140 141 int hHeap = OS.GetProcessHeap (); 142 CHOOSEFONT lpcf = new CHOOSEFONT (); 143 lpcf.lStructSize = CHOOSEFONT.sizeof; 144 lpcf.hwndOwner = hwndOwner; 145 lpcf.Flags = OS.CF_SCREENFONTS | OS.CF_EFFECTS; 146 int lpLogFont = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, LOGFONT.sizeof); 147 if (fontData != null && fontData.data != null) { 148 LOGFONT logFont = fontData.data; 149 int lfHeight = logFont.lfHeight; 150 int hDC = OS.GetDC (0); 151 int pixels = -(int)(0.5f + (fontData.height * OS.GetDeviceCaps(hDC, OS.LOGPIXELSY) / 72)); 152 OS.ReleaseDC (0, hDC); 153 logFont.lfHeight = pixels; 154 lpcf.Flags |= OS.CF_INITTOLOGFONTSTRUCT; 155 OS.MoveMemory (lpLogFont, logFont, LOGFONT.sizeof); 156 logFont.lfHeight = lfHeight; 157 } 158 lpcf.lpLogFont = lpLogFont; 159 if (rgb != null) { 160 int red = rgb.red & 0xFF; 161 int green = (rgb.green << 8) & 0xFF00; 162 int blue = (rgb.blue << 16) & 0xFF0000; 163 lpcf.rgbColors = red | green | blue; 164 } 165 166 167 Shell oldModal = null; 168 Display display = null; 169 if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) { 170 display = parent.getDisplay (); 171 oldModal = display.getModalDialogShell (); 172 display.setModalDialogShell (parent); 173 } 174 175 176 boolean success = OS.ChooseFont (lpcf); 177 178 179 if ((style & (SWT.APPLICATION_MODAL | SWT.SYSTEM_MODAL)) != 0) { 180 display.setModalDialogShell (oldModal); 181 } 182 183 184 if (success) { 185 LOGFONT logFont = OS.IsUnicode ? (LOGFONT) new LOGFONTW () : new LOGFONTA (); 186 OS.MoveMemory (logFont, lpLogFont, LOGFONT.sizeof); 187 188 192 int hDC = OS.GetDC(0); 193 int logPixelsY = OS.GetDeviceCaps(hDC, OS.LOGPIXELSY); 194 int pixels = 0; 195 if (logFont.lfHeight > 0) { 196 204 int hFont = OS.CreateFontIndirect(logFont); 205 int oldFont = OS.SelectObject(hDC, hFont); 206 TEXTMETRIC lptm = OS.IsUnicode ? (TEXTMETRIC) new TEXTMETRICW () : new TEXTMETRICA (); 207 OS.GetTextMetrics(hDC, lptm); 208 OS.SelectObject(hDC, oldFont); 209 OS.DeleteObject(hFont); 210 pixels = logFont.lfHeight - lptm.tmInternalLeading; 211 } else { 212 pixels = -logFont.lfHeight; 213 } 214 OS.ReleaseDC(0, hDC); 215 216 float points = pixels * 72f /logPixelsY; 217 fontData = FontData.win32_new (logFont, points); 218 int red = lpcf.rgbColors & 0xFF; 219 int green = (lpcf.rgbColors >> 8) & 0xFF; 220 int blue = (lpcf.rgbColors >> 16) & 0xFF; 221 rgb = new RGB (red, green, blue); 222 } 223 224 225 if (lpLogFont != 0) OS.HeapFree (hHeap, 0, lpLogFont); 226 227 233 235 if (!success) return null; 236 return fontData; 237 } 238 239 247 public void setFontData (FontData fontData) { 248 this.fontData = fontData; 249 } 250 251 263 public void setFontList (FontData [] fontData) { 264 if (fontData != null && fontData.length > 0) { 265 this.fontData = fontData [0]; 266 } else { 267 this.fontData = null; 268 } 269 } 270 271 282 public void setRGB (RGB rgb) { 283 this.rgb = rgb; 284 } 285 286 } 287 | Popular Tags |