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 import org.eclipse.swt.events.*; 18 19 35 public class TreeColumn extends Item { 36 Tree parent; 37 boolean resizable, moveable; 38 String toolTipText; 39 int id; 40 41 73 public TreeColumn (Tree parent, int style) { 74 super (parent, checkStyle (style)); 75 resizable = true; 76 this.parent = parent; 77 parent.createItem (this, parent.getColumnCount ()); 78 } 79 80 114 public TreeColumn (Tree parent, int style, int index) { 115 super (parent, checkStyle (style)); 116 resizable = true; 117 this.parent = parent; 118 parent.createItem (this, index); 119 } 120 121 140 public void addControlListener(ControlListener listener) { 141 checkWidget (); 142 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 143 TypedListener typedListener = new TypedListener (listener); 144 addListener (SWT.Resize,typedListener); 145 addListener (SWT.Move,typedListener); 146 } 147 148 172 public void addSelectionListener (SelectionListener listener) { 173 checkWidget (); 174 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 175 TypedListener typedListener = new TypedListener (listener); 176 addListener (SWT.Selection,typedListener); 177 addListener (SWT.DefaultSelection,typedListener); 178 } 179 180 static int checkStyle (int style) { 181 return checkBits (style, SWT.LEFT, SWT.CENTER, SWT.RIGHT, 0, 0, 0); 182 } 183 184 protected void checkSubclass () { 185 if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS); 186 } 187 188 void destroyWidget () { 189 parent.destroyItem (this); 190 releaseHandle (); 191 } 192 193 205 public int getAlignment () { 206 checkWidget (); 207 if ((style & SWT.LEFT) != 0) return SWT.LEFT; 208 if ((style & SWT.CENTER) != 0) return SWT.CENTER; 209 if ((style & SWT.RIGHT) != 0) return SWT.RIGHT; 210 return SWT.LEFT; 211 } 212 213 233 public boolean getMoveable () { 234 checkWidget (); 235 return moveable; 236 } 237 238 String getNameText () { 239 return getText (); 240 } 241 242 252 public Tree getParent () { 253 checkWidget (); 254 return parent; 255 } 256 257 269 public boolean getResizable () { 270 checkWidget (); 271 return resizable; 272 } 273 274 287 public String getToolTipText () { 288 checkWidget(); 289 return toolTipText; 290 } 291 292 302 public int getWidth () { 303 checkWidget (); 304 int index = parent.indexOf (this); 305 if (index == -1) return 0; 306 int hwndHeader = parent.hwndHeader; 307 if (hwndHeader == 0) return 0; 308 HDITEM hdItem = new HDITEM (); 309 hdItem.mask = OS.HDI_WIDTH; 310 OS.SendMessage (hwndHeader, OS.HDM_GETITEM, index, hdItem); 311 return hdItem.cxy; 312 } 313 314 325 public void pack () { 326 checkWidget (); 327 int index = parent.indexOf (this); 328 if (index == -1) return; 329 int columnWidth = 0; 330 int hwnd = parent.handle, hwndHeader = parent.hwndHeader; 331 RECT headerRect = new RECT (); 332 OS.SendMessage (hwndHeader, OS.HDM_GETITEMRECT, index, headerRect); 333 int hDC = OS.GetDC (hwnd); 334 int oldFont = 0, newFont = OS.SendMessage (hwnd, OS.WM_GETFONT, 0, 0); 335 if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont); 336 TVITEM tvItem = new TVITEM (); 337 tvItem.mask = OS.TVIF_HANDLE | OS.TVIF_PARAM; 338 tvItem.hItem = OS.SendMessage (hwnd, OS.TVM_GETNEXTITEM, OS.TVGN_ROOT, 0); 339 while (tvItem.hItem != 0) { 340 OS.SendMessage (hwnd, OS.TVM_GETITEM, 0, tvItem); 341 TreeItem item = tvItem.lParam != -1 ? parent.items [tvItem.lParam] : null; 342 if (item != null) { 343 int hFont = item.cellFont != null ? item.cellFont [index] : -1; 344 if (hFont == -1) hFont = item.font; 345 if (hFont != -1) hFont = OS.SelectObject (hDC, hFont); 346 RECT itemRect = item.getBounds (index, true, true, false, false, false, hDC); 347 if (hFont != -1) OS.SelectObject (hDC, hFont); 348 if (parent.hooks (SWT.MeasureItem)) { 349 int nSavedDC = OS.SaveDC (hDC); 350 GCData data = new GCData (); 351 data.device = display; 352 data.hFont = hFont; 353 GC gc = GC.win32_new (hDC, data); 354 Event event = new Event (); 355 event.item = item; 356 event.gc = gc; 357 event.index = index; 358 event.x = itemRect.left; 359 event.y = itemRect.top; 360 event.width = itemRect.right - itemRect.left; 361 event.height = itemRect.bottom - itemRect.top; 362 parent.sendEvent (SWT.MeasureItem, event); 363 event.gc = null; 364 gc.dispose (); 365 OS.RestoreDC (hDC, nSavedDC); 366 if (isDisposed () || parent.isDisposed ()) break; 367 if (event.height > parent.getItemHeight ()) parent.setItemHeight (event.height); 368 itemRect.right = event.x + event.width; 370 } 371 columnWidth = Math.max (columnWidth, itemRect.right - headerRect.left); 372 } 373 tvItem.hItem = OS.SendMessage (hwnd, OS.TVM_GETNEXTITEM, OS.TVGN_NEXTVISIBLE, tvItem.hItem); 374 } 375 RECT rect = new RECT (); 376 int flags = OS.DT_CALCRECT | OS.DT_NOPREFIX; 377 TCHAR buffer = new TCHAR (parent.getCodePage (), text, false); 378 OS.DrawText (hDC, buffer, buffer.length (), rect, flags); 379 int headerWidth = rect.right - rect.left + Tree.HEADER_MARGIN; 380 if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) headerWidth += Tree.HEADER_EXTRA; 381 if (image != null || parent.sortColumn == this) { 382 Image headerImage = null; 383 if (parent.sortColumn == this && parent.sortDirection != SWT.NONE) { 384 if (OS.COMCTL32_MAJOR < 6) { 385 headerImage = display.getSortImage (parent.sortDirection); 386 } else { 387 headerWidth += Tree.SORT_WIDTH; 388 } 389 } else { 390 headerImage = image; 391 } 392 if (headerImage != null) { 393 Rectangle bounds = headerImage.getBounds (); 394 headerWidth += bounds.width; 395 } 396 int margin = 0; 397 if (hwndHeader != 0 && OS.COMCTL32_VERSION >= OS.VERSION (5, 80)) { 398 margin = OS.SendMessage (hwndHeader, OS.HDM_GETBITMAPMARGIN, 0, 0); 399 } else { 400 margin = OS.GetSystemMetrics (OS.SM_CXEDGE) * 3; 401 } 402 headerWidth += margin * 2; 403 } 404 if (newFont != 0) OS.SelectObject (hDC, oldFont); 405 OS.ReleaseDC (hwnd, hDC); 406 int gridWidth = parent.linesVisible ? Tree.GRID_WIDTH : 0; 407 setWidth (Math.max (headerWidth, columnWidth + gridWidth)); 408 } 409 410 void releaseHandle () { 411 super.releaseHandle (); 412 parent = null; 413 } 414 415 void releaseParent () { 416 super.releaseParent (); 417 if (parent.sortColumn == this) { 418 parent.sortColumn = null; 419 } 420 } 421 422 439 public void removeControlListener (ControlListener listener) { 440 checkWidget (); 441 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 442 if (eventTable == null) return; 443 eventTable.unhook (SWT.Move, listener); 444 eventTable.unhook (SWT.Resize, listener); 445 } 446 447 464 public void removeSelectionListener(SelectionListener listener) { 465 checkWidget (); 466 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 467 if (eventTable == null) return; 468 eventTable.unhook (SWT.Selection, listener); 469 eventTable.unhook (SWT.DefaultSelection,listener); 470 } 471 472 484 public void setAlignment (int alignment) { 485 checkWidget (); 486 if ((alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER)) == 0) return; 487 int index = parent.indexOf (this); 488 if (index == -1 || index == 0) return; 489 style &= ~(SWT.LEFT | SWT.RIGHT | SWT.CENTER); 490 style |= alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER); 491 int hwndHeader = parent.hwndHeader; 492 if (hwndHeader == 0) return; 493 HDITEM hdItem = new HDITEM (); 494 hdItem.mask = OS.HDI_FORMAT | OS.HDI_IMAGE; 495 OS.SendMessage (hwndHeader, OS.HDM_GETITEM, index, hdItem); 496 hdItem.fmt &= ~OS.HDF_JUSTIFYMASK; 497 if ((style & SWT.LEFT) == SWT.LEFT) hdItem.fmt |= OS.HDF_LEFT; 498 if ((style & SWT.CENTER) == SWT.CENTER) hdItem.fmt |= OS.HDF_CENTER; 499 if ((style & SWT.RIGHT) == SWT.RIGHT) hdItem.fmt |= OS.HDF_RIGHT; 500 OS.SendMessage (hwndHeader, OS.HDM_SETITEM, index, hdItem); 501 if (index != 0) { 502 int hwnd = parent.handle; 503 parent.forceResize (); 504 RECT rect = new RECT (), headerRect = new RECT (); 505 OS.GetClientRect (hwnd, rect); 506 OS.SendMessage (hwndHeader, OS.HDM_GETITEMRECT, index, headerRect); 507 rect.left = headerRect.left; 508 rect.right = headerRect.right; 509 OS.InvalidateRect (hwnd, rect, true); 510 } 511 } 512 513 public void setImage (Image image) { 514 checkWidget(); 515 if (image != null && image.isDisposed ()) { 516 error (SWT.ERROR_INVALID_ARGUMENT); 517 } 518 super.setImage (image); 519 if (parent.sortColumn != this || parent.sortDirection != SWT.NONE) { 520 setImage (image, false, false); 521 } 522 } 523 524 void setImage (Image image, boolean sort, boolean right) { 525 int index = parent.indexOf (this); 526 if (index == -1) return; 527 int hwndHeader = parent.hwndHeader; 528 if (hwndHeader == 0) return; 529 HDITEM hdItem = new HDITEM (); 530 hdItem.mask = OS.HDI_FORMAT | OS.HDI_IMAGE | OS.HDI_BITMAP; 531 OS.SendMessage (hwndHeader, OS.HDM_GETITEM, index, hdItem); 532 hdItem.fmt &= ~OS.HDF_BITMAP_ON_RIGHT; 533 if (image != null) { 534 if (sort) { 535 hdItem.mask &= ~OS.HDI_IMAGE; 536 hdItem.fmt &= ~OS.HDF_IMAGE; 537 hdItem.fmt |= OS.HDF_BITMAP; 538 hdItem.hbm = image.handle; 539 } else { 540 hdItem.mask &= ~OS.HDI_BITMAP; 541 hdItem.fmt &= ~OS.HDF_BITMAP; 542 hdItem.fmt |= OS.HDF_IMAGE; 543 hdItem.iImage = parent.imageIndexHeader (image); 544 } 545 if (right) hdItem.fmt |= OS.HDF_BITMAP_ON_RIGHT; 546 } else { 547 hdItem.mask &= ~(OS.HDI_IMAGE | OS.HDI_BITMAP); 548 hdItem.fmt &= ~(OS.HDF_IMAGE | OS.HDF_BITMAP); 549 } 550 OS.SendMessage (hwndHeader, OS.HDM_SETITEM, index, hdItem); 551 } 552 553 574 public void setMoveable (boolean moveable) { 575 checkWidget (); 576 this.moveable = moveable; 577 } 578 579 591 public void setResizable (boolean resizable) { 592 checkWidget (); 593 this.resizable = resizable; 594 } 595 596 void setSortDirection (int direction) { 597 if (OS.COMCTL32_MAJOR >= 6) { 598 int hwndHeader = parent.hwndHeader; 599 if (hwndHeader != 0) { 600 int index = parent.indexOf (this); 601 if (index == -1) return; 602 HDITEM hdItem = new HDITEM (); 603 hdItem.mask = OS.HDI_FORMAT | OS.HDI_IMAGE; 604 OS.SendMessage (hwndHeader, OS.HDM_GETITEM, index, hdItem); 605 switch (direction) { 606 case SWT.UP: 607 hdItem.fmt &= ~(OS.HDF_IMAGE | OS.HDF_SORTDOWN); 608 hdItem.fmt |= OS.HDF_SORTUP; 609 break; 610 case SWT.DOWN: 611 hdItem.fmt &= ~(OS.HDF_IMAGE | OS.HDF_SORTUP); 612 hdItem.fmt |= OS.HDF_SORTDOWN; 613 break; 614 case SWT.NONE: 615 hdItem.fmt &= ~(OS.HDF_SORTUP | OS.HDF_SORTDOWN); 616 if (image != null) { 617 hdItem.fmt |= OS.HDF_IMAGE; 618 hdItem.iImage = parent.imageIndexHeader (image); 619 } else { 620 hdItem.fmt &= ~OS.HDF_IMAGE; 621 } 622 break; 623 } 624 OS.SendMessage (hwndHeader, OS.HDM_SETITEM, index, hdItem); 625 if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) { 626 int hwnd = parent.handle; 627 parent.forceResize (); 628 RECT rect = new RECT (), headerRect = new RECT (); 629 OS.GetClientRect (hwnd, rect); 630 OS.SendMessage (hwndHeader, OS.HDM_GETITEMRECT, index, headerRect); 631 rect.left = headerRect.left; 632 rect.right = headerRect.right; 633 OS.InvalidateRect (hwnd, rect, true); 634 } 635 } 636 } else { 637 switch (direction) { 638 case SWT.UP: 639 case SWT.DOWN: 640 setImage (display.getSortImage (direction), true, true); 641 break; 642 case SWT.NONE: 643 setImage (image, false, false); 644 break; 645 } 646 } 647 } 648 649 public void setText (String string) { 650 checkWidget (); 651 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 652 if (string.equals (text)) return; 653 int index = parent.indexOf (this); 654 if (index == -1) return; 655 super.setText (string); 656 664 int hHeap = OS.GetProcessHeap (); 665 TCHAR buffer = new TCHAR (parent.getCodePage (), fixMnemonic (string), true); 666 int byteCount = buffer.length () * TCHAR.sizeof; 667 int pszText = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); 668 OS.MoveMemory (pszText, buffer, byteCount); 669 int hwndHeader = parent.hwndHeader; 670 if (hwndHeader == 0) return; 671 HDITEM hdItem = new HDITEM (); 672 hdItem.mask = OS.HDI_TEXT; 673 hdItem.pszText = pszText; 674 int result = OS.SendMessage (hwndHeader, OS.HDM_SETITEM, index, hdItem); 675 if (pszText != 0) OS.HeapFree (hHeap, 0, pszText); 676 if (result == 0) error (SWT.ERROR_CANNOT_SET_TEXT); 677 } 678 679 692 public void setToolTipText (String string) { 693 checkWidget(); 694 toolTipText = string; 695 int hwndHeaderToolTip = parent.headerToolTipHandle; 696 if (hwndHeaderToolTip == 0) { 697 parent.createHeaderToolTips (); 698 parent.updateHeaderToolTips (); 699 } 700 } 701 702 712 public void setWidth (int width) { 713 checkWidget (); 714 if (width < 0) return; 715 int index = parent.indexOf (this); 716 if (index == -1) return; 717 int hwndHeader = parent.hwndHeader; 718 if (hwndHeader == 0) return; 719 HDITEM hdItem = new HDITEM (); 720 hdItem.mask = OS.HDI_WIDTH; 721 hdItem.cxy = width; 722 OS.SendMessage (hwndHeader, OS.HDM_SETITEM, index, hdItem); 723 RECT headerRect = new RECT (); 724 OS.SendMessage (hwndHeader, OS.HDM_GETITEMRECT, index, headerRect); 725 parent.forceResize (); 726 int hwnd = parent.handle; 727 RECT rect = new RECT (); 728 OS.GetClientRect (hwnd, rect); 729 rect.left = headerRect.left; 730 OS.InvalidateRect (hwnd, rect, true); 731 parent.setScrollWidth (); 732 } 733 734 void updateToolTip (int index) { 735 int hwndHeaderToolTip = parent.headerToolTipHandle; 736 if (hwndHeaderToolTip != 0) { 737 int hwndHeader = parent.hwndHeader; 738 RECT rect = new RECT (); 739 if (OS.SendMessage (hwndHeader, OS.HDM_GETITEMRECT, index, rect) != 0) { 740 TOOLINFO lpti = new TOOLINFO (); 741 lpti.cbSize = TOOLINFO.sizeof; 742 lpti.hwnd = hwndHeader; 743 lpti.uId = id; 744 lpti.left = rect.left; 745 lpti.top = rect.top; 746 lpti.right = rect.right; 747 lpti.bottom = rect.bottom; 748 OS.SendMessage (hwndHeaderToolTip, OS.TTM_NEWTOOLRECT, 0, lpti); 749 } 750 } 751 } 752 } 753 | Popular Tags |