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 33 public class Caret extends Widget { 34 Canvas parent; 35 int x, y, width, height; 36 boolean moved, resized; 37 boolean isVisible; 38 Image image; 39 Font font; 40 LOGFONT oldFont; 41 42 70 public Caret (Canvas parent, int style) { 71 super (parent, style); 72 this.parent = parent; 73 createWidget (); 74 } 75 76 void createWidget () { 77 isVisible = true; 78 if (parent.getCaret () == null) { 79 parent.setCaret (this); 80 } 81 } 82 83 int defaultFont () { 84 int hwnd = parent.handle; 85 int hwndIME = OS.ImmGetDefaultIMEWnd (hwnd); 86 int hFont = 0; 87 if (hwndIME != 0) { 88 hFont = OS.SendMessage (hwndIME, OS.WM_GETFONT, 0, 0); 89 } 90 if (hFont == 0) { 91 hFont = OS.SendMessage (hwnd, OS.WM_GETFONT, 0, 0); 92 } 93 if (hFont == 0) return parent.defaultFont (); 94 return hFont; 95 } 96 97 108 public Rectangle getBounds () { 109 checkWidget(); 110 if (image != null) { 111 Rectangle rect = image.getBounds (); 112 return new Rectangle (x, y, rect.width, rect.height); 113 } 114 return new Rectangle (x, y, width, height); 115 } 116 117 127 public Font getFont () { 128 checkWidget(); 129 if (font == null) { 130 int hFont = defaultFont (); 131 return Font.win32_new (display, hFont); 132 } 133 return font; 134 } 135 136 146 public Image getImage () { 147 checkWidget(); 148 return image; 149 } 150 151 162 public Point getLocation () { 163 checkWidget(); 164 return new Point (x, y); 165 } 166 167 177 public Canvas getParent () { 178 checkWidget(); 179 return parent; 180 } 181 182 192 public Point getSize () { 193 checkWidget(); 194 if (image != null) { 195 Rectangle rect = image.getBounds (); 196 return new Point (rect.width, rect.height); 197 } 198 return new Point (width, height); 199 } 200 201 218 public boolean getVisible () { 219 checkWidget(); 220 return isVisible; 221 } 222 223 boolean hasFocus () { 224 return parent.handle == OS.GetFocus (); 225 } 226 227 boolean isFocusCaret () { 228 return parent.caret == this && hasFocus (); 229 } 230 231 245 public boolean isVisible () { 246 checkWidget(); 247 return isVisible && parent.isVisible () && hasFocus (); 248 } 249 250 void killFocus () { 251 OS.DestroyCaret (); 252 restoreIMEFont (); 253 } 254 255 void move () { 256 moved = false; 257 if (!OS.SetCaretPos (x, y)) return; 258 resizeIME (); 259 } 260 261 void resizeIME () { 262 if (!OS.IsDBLocale) return; 263 POINT ptCurrentPos = new POINT (); 264 if (!OS.GetCaretPos (ptCurrentPos)) return; 265 int hwnd = parent.handle; 266 RECT rect = new RECT (); 267 OS.GetClientRect (hwnd, rect); 268 COMPOSITIONFORM lpCompForm = new COMPOSITIONFORM (); 269 lpCompForm.dwStyle = OS.CFS_RECT; 270 lpCompForm.x = ptCurrentPos.x; 271 lpCompForm.y = ptCurrentPos.y; 272 lpCompForm.left = rect.left; 273 lpCompForm.right = rect.right; 274 lpCompForm.top = rect.top; 275 lpCompForm.bottom = rect.bottom; 276 int hIMC = OS.ImmGetContext (hwnd); 277 OS.ImmSetCompositionWindow (hIMC, lpCompForm); 278 OS.ImmReleaseContext (hwnd, hIMC); 279 } 280 281 void releaseParent () { 282 super.releaseParent (); 283 if (this == parent.getCaret ()) parent.setCaret (null); 284 } 285 286 void releaseWidget () { 287 super.releaseWidget (); 288 parent = null; 289 image = null; 290 font = null; 291 oldFont = null; 292 } 293 294 void resize () { 295 resized = false; 296 int hwnd = parent.handle; 297 OS.DestroyCaret (); 298 int hBitmap = image != null ? image.handle : 0; 299 OS.CreateCaret (hwnd, hBitmap, width, height); 300 OS.SetCaretPos (x, y); 301 OS.ShowCaret (hwnd); 302 move (); 303 } 304 305 void restoreIMEFont () { 306 if (!OS.IsDBLocale) return; 307 if (oldFont == null) return; 308 int hwnd = parent.handle; 309 int hIMC = OS.ImmGetContext (hwnd); 310 OS.ImmSetCompositionFont (hIMC, oldFont); 311 OS.ImmReleaseContext (hwnd, hIMC); 312 oldFont = null; 313 } 314 315 331 public void setBounds (int x, int y, int width, int height) { 332 checkWidget(); 333 boolean samePosition = this.x == x && this.y == y; 334 boolean sameExtent = this.width == width && this.height == height; 335 if (samePosition && sameExtent) return; 336 this.x = x; this.y = y; 337 this.width = width; this.height = height; 338 if (sameExtent) { 339 moved = true; 340 if (isVisible && hasFocus ()) move (); 341 } else { 342 resized = true; 343 if (isVisible && hasFocus ()) resize (); 344 } 345 } 346 347 360 public void setBounds (Rectangle rect) { 361 if (rect == null) error (SWT.ERROR_NULL_ARGUMENT); 362 setBounds (rect.x, rect.y, rect.width, rect.height); 363 } 364 365 void setFocus () { 366 int hwnd = parent.handle; 367 int hBitmap = 0; 368 if (image != null) hBitmap = image.handle; 369 OS.CreateCaret (hwnd, hBitmap, width, height); 370 move (); 371 setIMEFont (); 372 if (isVisible) OS.ShowCaret (hwnd); 373 } 374 375 390 public void setFont (Font font) { 391 checkWidget(); 392 if (font != null && font.isDisposed ()) { 393 error (SWT.ERROR_INVALID_ARGUMENT); 394 } 395 this.font = font; 396 if (hasFocus ()) setIMEFont (); 397 } 398 399 414 public void setImage (Image image) { 415 checkWidget(); 416 if (image != null && image.isDisposed ()) { 417 error (SWT.ERROR_INVALID_ARGUMENT); 418 } 419 this.image = image; 420 if (isVisible && hasFocus ()) resize (); 421 } 422 423 void setIMEFont () { 424 if (!OS.IsDBLocale) return; 425 int hFont = 0; 426 if (font != null) hFont = font.handle; 427 if (hFont == 0) hFont = defaultFont (); 428 int hwnd = parent.handle; 429 int hIMC = OS.ImmGetContext (hwnd); 430 431 if (oldFont == null) { 432 oldFont = OS.IsUnicode ? (LOGFONT) new LOGFONTW () : new LOGFONTA (); 433 if (!OS.ImmGetCompositionFont (hIMC, oldFont)) oldFont = null; 434 } 435 436 LOGFONT logFont = OS.IsUnicode ? (LOGFONT) new LOGFONTW () : new LOGFONTA (); 437 if (OS.GetObject (hFont, LOGFONT.sizeof, logFont) != 0) { 438 OS.ImmSetCompositionFont (hIMC, logFont); 439 } 440 OS.ImmReleaseContext (hwnd, hIMC); 441 } 442 443 456 public void setLocation (int x, int y) { 457 checkWidget(); 458 if (this.x == x && this.y == y) return; 459 this.x = x; this.y = y; 460 moved = true; 461 if (isVisible && hasFocus ()) move (); 462 } 463 464 476 public void setLocation (Point location) { 477 checkWidget(); 478 if (location == null) error (SWT.ERROR_NULL_ARGUMENT); 479 setLocation (location.x, location.y); 480 } 481 482 493 public void setSize (int width, int height) { 494 checkWidget(); 495 if (this.width == width && this.height == height) return; 496 this.width = width; this.height = height; 497 resized = true; 498 if (isVisible && hasFocus ()) resize (); 499 } 500 501 514 public void setSize (Point size) { 515 checkWidget(); 516 if (size == null) error (SWT.ERROR_NULL_ARGUMENT); 517 setSize (size.x, size.y); 518 } 519 520 536 public void setVisible (boolean visible) { 537 checkWidget(); 538 if (visible == isVisible) return; 539 isVisible = visible; 540 int hwnd = parent.handle; 541 if (OS.GetFocus () != hwnd) return; 542 if (!isVisible) { 543 OS.HideCaret (hwnd); 544 } else { 545 if (resized) { 546 resize (); 547 } else { 548 if (moved) move (); 549 } 550 OS.ShowCaret (hwnd); 551 } 552 } 553 554 } 555 | Popular Tags |