KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > widgets > TreeColumn


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
20  * Instances of this class represent a column in a tree widget.
21  * <p><dl>
22  * <dt><b>Styles:</b></dt>
23  * <dd>LEFT, RIGHT, CENTER</dd>
24  * <dt><b>Events:</b></dt>
25  * <dd> Move, Resize, Selection</dd>
26  * </dl>
27  * </p><p>
28  * Note: Only one of the styles LEFT, RIGHT and CENTER may be specified.
29  * </p><p>
30  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
31  * </p>
32  *
33  * @since 3.1
34  */

35 public class TreeColumn extends Item {
36     Tree parent;
37     boolean resizable, moveable;
38     String JavaDoc toolTipText;
39     int id;
40
41 /**
42  * Constructs a new instance of this class given its parent
43  * (which must be a <code>Tree</code>) and a style value
44  * describing its behavior and appearance. The item is added
45  * to the end of the items maintained by its parent.
46  * <p>
47  * The style value is either one of the style constants defined in
48  * class <code>SWT</code> which is applicable to instances of this
49  * class, or must be built by <em>bitwise OR</em>'ing together
50  * (that is, using the <code>int</code> "|" operator) two or more
51  * of those <code>SWT</code> style constants. The class description
52  * lists the style constants that are applicable to the class.
53  * Style bits are also inherited from superclasses.
54  * </p>
55  *
56  * @param parent a composite control which will be the parent of the new instance (cannot be null)
57  * @param style the style of control to construct
58  *
59  * @exception IllegalArgumentException <ul>
60  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
61  * </ul>
62  * @exception SWTException <ul>
63  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
64  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
65  * </ul>
66  *
67  * @see SWT#LEFT
68  * @see SWT#RIGHT
69  * @see SWT#CENTER
70  * @see Widget#checkSubclass
71  * @see Widget#getStyle
72  */

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 /**
81  * Constructs a new instance of this class given its parent
82  * (which must be a <code>Tree</code>), a style value
83  * describing its behavior and appearance, and the index
84  * at which to place it in the items maintained by its parent.
85  * <p>
86  * The style value is either one of the style constants defined in
87  * class <code>SWT</code> which is applicable to instances of this
88  * class, or must be built by <em>bitwise OR</em>'ing together
89  * (that is, using the <code>int</code> "|" operator) two or more
90  * of those <code>SWT</code> style constants. The class description
91  * lists the style constants that are applicable to the class.
92  * Style bits are also inherited from superclasses.
93  * </p>
94  *
95  * @param parent a composite control which will be the parent of the new instance (cannot be null)
96  * @param style the style of control to construct
97  * @param index the zero-relative index to store the receiver in its parent
98  *
99  * @exception IllegalArgumentException <ul>
100  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
101  * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the parent (inclusive)</li>
102  * </ul>
103  * @exception SWTException <ul>
104  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
105  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
106  * </ul>
107  *
108  * @see SWT#LEFT
109  * @see SWT#RIGHT
110  * @see SWT#CENTER
111  * @see Widget#checkSubclass
112  * @see Widget#getStyle
113  */

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 /**
122  * Adds the listener to the collection of listeners who will
123  * be notified when the control is moved or resized, by sending
124  * it one of the messages defined in the <code>ControlListener</code>
125  * interface.
126  *
127  * @param listener the listener which should be notified
128  *
129  * @exception IllegalArgumentException <ul>
130  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
131  * </ul>
132  * @exception SWTException <ul>
133  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
134  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
135  * </ul>
136  *
137  * @see ControlListener
138  * @see #removeControlListener
139  */

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 /**
149  * Adds the listener to the collection of listeners who will
150  * be notified when the control is selected by the user, by sending
151  * it one of the messages defined in the <code>SelectionListener</code>
152  * interface.
153  * <p>
154  * <code>widgetSelected</code> is called when the column header is selected.
155  * <code>widgetDefaultSelected</code> is not called.
156  * </p>
157  *
158  * @param listener the listener which should be notified when the control is selected by the user
159  *
160  * @exception IllegalArgumentException <ul>
161  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
162  * </ul>
163  * @exception SWTException <ul>
164  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
165  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
166  * </ul>
167  *
168  * @see SelectionListener
169  * @see #removeSelectionListener
170  * @see SelectionEvent
171  */

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 /**
194  * Returns a value which describes the position of the
195  * text or image in the receiver. The value will be one of
196  * <code>LEFT</code>, <code>RIGHT</code> or <code>CENTER</code>.
197  *
198  * @return the alignment
199  *
200  * @exception SWTException <ul>
201  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
202  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
203  * </ul>
204  */

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 /**
214  * Gets the moveable attribute. A column that is
215  * not moveable cannot be reordered by the user
216  * by dragging the header but may be reordered
217  * by the programmer.
218  *
219  * @return the moveable attribute
220  *
221  * @exception SWTException <ul>
222  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
223  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
224  * </ul>
225  *
226  * @see Tree#getColumnOrder()
227  * @see Tree#setColumnOrder(int[])
228  * @see TreeColumn#setMoveable(boolean)
229  * @see SWT#Move
230  *
231  * @since 3.2
232  */

233 public boolean getMoveable () {
234     checkWidget ();
235     return moveable;
236 }
237
238 String JavaDoc getNameText () {
239     return getText ();
240 }
241
242 /**
243  * Returns the receiver's parent, which must be a <code>Tree</code>.
244  *
245  * @return the receiver's parent
246  *
247  * @exception SWTException <ul>
248  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
249  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
250  * </ul>
251  */

252 public Tree getParent () {
253     checkWidget ();
254     return parent;
255 }
256
257 /**
258  * Gets the resizable attribute. A column that is
259  * not resizable cannot be dragged by the user but
260  * may be resized by the programmer.
261  *
262  * @return the resizable attribute
263  *
264  * @exception SWTException <ul>
265  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
266  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
267  * </ul>
268  */

269 public boolean getResizable () {
270     checkWidget ();
271     return resizable;
272 }
273
274 /**
275  * Returns the receiver's tool tip text, or null if it has
276  * not been set.
277  *
278  * @return the receiver's tool tip text
279  *
280  * @exception SWTException <ul>
281  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
282  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
283  * </ul>
284  *
285  * @since 3.2
286  */

287 public String JavaDoc getToolTipText () {
288     checkWidget();
289     return toolTipText;
290 }
291
292 /**
293  * Gets the width of the receiver.
294  *
295  * @return the width
296  *
297  * @exception SWTException <ul>
298  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
299  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
300  * </ul>
301  */

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 /**
315  * Causes the receiver to be resized to its preferred size.
316  * For a composite, this involves computing the preferred size
317  * from its layout, if there is one.
318  *
319  * @exception SWTException <ul>
320  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
321  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
322  * </ul>
323  *
324  */

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.left = event.x;
369
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 /**
423  * Removes the listener from the collection of listeners who will
424  * be notified when the control is moved or resized.
425  *
426  * @param listener the listener which should no longer be notified
427  *
428  * @exception IllegalArgumentException <ul>
429  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
430  * </ul>
431  * @exception SWTException <ul>
432  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
433  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
434  * </ul>
435  *
436  * @see ControlListener
437  * @see #addControlListener
438  */

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 /**
448  * Removes the listener from the collection of listeners who will
449  * be notified when the control is selected by the user.
450  *
451  * @param listener the listener which should no longer be notified
452  *
453  * @exception IllegalArgumentException <ul>
454  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
455  * </ul>
456  * @exception SWTException <ul>
457  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
458  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
459  * </ul>
460  *
461  * @see SelectionListener
462  * @see #addSelectionListener
463  */

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 /**
473  * Controls how text and images will be displayed in the receiver.
474  * The argument should be one of <code>LEFT</code>, <code>RIGHT</code>
475  * or <code>CENTER</code>.
476  *
477  * @param alignment the new alignment
478  *
479  * @exception SWTException <ul>
480  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
481  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
482  * </ul>
483  */

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 /**
554  * Sets the moveable attribute. A column that is
555  * moveable can be reordered by the user by dragging
556  * the header. A column that is not moveable cannot be
557  * dragged by the user but may be reordered
558  * by the programmer.
559  *
560  * @param moveable the moveable attribute
561  *
562  * @exception SWTException <ul>
563  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
564  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
565  * </ul>
566  *
567  * @see Tree#setColumnOrder(int[])
568  * @see Tree#getColumnOrder()
569  * @see TreeColumn#getMoveable()
570  * @see SWT#Move
571  *
572  * @since 3.2
573  */

574 public void setMoveable (boolean moveable) {
575     checkWidget ();
576     this.moveable = moveable;
577 }
578
579 /**
580  * Sets the resizable attribute. A column that is
581  * not resizable cannot be dragged by the user but
582  * may be resized by the programmer.
583  *
584  * @param resizable the resize attribute
585  *
586  * @exception SWTException <ul>
587  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
588  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
589  * </ul>
590  */

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 JavaDoc 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     /*
657     * Bug in Windows. When a column header contains a
658     * mnemonic character, Windows does not measure the
659     * text properly. This causes '...' to always appear
660     * at the end of the text. The fix is to remove
661     * mnemonic characters and replace doubled mnemonics
662     * with spaces.
663     */

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 /**
680  * Sets the receiver's tool tip text to the argument, which
681  * may be null indicating that no tool tip text should be shown.
682  *
683  * @param string the new tool tip text (or null)
684  *
685  * @exception SWTException <ul>
686  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
687  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
688  * </ul>
689  *
690  * @since 3.2
691  */

692 public void setToolTipText (String JavaDoc 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 /**
703  * Sets the width of the receiver.
704  *
705  * @param width the new width
706  *
707  * @exception SWTException <ul>
708  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
709  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
710  * </ul>
711  */

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