summaryrefslogtreecommitdiffstats
path: root/bundles/org.eclipse.swt/Eclipse SWT Drag and Drop/win32/org/eclipse/swt/dnd/TreeDragSourceEffect.java
blob: 645549a14f111c62d2900bafa385e71b8f4ab366 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*******************************************************************************
 * Copyright (c) 2007, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.dnd;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.internal.win32.*;
import org.eclipse.swt.widgets.*;

/**
 * This class provides default implementations to display a source image
 * when a drag is initiated from a <code>Tree</code>.
 * 
 * <p>Classes that wish to provide their own source image for a <code>Tree</code> can
 * extend <code>TreeDragSourceEffect</code> class and override the <code>TreeDragSourceEffect.dragStart</code>
 * method and set the field <code>DragSourceEvent.image</code> with their own image.</p>
 *
 * Subclasses that override any methods of this class must call the corresponding
 * <code>super</code> method to get the default drag under effect implementation.
 *
 * @see DragSourceEffect
 * @see DragSourceEvent
 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
 * 
 * @since 3.3
 */
public class TreeDragSourceEffect extends DragSourceEffect {
	Image dragSourceImage = null;

	/**
	 * Creates a new <code>TreeDragSourceEffect</code> to handle drag effect 
	 * from the specified <code>Tree</code>.
	 *
	 * @param tree the <code>Tree</code> that the user clicks on to initiate the drag
	 */
	public TreeDragSourceEffect(Tree tree) {
		super(tree);
	}

	/**
	 * This implementation of <code>dragFinished</code> disposes the image
	 * that was created in <code>TreeDragSourceEffect.dragStart</code>.
	 * 
	 * Subclasses that override this method should call <code>super.dragFinished(event)</code>
	 * to dispose the image in the default implementation.
	 * 
	 * @param event the information associated with the drag finished event
	 */
	public void dragFinished(DragSourceEvent event) {
		if (dragSourceImage != null) dragSourceImage.dispose();
		dragSourceImage = null;
	}

	/**
	 * This implementation of <code>dragStart</code> will create a default
	 * image that will be used during the drag. The image should be disposed
	 * when the drag is completed in the <code>TreeDragSourceEffect.dragFinished</code>
	 * method.
	 * 
	 * Subclasses that override this method should call <code>super.dragStart(event)</code>
	 * to use the image from the default implementation.
	 * 
	 * @param event the information associated with the drag start event
	 */
	public void dragStart(DragSourceEvent event) {
		event.image = getDragSourceImage(event);
	}

	Image getDragSourceImage(DragSourceEvent event) {
		if (dragSourceImage != null) dragSourceImage.dispose();
		dragSourceImage = null;
		if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION (5, 1)) {
			SHDRAGIMAGE shdi = new SHDRAGIMAGE();
			int DI_GETDRAGIMAGE = OS.RegisterWindowMessage (new TCHAR (0, "ShellGetDragImage", true)); //$NON-NLS-1$
			if (OS.SendMessage (control.handle, DI_GETDRAGIMAGE, 0, shdi) != 0) {
				if ((control.getStyle() & SWT.MIRRORED) != 0) {
					event.offsetX = shdi.sizeDragImage.cx - shdi.ptOffset.x;
				} else {
					event.offsetX = shdi.ptOffset.x;
				}
				event.offsetY = shdi.ptOffset.y;
				int /*long*/ hImage = shdi.hbmpDragImage;
				if (hImage != 0) {
					BITMAP bm = new BITMAP ();
					OS.GetObject (hImage, BITMAP.sizeof, bm);
					int srcWidth = bm.bmWidth;
					int srcHeight = bm.bmHeight;
					
					/* Create resources */
					int /*long*/ hdc = OS.GetDC (0);
					int /*long*/ srcHdc = OS.CreateCompatibleDC (hdc);
					int /*long*/ oldSrcBitmap = OS.SelectObject (srcHdc, hImage);
					int /*long*/ memHdc = OS.CreateCompatibleDC (hdc);
					BITMAPINFOHEADER bmiHeader = new BITMAPINFOHEADER ();
					bmiHeader.biSize = BITMAPINFOHEADER.sizeof;
					bmiHeader.biWidth = srcWidth;
					bmiHeader.biHeight = -srcHeight;
					bmiHeader.biPlanes = 1;
					bmiHeader.biBitCount = 32;
					bmiHeader.biCompression = OS.BI_RGB;
					byte []	bmi = new byte[BITMAPINFOHEADER.sizeof];
					OS.MoveMemory (bmi, bmiHeader, BITMAPINFOHEADER.sizeof);
					int /*long*/ [] pBits = new int /*long*/ [1];
					int /*long*/ memDib = OS.CreateDIBSection (0, bmi, OS.DIB_RGB_COLORS, pBits, 0, 0);
					if (memDib == 0) SWT.error (SWT.ERROR_NO_HANDLES);
					int /*long*/ oldMemBitmap = OS.SelectObject (memHdc, memDib);

					BITMAP dibBM = new BITMAP ();
					OS.GetObject (memDib, BITMAP.sizeof, dibBM);
					int sizeInBytes = dibBM.bmWidthBytes * dibBM.bmHeight;

				 	/* Get the foreground pixels */
				 	OS.BitBlt (memHdc, 0, 0, srcWidth, srcHeight, srcHdc, 0, 0, OS.SRCCOPY);
				 	byte[] srcData = new byte [sizeInBytes];
					OS.MoveMemory (srcData, dibBM.bmBits, sizeInBytes);
					
					PaletteData palette = new PaletteData(0xFF00, 0xFF0000, 0xFF000000);
					ImageData data = new ImageData(srcWidth, srcHeight, bm.bmBitsPixel, palette, bm.bmWidthBytes, srcData);
					if (shdi.crColorKey == -1) {
						byte[] alphaData = new byte[srcWidth * srcHeight];
						int spinc = dibBM.bmWidthBytes - srcWidth * 4;
						int ap = 0, sp = 3;
						for (int y = 0; y < srcHeight; ++y) {
							for (int x = 0; x < srcWidth; ++x) {
								alphaData [ap++] = srcData [sp];
								sp += 4;
							}
							sp += spinc;
						}
						data.alphaData = alphaData;
					} else {
						data.transparentPixel = shdi.crColorKey << 8;
					}
					dragSourceImage = new Image (control.getDisplay (), data);
					OS.SelectObject (memHdc, oldMemBitmap);
					OS.DeleteDC (memHdc);
					OS.DeleteObject (memDib);
					OS.SelectObject (srcHdc, oldSrcBitmap);
					OS.DeleteDC (srcHdc);
					OS.ReleaseDC (0, hdc);
					OS.DeleteObject (hImage);
					return dragSourceImage;
				}
			}
			return null;
		}
		
		Tree tree = (Tree) control;
		//TEMPORARY CODE
		if (tree.isListening (SWT.EraseItem) || tree.isListening (SWT.PaintItem)) return null;
		TreeItem[] selection = tree.getSelection();
		if (selection.length == 0) return null;
		int /*long*/ treeImageList = OS.SendMessage (tree.handle, OS.TVM_GETIMAGELIST, OS.TVSIL_NORMAL, 0);
		if (treeImageList != 0) {
			int count = Math.min(selection.length, 10);
			Rectangle bounds = selection[0].getBounds(0);
			for (int i = 1; i < count; i++) {
				bounds = bounds.union(selection[i].getBounds(0));
			}
			int /*long*/ hDC = OS.GetDC(tree.handle);
			int /*long*/ hDC1 = OS.CreateCompatibleDC(hDC);
			int /*long*/ bitmap = OS.CreateCompatibleBitmap(hDC, bounds.width, bounds.height);
			int /*long*/ hOldBitmap = OS.SelectObject(hDC1, bitmap);
			RECT rect = new RECT();
			rect.right = bounds.width;
			rect.bottom = bounds.height;
			int /*long*/ hBrush = OS.GetStockObject(OS.WHITE_BRUSH);
			OS.FillRect(hDC1, rect, hBrush);
			for (int i = 0; i < count; i++) {
				TreeItem selected = selection[i];
				Rectangle cell = selected.getBounds(0);
				int /*long*/ imageList = OS.SendMessage(tree.handle, OS.TVM_CREATEDRAGIMAGE, 0, selected.handle);
				OS.ImageList_Draw(imageList, 0, hDC1, cell.x - bounds.x, cell.y - bounds.y, OS.ILD_SELECTED);
				OS.ImageList_Destroy(imageList);
			}
			OS.SelectObject(hDC1, hOldBitmap);
			OS.DeleteDC (hDC1);
			OS.ReleaseDC (tree.handle, hDC);
			Display display = tree.getDisplay();
			dragSourceImage = Image.win32_new(display, SWT.BITMAP, bitmap);
			return dragSourceImage;
		}
		return null;
	}
}