Mercurial > projects > dwt2
comparison org.eclipse.swt.win32.win32.x86/src/org/eclipse/swt/dnd/TableDragSourceEffect.d @ 0:6dd524f61e62
add dwt win and basic java stuff
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 02 Mar 2009 14:44:16 +0100 |
parents | |
children | 9f4c18c268b2 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:6dd524f61e62 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2007, 2008 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module org.eclipse.swt.dnd.TableDragSourceEffect; | |
14 | |
15 import org.eclipse.swt.SWT; | |
16 import org.eclipse.swt.graphics.Image; | |
17 import org.eclipse.swt.graphics.ImageData; | |
18 import org.eclipse.swt.graphics.PaletteData; | |
19 import org.eclipse.swt.graphics.Rectangle; | |
20 import org.eclipse.swt.internal.win32.OS; | |
21 import org.eclipse.swt.widgets.Display; | |
22 import org.eclipse.swt.widgets.Table; | |
23 import org.eclipse.swt.widgets.TableItem; | |
24 | |
25 import org.eclipse.swt.dnd.DragSourceEffect; | |
26 import org.eclipse.swt.dnd.DragSourceEvent; | |
27 | |
28 import java.lang.all; | |
29 | |
30 /** | |
31 * This class provides default implementations to display a source image | |
32 * when a drag is initiated from a <code>Table</code>. | |
33 * | |
34 * <p>Classes that wish to provide their own source image for a <code>Table</code> can | |
35 * extend the <code>TableDragSourceEffect</code> class, override the | |
36 * <code>TableDragSourceEffect.dragStart</code> method and set the field | |
37 * <code>DragSourceEvent.image</code> with their own image.</p> | |
38 * | |
39 * Subclasses that override any methods of this class must call the corresponding | |
40 * <code>super</code> method to get the default drag source effect implementation. | |
41 * | |
42 * @see DragSourceEffect | |
43 * @see DragSourceEvent | |
44 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> | |
45 * | |
46 * @since 3.3 | |
47 */ | |
48 public class TableDragSourceEffect : DragSourceEffect { | |
49 Image dragSourceImage = null; | |
50 | |
51 /** | |
52 * Creates a new <code>TableDragSourceEffect</code> to handle drag effect | |
53 * from the specified <code>Table</code>. | |
54 * | |
55 * @param table the <code>Table</code> that the user clicks on to initiate the drag | |
56 */ | |
57 public this(Table table) { | |
58 super(table); | |
59 } | |
60 | |
61 /** | |
62 * This implementation of <code>dragFinished</code> disposes the image | |
63 * that was created in <code>TableDragSourceEffect.dragStart</code>. | |
64 * | |
65 * Subclasses that override this method should call <code>super.dragFinished(event)</code> | |
66 * to dispose the image in the default implementation. | |
67 * | |
68 * @param event the information associated with the drag finished event | |
69 */ | |
70 public void dragFinished(DragSourceEvent event) { | |
71 if (dragSourceImage !is null) dragSourceImage.dispose(); | |
72 dragSourceImage = null; | |
73 } | |
74 | |
75 /** | |
76 * This implementation of <code>dragStart</code> will create a default | |
77 * image that will be used during the drag. The image should be disposed | |
78 * when the drag is completed in the <code>TableDragSourceEffect.dragFinished</code> | |
79 * method. | |
80 * | |
81 * Subclasses that override this method should call <code>super.dragStart(event)</code> | |
82 * to use the image from the default implementation. | |
83 * | |
84 * @param event the information associated with the drag start event | |
85 */ | |
86 public void dragStart(DragSourceEvent event) { | |
87 event.image = getDragSourceImage(event); | |
88 } | |
89 | |
90 Image getDragSourceImage(DragSourceEvent event) { | |
91 if (dragSourceImage !is null) dragSourceImage.dispose(); | |
92 dragSourceImage = null; | |
93 if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION (5, 1)) { | |
94 SHDRAGIMAGE shdi; | |
95 int DI_GETDRAGIMAGE = OS.RegisterWindowMessage ( "ShellGetDragImage"w.ptr ); //$NON-NLS-1$ | |
96 if (OS.SendMessage (control.handle, DI_GETDRAGIMAGE, 0, &shdi) !is 0) { | |
97 if ((control.getStyle() & SWT.MIRRORED) !is 0) { | |
98 event.x += shdi.sizeDragImage.cx - shdi.ptOffset.x; | |
99 } else { | |
100 event.x += shdi.ptOffset.x; | |
101 } | |
102 event.y += shdi.ptOffset.y; | |
103 auto hImage = shdi.hbmpDragImage; | |
104 if (hImage !is null) { | |
105 BITMAP bm; | |
106 OS.GetObject (hImage, BITMAP.sizeof, &bm); | |
107 int srcWidth = bm.bmWidth; | |
108 int srcHeight = bm.bmHeight; | |
109 | |
110 /* Create resources */ | |
111 auto hdc = OS.GetDC (null); | |
112 auto srcHdc = OS.CreateCompatibleDC (hdc); | |
113 auto oldSrcBitmap = OS.SelectObject (srcHdc, hImage); | |
114 auto memHdc = OS.CreateCompatibleDC (hdc); | |
115 BITMAPINFOHEADER bmiHeader; | |
116 bmiHeader.biSize = BITMAPINFOHEADER.sizeof; | |
117 bmiHeader.biWidth = srcWidth; | |
118 bmiHeader.biHeight = -srcHeight; | |
119 bmiHeader.biPlanes = 1; | |
120 bmiHeader.biBitCount = 32; | |
121 bmiHeader.biCompression = OS.BI_RGB; | |
122 void* pBits; | |
123 auto memDib = OS.CreateDIBSection (null, cast(BITMAPINFO*)&bmiHeader, OS.DIB_RGB_COLORS, &pBits, null, 0); | |
124 if (memDib is null) SWT.error (SWT.ERROR_NO_HANDLES); | |
125 auto oldMemBitmap = OS.SelectObject (memHdc, memDib); | |
126 | |
127 BITMAP dibBM; | |
128 OS.GetObject (memDib, BITMAP.sizeof, &dibBM); | |
129 int sizeInBytes = dibBM.bmWidthBytes * dibBM.bmHeight; | |
130 | |
131 /* Get the foreground pixels */ | |
132 OS.BitBlt (memHdc, 0, 0, srcWidth, srcHeight, srcHdc, 0, 0, OS.SRCCOPY); | |
133 //byte[] srcData = new byte [sizeInBytes]; | |
134 //OS.MoveMemory (srcData, dibBM.bmBits, sizeInBytes); | |
135 byte[] srcData = (cast(byte*) dibBM.bmBits)[ 0 .. BITMAPINFOHEADER.sizeof ]; | |
136 | |
137 PaletteData palette = new PaletteData(0xFF00, 0xFF0000, 0xFF000000); | |
138 ImageData data = new ImageData(srcWidth, srcHeight, bm.bmBitsPixel, palette, bm.bmWidthBytes, srcData); | |
139 if (shdi.crColorKey is -1) { | |
140 byte[] alphaData = new byte[srcWidth * srcHeight]; | |
141 int spinc = dibBM.bmWidthBytes - srcWidth * 4; | |
142 int ap = 0, sp = 3; | |
143 for (int y = 0; y < srcHeight; ++y) { | |
144 for (int x = 0; x < srcWidth; ++x) { | |
145 alphaData [ap++] = srcData [sp]; | |
146 sp += 4; | |
147 } | |
148 sp += spinc; | |
149 } | |
150 data.alphaData = alphaData; | |
151 } else { | |
152 data.transparentPixel = shdi.crColorKey << 8; | |
153 } | |
154 dragSourceImage = new Image(control.getDisplay(), data); | |
155 OS.SelectObject (memHdc, oldMemBitmap); | |
156 OS.DeleteDC (memHdc); | |
157 OS.DeleteObject (memDib); | |
158 OS.SelectObject (srcHdc, oldSrcBitmap); | |
159 OS.DeleteDC (srcHdc); | |
160 OS.ReleaseDC (null, hdc); | |
161 OS.DeleteObject (hImage); | |
162 return dragSourceImage; | |
163 } | |
164 } | |
165 return null; | |
166 } | |
167 Table table = cast(Table) control; | |
168 //TEMPORARY CODE | |
169 if (table.isListening (SWT.EraseItem) || table.isListening (SWT.PaintItem)) return null; | |
170 TableItem[] selection = table.getSelection(); | |
171 if (selection.length is 0) return null; | |
172 int /*long*/ tableImageList = OS.SendMessage (table.handle, OS.LVM_GETIMAGELIST, OS.LVSIL_SMALL, 0); | |
173 if (tableImageList !is 0) { | |
174 int count = Math.min(selection.length, 10); | |
175 Rectangle bounds = selection[0].getBounds(0); | |
176 for (int i = 1; i < count; i++) { | |
177 bounds = bounds.makeUnion(selection[i].getBounds(0)); | |
178 } | |
179 auto hDC = OS.GetDC(null); | |
180 auto hDC1 = OS.CreateCompatibleDC(hDC); | |
181 if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION(4, 10)) { | |
182 if ((table.getStyle() & SWT.RIGHT_TO_LEFT) !is 0) { | |
183 OS.SetLayout(hDC1, OS.LAYOUT_RTL | OS.LAYOUT_BITMAPORIENTATIONPRESERVED); | |
184 } | |
185 } | |
186 auto bitmap = OS.CreateCompatibleBitmap(hDC, bounds.width, bounds.height); | |
187 auto hOldBitmap = OS.SelectObject(hDC1, bitmap); | |
188 RECT rect; | |
189 rect.right = bounds.width; | |
190 rect.bottom = bounds.height; | |
191 auto hBrush = OS.GetStockObject(OS.WHITE_BRUSH); | |
192 OS.FillRect(hDC1, &rect, hBrush); | |
193 for (int i = 0; i < count; i++) { | |
194 TableItem selected = selection[i]; | |
195 Rectangle cell = selected.getBounds(0); | |
196 POINT pt; | |
197 HANDLE imageList = cast(HANDLE) OS.SendMessage (table.handle, OS.LVM_CREATEDRAGIMAGE, table.indexOf(selected), &pt); | |
198 OS.ImageList_Draw(imageList, 0, hDC1, cell.x - bounds.x, cell.y - bounds.y, OS.ILD_SELECTED); | |
199 OS.ImageList_Destroy(imageList); | |
200 } | |
201 OS.SelectObject(hDC1, hOldBitmap); | |
202 OS.DeleteDC (hDC1); | |
203 OS.ReleaseDC (null, hDC); | |
204 Display display = table.getDisplay(); | |
205 dragSourceImage = Image.win32_new(display, SWT.BITMAP, bitmap); | |
206 return dragSourceImage; | |
207 } | |
208 return null; | |
209 } | |
210 } |