| 1 | /* |
| 2 | * $Id: HDC.java,v 1.4 2005/12/01 06:10:00 hastings Exp $ |
| 3 | * |
| 4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This material may be reproduced by or for the U. S. Government |
| 9 | * pursuant to the copyright license under the clause at |
| 10 | * DFARS 252.227-7014 (OCT 2001). |
| 11 | */ |
| 12 | |
| 13 | package com.moesol.bindings.platform_sdk.windows_api; |
| 14 | |
| 15 | /** |
| 16 | * Wrap a Win32 HDC. |
| 17 | */ |
| 18 | public class HDC extends HANDLE { |
| 19 | |
| 20 | /** |
| 21 | * Wrap an HDC around a native hdc handle. If temporary is true |
| 22 | * then do not check to see if this HDC has been deleted after it |
| 23 | * has been gc'ed. |
| 24 | * |
| 25 | * @param handle |
| 26 | * @param temporary |
| 27 | */ |
| 28 | protected HDC(long handle, boolean temporary) { |
| 29 | super(handle, temporary); |
| 30 | } |
| 31 | /** |
| 32 | * Wrap a HDC around a native hdc handle. |
| 33 | * @param handle |
| 34 | */ |
| 35 | protected HDC(long handle) { |
| 36 | super(handle); |
| 37 | } |
| 38 | /** |
| 39 | * Call Win32 CreateDC. |
| 40 | * @param driver |
| 41 | * @param device |
| 42 | * @param output |
| 43 | * @param devmode - must be null (not implemented). |
| 44 | * @return a new HDC |
| 45 | */ |
| 46 | public static HDC createDC(String driver, String device, String output, DEVMODE devmode) { |
| 47 | long r = jni_createDC(driver, device, output, devmode); |
| 48 | HDC rdc = new HDC(checkReturn(r, 0)); |
| 49 | rdc.setNeedsDelete(true); |
| 50 | return rdc; |
| 51 | } |
| 52 | /** |
| 53 | * Call Win32 CreateCompatibleDC |
| 54 | * @param hdc |
| 55 | * @return an HDC that is compatible with @{code hdc}. |
| 56 | */ |
| 57 | public static HDC createCompatibleDC(HDC hdc) { |
| 58 | long r = jni_createCompatibleDC(hdc._getHandle()); |
| 59 | HDC rdc = new HDC(checkReturn(r, 0)); |
| 60 | rdc.setNeedsDelete(true); |
| 61 | return rdc; |
| 62 | } |
| 63 | /** |
| 64 | * Call Win32 DeleteDC |
| 65 | * |
| 66 | */ |
| 67 | public void deleteDC() { |
| 68 | if (!isNeedsDelete()) { |
| 69 | throw new IllegalStateException("this HDC was not created"); |
| 70 | } |
| 71 | boolean r = jni_deleteDC(_getHandle()); |
| 72 | checkReturn(r); |
| 73 | detach(); |
| 74 | } |
| 75 | /** |
| 76 | * Call Win32 GetTextMetrics |
| 77 | * @param metric |
| 78 | */ |
| 79 | public void getTextMetrics(TEXTMETRIC metric) { |
| 80 | boolean r = jni_getTextMetrics(_getHandle(), metric._getStructureBytes()); |
| 81 | checkReturn(r); |
| 82 | } |
| 83 | /** |
| 84 | * Call Win32 GetTextExtentPoint32W using text.length() for the len parameter. |
| 85 | * @param text |
| 86 | * @param size |
| 87 | */ |
| 88 | public void getTextExtentPoint32(String text, SIZE size) { |
| 89 | getTextExtentPoint32(text, text.length(), size); |
| 90 | } |
| 91 | /** |
| 92 | * Call Win32 GetTextExtendPoint32W |
| 93 | * @param text |
| 94 | * @param len |
| 95 | * @param size |
| 96 | */ |
| 97 | public void getTextExtentPoint32(String text, int len, SIZE size) { |
| 98 | boolean r = jni_getTextExtentPoint32(_getHandle(), text, len, size._getStructureBytes()); |
| 99 | checkReturn(r); |
| 100 | } |
| 101 | /** |
| 102 | * Call Win32 GetTextExtendPoint32A using text.length as len parameter. |
| 103 | * @param text |
| 104 | * @param size |
| 105 | */ |
| 106 | public void getTextExtentPoint32(byte[] text, SIZE size) { |
| 107 | getTextExtentPoint32(text, text.length, size); |
| 108 | } |
| 109 | /** |
| 110 | * Call Win32 GetTextExtendPoint32A |
| 111 | * @param text |
| 112 | * @param len |
| 113 | * @param size |
| 114 | */ |
| 115 | public void getTextExtentPoint32(byte[] text, int len, SIZE size) { |
| 116 | boolean r = jni_getTextExtentPoint32(_getHandle(), text, len, size._getStructureBytes()); |
| 117 | checkReturn(r); |
| 118 | } |
| 119 | /** |
| 120 | * Call Win32 SetBkColor. |
| 121 | * @param colorref |
| 122 | * @return |
| 123 | */ |
| 124 | public int setBkColor(int colorref) { |
| 125 | int r = jni_setBkColor(_getHandle(), colorref); |
| 126 | return checkReturn(r, CLR_INVALID); |
| 127 | } |
| 128 | /** |
| 129 | * Call Win32 SetBkMode. |
| 130 | * @param iBkMode |
| 131 | * @return |
| 132 | */ |
| 133 | public int setBkMode(int iBkMode) { |
| 134 | int r = jni_setBkMode(_getHandle(), iBkMode); |
| 135 | return checkReturn(r, 0); |
| 136 | } |
| 137 | /** |
| 138 | * Call Win32 SetTextColor. |
| 139 | * @param colorref |
| 140 | * @return |
| 141 | */ |
| 142 | public int setTextColor(int colorref) { |
| 143 | int r = jni_setTextColor(_getHandle(), colorref); |
| 144 | return checkReturn(r, CLR_INVALID); |
| 145 | } |
| 146 | /** |
| 147 | * Call Win32 SetTextAlign. |
| 148 | * @param fMode |
| 149 | * @return |
| 150 | */ |
| 151 | public int setTextAlign(int fMode) { |
| 152 | int r = jni_setTextAlign(_getHandle(), fMode); |
| 153 | return checkReturn(r, GDI_ERROR); |
| 154 | } |
| 155 | /** |
| 156 | * Call Win32 SelectObject. |
| 157 | * |
| 158 | * @param hbitmap |
| 159 | * @return the HBITMAP that was previously selected. |
| 160 | */ |
| 161 | public HBITMAP selectObject(HBITMAP hbitmap) { |
| 162 | long r = jni_selectObject(_getHandle(), hbitmap._getHandle()); |
| 163 | return new HBITMAP(checkReturn(r, 0), true); |
| 164 | } |
| 165 | /** |
| 166 | * Call Win32 SelectObject. |
| 167 | * |
| 168 | * @param hpen |
| 169 | * @return the HPEN what was previously selected. |
| 170 | */ |
| 171 | public HPEN selectObject(HPEN hpen) { |
| 172 | long r = jni_selectObject(_getHandle(), hpen._getHandle()); |
| 173 | return new HPEN(checkReturn(r, 0), true); |
| 174 | } |
| 175 | /** |
| 176 | * Select <code>hfont</code> into this DC. The returned HFONT should |
| 177 | * be re-selected when the drawing operation using hfont is complete. |
| 178 | * The returned HFONT is marked as temporary and will not be reported |
| 179 | * if leaked. |
| 180 | * |
| 181 | * @param hfont |
| 182 | * @return the HFONT that was previously selected. |
| 183 | */ |
| 184 | public HFONT selectObject(HFONT hfont) { |
| 185 | long r = jni_selectObject(_getHandle(), hfont._getHandle()); |
| 186 | return new HFONT(checkReturn(r, 0), true); |
| 187 | } |
| 188 | /** |
| 189 | * Call Win32 MoveTo. |
| 190 | * |
| 191 | * @param x |
| 192 | * @param y |
| 193 | */ |
| 194 | public void moveTo(int x, int y) { |
| 195 | moveTo(x, y, null); |
| 196 | } |
| 197 | /** |
| 198 | * Call Win32 MoveTo. |
| 199 | * |
| 200 | * @param x |
| 201 | * @param y |
| 202 | * @param out_old_position |
| 203 | */ |
| 204 | public void moveTo(int x, int y, POINT out_old_position) { |
| 205 | boolean r = jni_moveTo(_getHandle(), x, y, out_old_position); |
| 206 | checkReturn(r); |
| 207 | } |
| 208 | /** |
| 209 | * Call Win32 LineTo. |
| 210 | * |
| 211 | * @param x |
| 212 | * @param y |
| 213 | */ |
| 214 | public void lineTo(int x, int y) { |
| 215 | boolean r = jni_lineTo(_getHandle(), x, y); |
| 216 | checkReturn(r); |
| 217 | } |
| 218 | /** |
| 219 | * Call Win32 TextOutW |
| 220 | * |
| 221 | * @param xstart |
| 222 | * @param ystart |
| 223 | * @param s |
| 224 | */ |
| 225 | public void textOut(int xstart, int ystart, String s) { |
| 226 | textOut(xstart, ystart, s, s.length()); |
| 227 | } |
| 228 | /** |
| 229 | * Call Win32 TextOutW |
| 230 | * |
| 231 | * @param xstart |
| 232 | * @param ystart |
| 233 | * @param s |
| 234 | * @param len |
| 235 | */ |
| 236 | public void textOut(int xstart, int ystart, String s, int len) { |
| 237 | boolean r = jni_textOut(_getHandle(), xstart, ystart, s, len); |
| 238 | checkReturn(r); |
| 239 | } |
| 240 | /** |
| 241 | * Call Win32 TextOutA |
| 242 | * |
| 243 | * @param xstart |
| 244 | * @param ystart |
| 245 | * @param s |
| 246 | */ |
| 247 | public void textOut(int xstart, int ystart, byte[] s) { |
| 248 | textOut(xstart, ystart, s, s.length); |
| 249 | } |
| 250 | /** |
| 251 | * Call Win32 TextOutA |
| 252 | * @param xstart |
| 253 | * @param ystart |
| 254 | * @param s |
| 255 | * @param len |
| 256 | */ |
| 257 | public void textOut(int xstart, int ystart, byte[] s, int len) { |
| 258 | boolean r = jni_textOutA(_getHandle(), xstart, ystart, s, len); |
| 259 | checkReturn(r); |
| 260 | } |
| 261 | /** |
| 262 | * Call Win32 BitBlt. |
| 263 | * |
| 264 | * @param nXDest |
| 265 | * @param nYDest |
| 266 | * @param nWidth |
| 267 | * @param nHeight |
| 268 | * @param hdcSrc |
| 269 | * @param nXSrc |
| 270 | * @param nYSrc |
| 271 | * @param dwRop |
| 272 | */ |
| 273 | public void bitBlt( |
| 274 | int nXDest, // x-coordinate of destination rectangle's upper-left |
| 275 | // corner |
| 276 | int nYDest, // y-coordinate of destination rectangle's upper-left |
| 277 | // corner |
| 278 | int nWidth, // width of destination rectangle |
| 279 | int nHeight, // height of destination rectangle |
| 280 | HDC hdcSrc, // handle to source device context |
| 281 | int nXSrc, // x-coordinate of source rectangle's upper-left |
| 282 | // corner |
| 283 | int nYSrc, // y-coordinate of source rectangle's upper-left |
| 284 | // corner |
| 285 | long dwRop) |
| 286 | { |
| 287 | boolean r = jni_bitBlt(_getHandle(), nXDest, nYDest, nWidth, nHeight, |
| 288 | hdcSrc._getHandle(), nXSrc, nYSrc, dwRop); |
| 289 | checkReturn(r); |
| 290 | } |
| 291 | /** |
| 292 | * @return true if this HDC needs to be deleted. |
| 293 | */ |
| 294 | public boolean isNeedsDelete() { |
| 295 | return (m_create_flags & NEEDS_DELETE) != 0; |
| 296 | } |
| 297 | /** |
| 298 | * @return true if this HDC needs to be released. |
| 299 | */ |
| 300 | public boolean isNeedsRelease() { |
| 301 | return (m_create_flags & NEEDS_RELEASE) != 0; |
| 302 | } |
| 303 | /** Win32 constant */ |
| 304 | public static final long SRCCOPY = 0x00CC0020; |
| 305 | /** Win32 constant */ |
| 306 | public static final long SRCPAINT = 0x00EE0086; |
| 307 | /** Win32 constant */ |
| 308 | public static final long SRCAND = 0x008800C6; |
| 309 | /** Win32 constant */ |
| 310 | public static final long SRCINVERT = 0x00660046; |
| 311 | /** Win32 constant */ |
| 312 | public static final long SRCERASE = 0x00440328; |
| 313 | /** Win32 constant */ |
| 314 | public static final long NOTSRCCOPY = 0x00330008; |
| 315 | /** Win32 constant */ |
| 316 | public static final long NOTSRCERASE = 0x001100A6; |
| 317 | /** Win32 constant */ |
| 318 | public static final long MERGECOPY = 0x00C000CA; |
| 319 | /** Win32 constant */ |
| 320 | public static final long MERGEPAINT = 0x00BB0226; |
| 321 | /** Win32 constant */ |
| 322 | public static final long PATCOPY = 0x00F00021; |
| 323 | /** Win32 constant */ |
| 324 | public static final long PATPAINT = 0x00FB0A09; |
| 325 | /** Win32 constant */ |
| 326 | public static final long PATINVERT = 0x005A0049; |
| 327 | /** Win32 constant */ |
| 328 | public static final long DSTINVERT = 0x00550009; |
| 329 | /** Win32 constant */ |
| 330 | public static final long BLACKNESS = 0x00000042; |
| 331 | /** Win32 constant */ |
| 332 | public static final long WHITENESS = 0x00FF0062; |
| 333 | |
| 334 | /** Win32 constant */ |
| 335 | public static final int CLR_INVALID = 0xFFFFFFFF; |
| 336 | /** Win32 constant */ |
| 337 | public static final int GDI_ERROR = 0xFFFFFFFF; |
| 338 | |
| 339 | /* Text Alignment Options */ |
| 340 | /** Win32 constant */ |
| 341 | public static final int TA_NOUPDATECP = 0; |
| 342 | /** Win32 constant */ |
| 343 | public static final int TA_UPDATECP = 1; |
| 344 | |
| 345 | /** Win32 constant */ |
| 346 | public static final int TA_LEFT = 0; |
| 347 | /** Win32 constant */ |
| 348 | public static final int TA_RIGHT = 2; |
| 349 | /** Win32 constant */ |
| 350 | public static final int TA_CENTER = 6; |
| 351 | |
| 352 | /** Win32 constant */ |
| 353 | public static final int TA_TOP = 0; |
| 354 | /** Win32 constant */ |
| 355 | public static final int TA_BOTTOM = 8; |
| 356 | /** Win32 constant */ |
| 357 | public static final int TA_BASELINE = 24; |
| 358 | |
| 359 | /* Background Modes */ |
| 360 | /** Win32 constant */ |
| 361 | public static final int TRANSPARENT = 1; |
| 362 | /** Win32 constant */ |
| 363 | public static final int OPAQUE = 2; |
| 364 | |
| 365 | |
| 366 | private static void checkReturn(boolean r) { |
| 367 | if (!r) { |
| 368 | throw new RuntimeException("HDC call failed"); |
| 369 | } |
| 370 | } |
| 371 | private static long checkReturn(long r, long err_value) { |
| 372 | if (r == err_value) { |
| 373 | throw new RuntimeException("HDC call failed"); |
| 374 | } |
| 375 | return r; |
| 376 | } |
| 377 | private static int checkReturn(int r, int err_value) { |
| 378 | if (r == err_value) { |
| 379 | throw new RuntimeException("HDC call failed"); |
| 380 | } |
| 381 | return r; |
| 382 | } |
| 383 | private void setNeedsDelete(boolean v) { |
| 384 | setBit(v, NEEDS_DELETE); |
| 385 | } |
| 386 | private void setNeedsRelease(boolean v) { |
| 387 | setBit(v, NEEDS_RELEASE); |
| 388 | } |
| 389 | /** |
| 390 | * Fix eclipse warning about private method that is never called. |
| 391 | * The method is called from JNI. |
| 392 | */ |
| 393 | void fixWarning() { |
| 394 | if (true) { |
| 395 | throw new IllegalStateException("never call"); |
| 396 | } |
| 397 | setNeedsRelease(true); |
| 398 | } |
| 399 | private void setBit(boolean v, int bit) { |
| 400 | if (v) { |
| 401 | m_create_flags |= bit; |
| 402 | } else { |
| 403 | m_create_flags &= ~bit; |
| 404 | } |
| 405 | } |
| 406 | private static native long jni_createDC(String driver, String device, String output, DEVMODE devmode); |
| 407 | private static native long jni_createCompatibleDC(long handle); |
| 408 | private native boolean jni_deleteDC(long handle); |
| 409 | private native boolean jni_getTextMetrics(long handle, byte[] metric_bytes); |
| 410 | private native boolean jni_getTextExtentPoint32(long handle, String text, int len, byte[] size_bytes); |
| 411 | private native boolean jni_getTextExtentPoint32(long handle, byte[] text, int len, byte[] size_bytes); |
| 412 | |
| 413 | private native int jni_setBkColor(long handle, int colorref); |
| 414 | private native int jni_setBkMode(long handle, int iBkMode); |
| 415 | private native int jni_setTextColor(long handle, int colorref); |
| 416 | private native int jni_setTextAlign(long handle, int fMode); |
| 417 | private native long jni_selectObject(long handle, long hgdiobj); |
| 418 | private native boolean jni_moveTo(long handle, int x, int y, POINT out_old_position); |
| 419 | private native boolean jni_lineTo(long handle, int x, int y); |
| 420 | private native boolean jni_textOut(long handle, int xstart, int ystart, String s, int len); |
| 421 | private native boolean jni_textOutA(long handle, int xstart, int ystart, byte[] s, int len); |
| 422 | private native boolean jni_bitBlt( |
| 423 | long handle, |
| 424 | int nXDest, |
| 425 | int nYDest, |
| 426 | int nWidth, |
| 427 | int nHeight, |
| 428 | long hdcSrc, |
| 429 | int nXSrc, |
| 430 | int nYSrc, |
| 431 | long dwRop); |
| 432 | |
| 433 | private int m_create_flags = 0; |
| 434 | private static final int NEEDS_DELETE = (1 << 0); |
| 435 | private static final int NEEDS_RELEASE = (1 << 1); |
| 436 | } |