]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/dnd/TreeDragSourceEffect.java
363bf9c581b7fb4d5576a9371744c8df0c33a340
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / dnd / TreeDragSourceEffect.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2012 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.dnd;
15
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.graphics.*;
18 import org.eclipse.swt.internal.*;
19 import org.eclipse.swt.internal.DPIUtil.*;
20 import org.eclipse.swt.internal.win32.*;
21 import org.eclipse.swt.widgets.*;
22
23 /**
24  * This class provides default implementations to display a source image
25  * when a drag is initiated from a <code>Tree</code>.
26  *
27  * <p>Classes that wish to provide their own source image for a <code>Tree</code> can
28  * extend <code>TreeDragSourceEffect</code> class and override the <code>TreeDragSourceEffect.dragStart</code>
29  * method and set the field <code>DragSourceEvent.image</code> with their own image.</p>
30  *
31  * Subclasses that override any methods of this class must call the corresponding
32  * <code>super</code> method to get the default drag under effect implementation.
33  *
34  * @see DragSourceEffect
35  * @see DragSourceEvent
36  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
37  *
38  * @since 3.3
39  */
40 public class TreeDragSourceEffect extends DragSourceEffect {
41         Image dragSourceImage = null;
42
43         /**
44          * Creates a new <code>TreeDragSourceEffect</code> to handle drag effect
45          * from the specified <code>Tree</code>.
46          *
47          * @param tree the <code>Tree</code> that the user clicks on to initiate the drag
48          */
49         public TreeDragSourceEffect(Tree tree) {
50                 super(tree);
51         }
52
53         /**
54          * This implementation of <code>dragFinished</code> disposes the image
55          * that was created in <code>TreeDragSourceEffect.dragStart</code>.
56          *
57          * Subclasses that override this method should call <code>super.dragFinished(event)</code>
58          * to dispose the image in the default implementation.
59          *
60          * @param event the information associated with the drag finished event
61          */
62         @Override
63         public void dragFinished(DragSourceEvent event) {
64                 if (dragSourceImage != null) dragSourceImage.dispose();
65                 dragSourceImage = null;
66         }
67
68         /**
69          * This implementation of <code>dragStart</code> will create a default
70          * image that will be used during the drag. The image should be disposed
71          * when the drag is completed in the <code>TreeDragSourceEffect.dragFinished</code>
72          * method.
73          *
74          * Subclasses that override this method should call <code>super.dragStart(event)</code>
75          * to use the image from the default implementation.
76          *
77          * @param event the information associated with the drag start event
78          */
79         @Override
80         public void dragStart(DragSourceEvent event) {
81                 event.image = getDragSourceImage(event);
82         }
83
84         Image getDragSourceImage(DragSourceEvent event) {
85                 if (dragSourceImage != null) dragSourceImage.dispose();
86                 dragSourceImage = null;
87                 SHDRAGIMAGE shdi = new SHDRAGIMAGE();
88                 int DI_GETDRAGIMAGE = OS.RegisterWindowMessage (new TCHAR (0, "ShellGetDragImage", true)); //$NON-NLS-1$
89                 if (OS.SendMessage (control.handle, DI_GETDRAGIMAGE, 0, shdi) != 0) {
90                         if ((control.getStyle() & SWT.MIRRORED) != 0) {
91                                 event.offsetX = shdi.sizeDragImage.cx - shdi.ptOffset.x;
92                         } else {
93                                 event.offsetX = shdi.ptOffset.x;
94                         }
95                         event.offsetY = shdi.ptOffset.y;
96                         long hImage = shdi.hbmpDragImage;
97                         if (hImage != 0) {
98                                 BITMAP bm = new BITMAP ();
99                                 OS.GetObject (hImage, BITMAP.sizeof, bm);
100                                 int srcWidth = bm.bmWidth;
101                                 int srcHeight = bm.bmHeight;
102
103                                 /* Create resources */
104                                 long hdc = OS.GetDC (0);
105                                 long srcHdc = OS.CreateCompatibleDC (hdc);
106                                 long oldSrcBitmap = OS.SelectObject (srcHdc, hImage);
107                                 long memHdc = OS.CreateCompatibleDC (hdc);
108                                 BITMAPINFOHEADER bmiHeader = new BITMAPINFOHEADER ();
109                                 bmiHeader.biSize = BITMAPINFOHEADER.sizeof;
110                                 bmiHeader.biWidth = srcWidth;
111                                 bmiHeader.biHeight = -srcHeight;
112                                 bmiHeader.biPlanes = 1;
113                                 bmiHeader.biBitCount = 32;
114                                 bmiHeader.biCompression = OS.BI_RGB;
115                                 byte [] bmi = new byte[BITMAPINFOHEADER.sizeof];
116                                 OS.MoveMemory (bmi, bmiHeader, BITMAPINFOHEADER.sizeof);
117                                 long [] pBits = new long [1];
118                                 long memDib = OS.CreateDIBSection (0, bmi, OS.DIB_RGB_COLORS, pBits, 0, 0);
119                                 if (memDib == 0) SWT.error (SWT.ERROR_NO_HANDLES);
120                                 long oldMemBitmap = OS.SelectObject (memHdc, memDib);
121
122                                 BITMAP dibBM = new BITMAP ();
123                                 OS.GetObject (memDib, BITMAP.sizeof, dibBM);
124                                 int sizeInBytes = dibBM.bmWidthBytes * dibBM.bmHeight;
125
126                                 /* Get the foreground pixels */
127                                 OS.BitBlt (memHdc, 0, 0, srcWidth, srcHeight, srcHdc, 0, 0, OS.SRCCOPY);
128                                 byte[] srcData = new byte [sizeInBytes];
129                                 OS.MoveMemory (srcData, dibBM.bmBits, sizeInBytes);
130
131                                 PaletteData palette = new PaletteData(0xFF00, 0xFF0000, 0xFF000000);
132                                 ImageData data = new ImageData(srcWidth, srcHeight, bm.bmBitsPixel, palette, bm.bmWidthBytes, srcData);
133                                 if (shdi.crColorKey == -1) {
134                                         byte[] alphaData = new byte[srcWidth * srcHeight];
135                                         int spinc = dibBM.bmWidthBytes - srcWidth * 4;
136                                         int ap = 0, sp = 3;
137                                         for (int y = 0; y < srcHeight; ++y) {
138                                                 for (int x = 0; x < srcWidth; ++x) {
139                                                         alphaData [ap++] = srcData [sp];
140                                                         sp += 4;
141                                                 }
142                                                 sp += spinc;
143                                         }
144                                         data.alphaData = alphaData;
145                                 } else {
146                                         data.transparentPixel = shdi.crColorKey << 8;
147                                 }
148                                 Display display = control.getDisplay ();
149                                 dragSourceImage = new Image (display, new AutoScaleImageDataProvider(display, data, DPIUtil.getDeviceZoom()));
150                                 OS.SelectObject (memHdc, oldMemBitmap);
151                                 OS.DeleteDC (memHdc);
152                                 OS.DeleteObject (memDib);
153                                 OS.SelectObject (srcHdc, oldSrcBitmap);
154                                 OS.DeleteDC (srcHdc);
155                                 OS.ReleaseDC (0, hdc);
156                                 OS.DeleteObject (hImage);
157                                 return dragSourceImage;
158                         }
159                 }
160                 return null;
161         }
162 }