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 33 public class TableColumn extends Item { 34 Table parent; 35 boolean resizable, moveable; 36 String toolTipText; 37 int id; 38 39 71 public TableColumn (Table parent, int style) { 72 super (parent, checkStyle (style)); 73 resizable = true; 74 this.parent = parent; 75 parent.createItem (this, parent.getColumnCount ()); 76 } 77 78 112 public TableColumn (Table parent, int style, int index) { 113 super (parent, checkStyle (style)); 114 resizable = true; 115 this.parent = parent; 116 parent.createItem (this, index); 117 } 118 119 138 public void addControlListener(ControlListener listener) { 139 checkWidget (); 140 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 141 TypedListener typedListener = new TypedListener (listener); 142 addListener (SWT.Resize,typedListener); 143 addListener (SWT.Move,typedListener); 144 } 145 146 170 public void addSelectionListener (SelectionListener listener) { 171 checkWidget (); 172 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 173 TypedListener typedListener = new TypedListener (listener); 174 addListener (SWT.Selection,typedListener); 175 addListener (SWT.DefaultSelection,typedListener); 176 } 177 178 static int checkStyle (int style) { 179 return checkBits (style, SWT.LEFT, SWT.CENTER, SWT.RIGHT, 0, 0, 0); 180 } 181 182 protected void checkSubclass () { 183 if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS); 184 } 185 186 void destroyWidget () { 187 parent.destroyItem (this); 188 releaseHandle (); 189 } 190 191 203 public int getAlignment () { 204 checkWidget (); 205 if ((style & SWT.LEFT) != 0) return SWT.LEFT; 206 if ((style & SWT.CENTER) != 0) return SWT.CENTER; 207 if ((style & SWT.RIGHT) != 0) return SWT.RIGHT; 208 return SWT.LEFT; 209 } 210 211 String getNameText () { 212 return getText (); 213 } 214 215 225 public Table getParent () { 226 checkWidget (); 227 return parent; 228 } 229 230 250 public boolean getMoveable () { 251 checkWidget (); 252 return moveable; 253 } 254 255 267 public boolean getResizable () { 268 checkWidget (); 269 return resizable; 270 } 271 272 285 public String getToolTipText () { 286 checkWidget(); 287 return toolTipText; 288 } 289 290 300 public int getWidth () { 301 checkWidget (); 302 int index = parent.indexOf (this); 303 if (index == -1) return 0; 304 int hwnd = parent.handle; 305 return OS.SendMessage (hwnd, OS.LVM_GETCOLUMNWIDTH, index, 0); 306 } 307 308 319 public void pack () { 320 checkWidget (); 321 int index = parent.indexOf (this); 322 if (index == -1) return; 323 int hwnd = parent.handle; 324 int oldWidth = OS.SendMessage (hwnd, OS.LVM_GETCOLUMNWIDTH, index, 0); 325 TCHAR buffer = new TCHAR (parent.getCodePage (), text, true); 326 int headerWidth = OS.SendMessage (hwnd, OS.LVM_GETSTRINGWIDTH, 0, buffer) + Table.HEADER_MARGIN; 327 if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) headerWidth += Table.HEADER_EXTRA; 328 boolean hasHeaderImage = false; 329 if (image != null || parent.sortColumn == this) { 330 hasHeaderImage = true; 331 Image headerImage = null; 332 if (parent.sortColumn == this && parent.sortDirection != SWT.NONE) { 333 if (OS.COMCTL32_MAJOR < 6) { 334 headerImage = display.getSortImage (parent.sortDirection); 335 } else { 336 headerWidth += Table.SORT_WIDTH; 337 } 338 } else { 339 headerImage = image; 340 } 341 if (headerImage != null) { 342 Rectangle bounds = headerImage.getBounds (); 343 headerWidth += bounds.width; 344 } 345 int margin = 0; 346 if (OS.COMCTL32_VERSION >= OS.VERSION (5, 80)) { 347 int hwndHeader = OS.SendMessage (hwnd, OS.LVM_GETHEADER, 0, 0); 348 margin = OS.SendMessage (hwndHeader, OS.HDM_GETBITMAPMARGIN, 0, 0); 349 } else { 350 margin = OS.GetSystemMetrics (OS.SM_CXEDGE) * 3; 351 } 352 headerWidth += margin * 4; 353 } 354 parent.ignoreColumnResize = true; 355 int columnWidth = 0; 356 363 if ((index == 0 && !parent.firstColumnImage) || parent.hooks (SWT.MeasureItem)) { 364 RECT headerRect = new RECT (); 365 int hwndHeader = OS.SendMessage (hwnd, OS.LVM_GETHEADER, 0, 0); 366 OS.SendMessage (hwndHeader, OS.HDM_GETITEMRECT, index, headerRect); 367 OS.MapWindowPoints (hwndHeader, hwnd, headerRect, 2); 368 int hDC = OS.GetDC (hwnd); 369 int oldFont = 0, newFont = OS.SendMessage (hwnd, OS.WM_GETFONT, 0, 0); 370 if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont); 371 int count = OS.SendMessage (hwnd, OS.LVM_GETITEMCOUNT, 0, 0); 372 for (int i=0; i<count; i++) { 373 TableItem item = parent.items [i]; 374 if (item != null) { 375 int hFont = item.cellFont != null ? item.cellFont [index] : -1; 376 if (hFont == -1) hFont = item.font; 377 if (hFont != -1) hFont = OS.SelectObject (hDC, hFont); 378 Event event = parent.sendMeasureItemEvent (item, i, index, hDC); 379 if (hFont != -1) hFont = OS.SelectObject (hDC, hFont); 380 if (isDisposed () || parent.isDisposed ()) break; 381 columnWidth = Math.max (columnWidth, event.x + event.width - headerRect.left); 382 } 383 } 384 if (newFont != 0) OS.SelectObject (hDC, oldFont); 385 OS.ReleaseDC (hwnd, hDC); 386 OS.SendMessage (hwnd, OS.LVM_SETCOLUMNWIDTH, index, columnWidth); 387 } else { 388 OS.SendMessage (hwnd, OS.LVM_SETCOLUMNWIDTH, index, OS.LVSCW_AUTOSIZE); 389 columnWidth = OS.SendMessage (hwnd, OS.LVM_GETCOLUMNWIDTH, index, 0); 390 if (index == 0) { 391 398 if (parent.imageList == null) columnWidth += 2; 399 405 if ((parent.style & SWT.CHECK) != 0) { 406 int hStateList = OS.SendMessage (hwnd, OS.LVM_GETIMAGELIST, OS.LVSIL_STATE, 0); 407 if (hStateList != 0) { 408 int [] cx = new int [1], cy = new int [1]; 409 OS.ImageList_GetIconSize (hStateList, cx, cy); 410 columnWidth += cx [0]; 411 } 412 } 413 } 414 } 415 if (headerWidth > columnWidth) { 416 if (!hasHeaderImage) { 417 424 RECT rect = null; 425 boolean fixWidth = index == parent.getColumnCount () - 1; 426 if (fixWidth) { 427 rect = new RECT (); 428 OS.GetWindowRect (hwnd, rect); 429 OS.UpdateWindow (hwnd); 430 int flags = OS.SWP_NOACTIVATE | OS.SWP_NOMOVE | OS.SWP_NOREDRAW | OS.SWP_NOZORDER; 431 SetWindowPos (hwnd, 0, 0, 0, 0, rect.bottom - rect.top, flags); 432 } 433 OS.SendMessage (hwnd, OS.LVM_SETCOLUMNWIDTH, index, OS.LVSCW_AUTOSIZE_USEHEADER); 434 if (fixWidth) { 435 int flags = OS.SWP_NOACTIVATE | OS.SWP_NOMOVE | OS.SWP_NOZORDER; 436 SetWindowPos (hwnd, 0, 0, 0, rect.right - rect.left, rect.bottom - rect.top, flags); 437 } 438 } else { 439 OS.SendMessage (hwnd, OS.LVM_SETCOLUMNWIDTH, index, headerWidth); 440 } 441 } else { 442 if (index == 0) { 443 OS.SendMessage (hwnd, OS.LVM_SETCOLUMNWIDTH, index, columnWidth); 444 } 445 } 446 parent.ignoreColumnResize = false; 447 int newWidth = OS.SendMessage (hwnd, OS.LVM_GETCOLUMNWIDTH, index, 0); 448 if (oldWidth != newWidth) { 449 updateToolTip (index); 450 sendEvent (SWT.Resize); 451 if (isDisposed ()) return; 452 boolean moved = false; 453 int [] order = parent.getColumnOrder (); 454 TableColumn [] columns = parent.getColumns (); 455 for (int i=0; i<order.length; i++) { 456 TableColumn column = columns [order [i]]; 457 if (moved && !column.isDisposed ()) { 458 column.updateToolTip (order [i]); 459 column.sendEvent (SWT.Move); 460 } 461 if (column == this) moved = true; 462 } 463 } 464 } 465 466 void releaseHandle () { 467 super.releaseHandle (); 468 parent = null; 469 } 470 471 void releaseParent () { 472 super.releaseParent (); 473 if (parent.sortColumn == this) { 474 parent.sortColumn = null; 475 } 476 } 477 478 495 public void removeControlListener (ControlListener listener) { 496 checkWidget (); 497 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 498 if (eventTable == null) return; 499 eventTable.unhook (SWT.Move, listener); 500 eventTable.unhook (SWT.Resize, listener); 501 } 502 503 520 public void removeSelectionListener(SelectionListener listener) { 521 checkWidget (); 522 if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); 523 if (eventTable == null) return; 524 eventTable.unhook (SWT.Selection, listener); 525 eventTable.unhook (SWT.DefaultSelection,listener); 526 } 527 528 540 public void setAlignment (int alignment) { 541 checkWidget (); 542 if ((alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER)) == 0) return; 543 int index = parent.indexOf (this); 544 if (index == -1 || index == 0) return; 545 style &= ~(SWT.LEFT | SWT.RIGHT | SWT.CENTER); 546 style |= alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER); 547 int hwnd = parent.handle; 548 LVCOLUMN lvColumn = new LVCOLUMN (); 549 lvColumn.mask = OS.LVCF_FMT | OS.LVCF_IMAGE; 550 OS.SendMessage (hwnd, OS.LVM_GETCOLUMN, index, lvColumn); 551 lvColumn.fmt &= ~OS.LVCFMT_JUSTIFYMASK; 552 int fmt = 0; 553 if ((style & SWT.LEFT) == SWT.LEFT) fmt = OS.LVCFMT_LEFT; 554 if ((style & SWT.CENTER) == SWT.CENTER) fmt = OS.LVCFMT_CENTER; 555 if ((style & SWT.RIGHT) == SWT.RIGHT) fmt = OS.LVCFMT_RIGHT; 556 lvColumn.fmt |= fmt; 557 OS.SendMessage (hwnd, OS.LVM_SETCOLUMN, index, lvColumn); 558 564 if (index != 0) { 565 parent.forceResize (); 566 RECT rect = new RECT (), headerRect = new RECT (); 567 OS.GetClientRect (hwnd, rect); 568 int hwndHeader = OS.SendMessage (hwnd, OS.LVM_GETHEADER, 0, 0); 569 OS.SendMessage (hwndHeader, OS.HDM_GETITEMRECT, index, headerRect); 570 OS.MapWindowPoints (hwndHeader, hwnd, headerRect, 2); 571 rect.left = headerRect.left; 572 rect.right = headerRect.right; 573 OS.InvalidateRect (hwnd, rect, true); 574 } 575 } 576 577 public void setImage (Image image) { 578 checkWidget(); 579 if (image != null && image.isDisposed ()) { 580 error (SWT.ERROR_INVALID_ARGUMENT); 581 } 582 super.setImage (image); 583 if (parent.sortColumn != this || parent.sortDirection != SWT.NONE) { 584 setImage (image, false, false); 585 } 586 } 587 588 void setImage (Image image, boolean sort, boolean right) { 589 int index = parent.indexOf (this); 590 if (index == -1) return; 591 int hwnd = parent.handle; 592 if (OS.COMCTL32_MAJOR < 6) { 593 int hwndHeader = OS.SendMessage (hwnd, OS.LVM_GETHEADER, 0, 0); 594 HDITEM hdItem = new HDITEM (); 595 hdItem.mask = OS.HDI_FORMAT | OS.HDI_IMAGE | OS.HDI_BITMAP; 596 OS.SendMessage (hwndHeader, OS.HDM_GETITEM, index, hdItem); 597 hdItem.fmt &= ~OS.HDF_BITMAP_ON_RIGHT; 598 if (image != null) { 599 if (sort) { 600 hdItem.mask &= ~OS.HDI_IMAGE; 601 hdItem.fmt &= ~OS.HDF_IMAGE; 602 hdItem.fmt |= OS.HDF_BITMAP; 603 hdItem.hbm = image.handle; 604 } else { 605 hdItem.mask &= ~OS.HDI_BITMAP; 606 hdItem.fmt &= ~OS.HDF_BITMAP; 607 hdItem.fmt |= OS.HDF_IMAGE; 608 hdItem.iImage = parent.imageIndexHeader (image); 609 } 610 if (right) hdItem.fmt |= OS.HDF_BITMAP_ON_RIGHT; 611 } else { 612 hdItem.fmt &= ~(OS.HDF_IMAGE | OS.HDF_BITMAP); 613 } 614 OS.SendMessage (hwndHeader, OS.HDM_SETITEM, index, hdItem); 615 } else { 616 LVCOLUMN lvColumn = new LVCOLUMN (); 617 lvColumn.mask = OS.LVCF_FMT | OS.LVCF_IMAGE; 618 OS.SendMessage (hwnd, OS.LVM_GETCOLUMN, index, lvColumn); 619 if (image != null) { 620 lvColumn.fmt |= OS.LVCFMT_IMAGE; 621 lvColumn.iImage = parent.imageIndexHeader (image); 622 if (right) lvColumn.fmt |= OS.LVCFMT_BITMAP_ON_RIGHT; 623 } else { 624 lvColumn.mask &= ~OS.LVCF_IMAGE; 625 lvColumn.fmt &= ~(OS.LVCFMT_IMAGE | OS.LVCFMT_BITMAP_ON_RIGHT); 626 } 627 OS.SendMessage (hwnd, OS.LVM_SETCOLUMN, index, lvColumn); 628 } 629 } 630 631 652 public void setMoveable (boolean moveable) { 653 checkWidget (); 654 this.moveable = moveable; 655 parent.updateMoveable (); 656 } 657 658 672 public void setResizable (boolean resizable) { 673 checkWidget (); 674 this.resizable = resizable; 675 } 676 677 void setSortDirection (int direction) { 678 if (OS.COMCTL32_MAJOR >= 6) { 679 int index = parent.indexOf (this); 680 if (index == -1) return; 681 int hwnd = parent.handle; 682 int hwndHeader = OS.SendMessage (hwnd, OS.LVM_GETHEADER, 0, 0); 683 HDITEM hdItem = new HDITEM (); 684 hdItem.mask = OS.HDI_FORMAT | OS.HDI_IMAGE; 685 OS.SendMessage (hwndHeader, OS.HDM_GETITEM, index, hdItem); 686 switch (direction) { 687 case SWT.UP: 688 hdItem.fmt &= ~(OS.HDF_IMAGE | OS.HDF_SORTDOWN); 689 hdItem.fmt |= OS.HDF_SORTUP; 690 break; 691 case SWT.DOWN: 692 hdItem.fmt &= ~(OS.HDF_IMAGE | OS.HDF_SORTUP); 693 hdItem.fmt |= OS.HDF_SORTDOWN; 694 break; 695 case SWT.NONE: 696 hdItem.fmt &= ~(OS.HDF_SORTUP | OS.HDF_SORTDOWN); 697 if (image != null) { 698 hdItem.fmt |= OS.HDF_IMAGE; 699 hdItem.iImage = parent.imageIndexHeader (image); 700 } else { 701 hdItem.fmt &= ~OS.HDF_IMAGE; 702 hdItem.mask &= ~OS.HDI_IMAGE; 703 } 704 break; 705 } 706 OS.SendMessage (hwndHeader, OS.HDM_SETITEM, index, hdItem); 707 720 parent.forceResize (); 721 RECT rect = new RECT (); 722 OS.GetClientRect (hwnd, rect); 723 if (OS.SendMessage (hwnd, OS.LVM_GETBKCOLOR, 0, 0) != OS.CLR_NONE) { 724 int oldColumn = OS.SendMessage (hwnd, OS.LVM_GETSELECTEDCOLUMN, 0, 0); 725 int newColumn = direction == SWT.NONE ? -1 : index; 726 OS.SendMessage (hwnd, OS.LVM_SETSELECTEDCOLUMN, newColumn, 0); 727 RECT headerRect = new RECT (); 728 if (oldColumn != -1) { 729 if (OS.SendMessage (hwndHeader, OS.HDM_GETITEMRECT, oldColumn, headerRect) != 0) { 730 OS.MapWindowPoints (hwndHeader, hwnd, headerRect, 2); 731 rect.left = headerRect.left; 732 rect.right = headerRect.right; 733 OS.InvalidateRect (hwnd, rect, true); 734 } 735 } 736 } 737 RECT headerRect = new RECT (); 738 if (OS.SendMessage (hwndHeader, OS.HDM_GETITEMRECT, index, headerRect) != 0) { 739 OS.MapWindowPoints (hwndHeader, hwnd, headerRect, 2); 740 rect.left = headerRect.left; 741 rect.right = headerRect.right; 742 OS.InvalidateRect (hwnd, rect, true); 743 } 744 } else { 745 switch (direction) { 746 case SWT.UP: 747 case SWT.DOWN: 748 setImage (display.getSortImage (direction), true, true); 749 break; 750 case SWT.NONE: 751 setImage (image, false, false); 752 break; 753 } 754 } 755 } 756 757 public void setText (String string) { 758 checkWidget (); 759 if (string == null) error (SWT.ERROR_NULL_ARGUMENT); 760 if (string.equals (text)) return; 761 int index = parent.indexOf (this); 762 if (index == -1) return; 763 super.setText (string); 764 765 772 int hwnd = parent.handle; 773 LVCOLUMN lvColumn = new LVCOLUMN (); 774 lvColumn.mask = OS.LVCF_FMT; 775 OS.SendMessage (hwnd, OS.LVM_GETCOLUMN, index, lvColumn); 776 777 785 int hHeap = OS.GetProcessHeap (); 786 TCHAR buffer = new TCHAR (parent.getCodePage (), fixMnemonic (string), true); 787 int byteCount = buffer.length () * TCHAR.sizeof; 788 int pszText = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount); 789 OS.MoveMemory (pszText, buffer, byteCount); 790 lvColumn.mask |= OS.LVCF_TEXT; 791 lvColumn.pszText = pszText; 792 int result = OS.SendMessage (hwnd, OS.LVM_SETCOLUMN, index, lvColumn); 793 if (pszText != 0) OS.HeapFree (hHeap, 0, pszText); 794 if (result == 0) error (SWT.ERROR_CANNOT_SET_TEXT); 795 } 796 797 810 public void setToolTipText (String string) { 811 checkWidget(); 812 toolTipText = string; 813 int hwndHeaderToolTip = parent.headerToolTipHandle; 814 if (hwndHeaderToolTip == 0) { 815 parent.createHeaderToolTips (); 816 parent.updateHeaderToolTips (); 817 } 818 } 819 820 830 public void setWidth (int width) { 831 checkWidget (); 832 if (width < 0) return; 833 int index = parent.indexOf (this); 834 if (index == -1) return; 835 int hwnd = parent.handle; 836 OS.SendMessage (hwnd, OS.LVM_SETCOLUMNWIDTH, index, width); 837 } 838 839 void updateToolTip (int index) { 840 int hwndHeaderToolTip = parent.headerToolTipHandle; 841 if (hwndHeaderToolTip != 0) { 842 int hwnd = parent.handle; 843 int hwndHeader = OS.SendMessage (hwnd, OS.LVM_GETHEADER, 0, 0); 844 RECT rect = new RECT (); 845 if (OS.SendMessage (hwndHeader, OS.HDM_GETITEMRECT, index, rect) != 0) { 846 TOOLINFO lpti = new TOOLINFO (); 847 lpti.cbSize = TOOLINFO.sizeof; 848 lpti.hwnd = hwndHeader; 849 lpti.uId = id; 850 lpti.left = rect.left; 851 lpti.top = rect.top; 852 lpti.right = rect.right; 853 lpti.bottom = rect.bottom; 854 OS.SendMessage (hwndHeaderToolTip, OS.TTM_NEWTOOLRECT, 0, lpti); 855 } 856 } 857 } 858 859 } 860 | Popular Tags |