1 /*******************************************************************************
2  * Copyright (c) 2000, 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.widgets;
15 
16 
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.events.*;
19 import org.eclipse.swt.graphics.*;
20 import org.eclipse.swt.internal.win32.*;
21 
22 /**
23  * Instances of the receiver represent a selectable user interface object
24  * that allows the user to drag a rubber banded outline of the sash within
25  * the parent control.
26  * <dl>
27  * <dt><b>Styles:</b></dt>
28  * <dd>HORIZONTAL, VERTICAL, SMOOTH</dd>
29  * <dt><b>Events:</b></dt>
30  * <dd>Selection</dd>
31  * </dl>
32  * <p>
33  * Note: Only one of the styles HORIZONTAL and VERTICAL may be specified.
34  * </p><p>
35  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
36  * </p>
37  *
38  * @see <a href="http://www.eclipse.org/swt/snippets/#sash">Sash snippets</a>
39  * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample</a>
40  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
41  * @noextend This class is not intended to be subclassed by clients.
42  */
43 public class Sash extends Control {
44 	boolean dragging;
45 	int startX, startY, lastX, lastY;
46 	final static int INCREMENT = 1;
47 	final static int PAGE_INCREMENT = 9;
48 
49 /**
50  * Constructs a new instance of this class given its parent
51  * and a style value describing its behavior and appearance.
52  * <p>
53  * The style value is either one of the style constants defined in
54  * class <code>SWT</code> which is applicable to instances of this
55  * class, or must be built by <em>bitwise OR</em>'ing together
56  * (that is, using the <code>int</code> "|" operator) two or more
57  * of those <code>SWT</code> style constants. The class description
58  * lists the style constants that are applicable to the class.
59  * Style bits are also inherited from superclasses.
60  * </p>
61  *
62  * @param parent a composite control which will be the parent of the new instance (cannot be null)
63  * @param style the style of control to construct
64  *
65  * @exception IllegalArgumentException <ul>
66  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
67  * </ul>
68  * @exception SWTException <ul>
69  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
70  *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
71  * </ul>
72  *
73  * @see SWT#HORIZONTAL
74  * @see SWT#VERTICAL
75  * @see SWT#SMOOTH
76  * @see Widget#checkSubclass
77  * @see Widget#getStyle
78  */
Sash(Composite parent, int style)79 public Sash (Composite parent, int style) {
80 	super (parent, checkStyle (style));
81 }
82 
83 /**
84  * Adds the listener to the collection of listeners who will
85  * be notified when the control is selected by the user, by sending
86  * it one of the messages defined in the <code>SelectionListener</code>
87  * interface.
88  * <p>
89  * When <code>widgetSelected</code> is called, the x, y, width, and height fields of the event object are valid.
90  * If the receiver is being dragged, the event object detail field contains the value <code>SWT.DRAG</code>.
91  * <code>widgetDefaultSelected</code> is not called.
92  * </p>
93  *
94  * @param listener the listener which should be notified when the control is selected by the user
95  *
96  * @exception IllegalArgumentException <ul>
97  *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
98  * </ul>
99  * @exception SWTException <ul>
100  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
101  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
102  * </ul>
103  *
104  * @see SelectionListener
105  * @see #removeSelectionListener
106  * @see SelectionEvent
107  */
addSelectionListener(SelectionListener listener)108 public void addSelectionListener (SelectionListener listener) {
109 	checkWidget ();
110 	if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
111 	TypedListener typedListener = new TypedListener (listener);
112 	addListener (SWT.Selection,typedListener);
113 	addListener (SWT.DefaultSelection,typedListener);
114 }
115 
116 @Override
callWindowProc(long hwnd, int msg, long wParam, long lParam)117 long callWindowProc (long hwnd, int msg, long wParam, long lParam) {
118 	if (handle == 0) return 0;
119 	return OS.DefWindowProc (hwnd, msg, wParam, lParam);
120 }
121 
122 @Override
createHandle()123 void createHandle () {
124 	super.createHandle ();
125 	state |= THEME_BACKGROUND;
126 }
127 
checkStyle(int style)128 static int checkStyle (int style) {
129 	return checkBits (style, SWT.HORIZONTAL, SWT.VERTICAL, 0, 0, 0, 0);
130 }
131 
computeSizeInPixels(int wHint, int hHint, boolean changed)132 @Override Point computeSizeInPixels (int wHint, int hHint, boolean changed) {
133 	checkWidget ();
134 	int border = getBorderWidthInPixels ();
135 	int width = border * 2, height = border * 2;
136 	if ((style & SWT.HORIZONTAL) != 0) {
137 		width += DEFAULT_WIDTH;  height += 3;
138 	} else {
139 		width += 3; height += DEFAULT_HEIGHT;
140 	}
141 	if (wHint != SWT.DEFAULT) width = wHint + (border * 2);
142 	if (hHint != SWT.DEFAULT) height = hHint + (border * 2);
143 	return new Point (width, height);
144 }
145 
drawBand(int x, int y, int width, int height)146 void drawBand (int x, int y, int width, int height) {
147 	if ((style & SWT.SMOOTH) != 0) return;
148 	long hwndTrack = parent.handle;
149 	byte [] bits = {-86, 0, 85, 0, -86, 0, 85, 0, -86, 0, 85, 0, -86, 0, 85, 0};
150 	long stippleBitmap = OS.CreateBitmap (8, 8, 1, 1, bits);
151 	long stippleBrush = OS.CreatePatternBrush (stippleBitmap);
152 	long hDC = OS.GetDCEx (hwndTrack, 0, OS.DCX_CACHE);
153 	long oldBrush = OS.SelectObject (hDC, stippleBrush);
154 	OS.PatBlt (hDC, x, y, width, height, OS.PATINVERT);
155 	OS.SelectObject (hDC, oldBrush);
156 	OS.ReleaseDC (hwndTrack, hDC);
157 	OS.DeleteObject (stippleBrush);
158 	OS.DeleteObject (stippleBitmap);
159 }
160 
161 /**
162  * Removes the listener from the collection of listeners who will
163  * be notified when the control is selected by the user.
164  *
165  * @param listener the listener which should no longer be notified
166  *
167  * @exception IllegalArgumentException <ul>
168  *    <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
169  * </ul>
170  * @exception SWTException <ul>
171  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
172  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
173  * </ul>
174  *
175  * @see SelectionListener
176  * @see #addSelectionListener
177  */
removeSelectionListener(SelectionListener listener)178 public void removeSelectionListener(SelectionListener listener) {
179 	checkWidget ();
180 	if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
181 	if (eventTable == null) return;
182 	eventTable.unhook (SWT.Selection, listener);
183 	eventTable.unhook (SWT.DefaultSelection,listener);
184 }
185 
186 @Override
windowClass()187 TCHAR windowClass () {
188 	return display.windowClass;
189 }
190 
191 @Override
windowProc()192 long windowProc () {
193 	return display.windowProc;
194 }
195 
196 @Override
WM_ERASEBKGND(long wParam, long lParam)197 LRESULT WM_ERASEBKGND (long wParam, long lParam) {
198 	super.WM_ERASEBKGND (wParam, lParam);
199 	drawBackground (wParam);
200 	return LRESULT.ONE;
201 }
202 
203 @Override
WM_KEYDOWN(long wParam, long lParam)204 LRESULT WM_KEYDOWN (long wParam, long lParam) {
205 	LRESULT result = super.WM_KEYDOWN (wParam, lParam);
206 	if (result != null) return result;
207 	switch ((int)wParam) {
208 		case OS.VK_LEFT:
209 		case OS.VK_RIGHT:
210 		case OS.VK_UP:
211 		case OS.VK_DOWN:
212 
213 			/* Calculate the new x or y position */
214 			if (OS.GetKeyState (OS.VK_LBUTTON) < 0) return result;
215 			int step = OS.GetKeyState (OS.VK_CONTROL) < 0 ? INCREMENT : PAGE_INCREMENT;
216 			if ((style & SWT.VERTICAL) != 0) {
217 				if (wParam == OS.VK_UP || wParam == OS.VK_DOWN) break;
218 				if (wParam == OS.VK_LEFT) step = -step;
219 				if ((parent.style & SWT.MIRRORED) != 0) step = -step;
220 			} else {
221 				if (wParam == OS.VK_LEFT || wParam == OS.VK_RIGHT) break;
222 				if (wParam == OS.VK_UP) step = -step;
223 			}
224 			RECT rect = new RECT ();
225 			OS.GetWindowRect (handle, rect);
226 			int width = rect.right - rect.left;
227 			int height = rect.bottom - rect.top;
228 			long hwndTrack = parent.handle;
229 			RECT clientRect = new RECT ();
230 			OS.GetClientRect (hwndTrack, clientRect);
231 			int clientWidth = clientRect.right - clientRect.left;
232 			int clientHeight = clientRect.bottom - clientRect.top;
233 			OS.MapWindowPoints (0, hwndTrack, rect, 2);
234 			POINT cursorPt = new POINT ();
235 			int newX = rect.left, newY = rect.top;
236 			if ((style & SWT.VERTICAL) != 0) {
237 				cursorPt.x = newX = Math.min (Math.max (clientRect.left, newX + step), clientWidth - width);
238 				cursorPt.y = rect.top + height / 2;
239 			} else {
240 				cursorPt.x = rect.left + width / 2;
241 				cursorPt.y = newY = Math.min (Math.max (clientRect.top, newY + step), clientHeight - height);
242 			}
243 			if (newX == rect.left && newY == rect.top) return result;
244 
245 			/* Update the pointer position */
246 			OS.ClientToScreen (hwndTrack, cursorPt);
247 			OS.SetCursorPos (cursorPt.x, cursorPt.y);
248 
249 			Event event = new Event ();
250 			event.setBoundsInPixels(new Rectangle(newX, newY, width, height));
251 			sendSelectionEvent  (SWT.Selection, event, true);
252 			if (isDisposed ()) return LRESULT.ZERO;
253 			if (event.doit) {
254 				if ((style & SWT.SMOOTH) != 0) {
255 					setBoundsInPixels (event.getBoundsInPixels());
256 				}
257 			}
258 			return result;
259 	}
260 	return result;
261 }
262 
263 @Override
264 LRESULT WM_GETDLGCODE (long wParam, long lParam) {
265 	return new LRESULT (OS.DLGC_STATIC);
266 }
267 
268 @Override
269 LRESULT WM_LBUTTONDOWN (long wParam, long lParam) {
270 	LRESULT result = super.WM_LBUTTONDOWN (wParam, lParam);
271 	if (result == LRESULT.ZERO) return result;
272 
273 	/* Compute the banding rectangle */
274 	long hwndTrack = parent.handle;
275 	POINT pt = new POINT ();
276 	OS.POINTSTOPOINT (pt, lParam);
277 	RECT rect = new RECT ();
278 	OS.GetWindowRect (handle, rect);
279 	OS.MapWindowPoints (handle, 0, pt, 1);
280 	startX = pt.x - rect.left;
281 	startY = pt.y - rect.top;
282 	OS.MapWindowPoints (0, hwndTrack, rect, 2);
283 	lastX = rect.left;
284 	lastY = rect.top;
285 	int width = rect.right - rect.left;
286 	int height = rect.bottom - rect.top;
287 
288 	/* The event must be sent because doit flag is used */
289 	Event event = new Event ();
290 	event.setBoundsInPixels(new Rectangle(lastX, lastY, width, height));
291 	if ((style & SWT.SMOOTH) == 0) {
292 		event.detail = SWT.DRAG;
293 	}
294 	sendSelectionEvent (SWT.Selection, event, true);
295 	if (isDisposed ()) return LRESULT.ZERO;
296 
297 	/* Draw the banding rectangle */
298 	Rectangle bounds = event.getBoundsInPixels();
299 	if (event.doit) {
300 		dragging = true;
301 		lastX = bounds.x;
302 		lastY = bounds.y;
303 		menuShell ().bringToTop ();
304 		if (isDisposed ()) return LRESULT.ZERO;
305 		int flags = OS.RDW_UPDATENOW | OS.RDW_ALLCHILDREN;
306 		OS.RedrawWindow (hwndTrack, null, 0, flags);
307 		drawBand (bounds.x, bounds.y, width, height);
308 		if ((style & SWT.SMOOTH) != 0) {
309 			setBoundsInPixels (bounds.x, bounds.y, width, height);
310 			// widget could be disposed at this point
311 		}
312 	}
313 	return result;
314 }
315 
316 @Override
317 LRESULT WM_LBUTTONUP (long wParam, long lParam) {
318 	LRESULT result = super.WM_LBUTTONUP (wParam, lParam);
319 	if (result == LRESULT.ZERO) return result;
320 
321 	/* Compute the banding rectangle */
322 	if (!dragging) return result;
323 	dragging = false;
324 	RECT rect = new RECT ();
325 	OS.GetWindowRect (handle, rect);
326 	int width = rect.right - rect.left;
327 	int height = rect.bottom - rect.top;
328 
329 	/* The event must be sent because doit flag is used */
330 	Event event = new Event ();
331 	event.setBoundsInPixels(new Rectangle(lastX, lastY, width, height));
332 	drawBand (lastX, lastY, width, height);
333 	sendSelectionEvent (SWT.Selection, event, true);
334 	if (isDisposed ()) return result;
335 	Rectangle bounds = event.getBoundsInPixels();
336 	if (event.doit) {
337 		if ((style & SWT.SMOOTH) != 0) {
338 			setBoundsInPixels (bounds.x, bounds.y, width, height);
339 			// widget could be disposed at this point
340 		}
341 	}
342 	return result;
343 }
344 
345 @Override
346 LRESULT WM_MOUSEMOVE (long wParam, long lParam) {
347 	LRESULT result = super.WM_MOUSEMOVE (wParam, lParam);
348 	if (result != null) return result;
349 	if (!dragging || (wParam & OS.MK_LBUTTON) == 0) return result;
350 
351 	/* Compute the banding rectangle */
352 	POINT pt = new POINT ();
353 	OS.POINTSTOPOINT (pt, lParam);
354 	long hwndTrack = parent.handle;
355 	OS.MapWindowPoints (handle, hwndTrack, pt, 1);
356 	RECT rect = new RECT (), clientRect = new RECT ();
357 	OS.GetWindowRect (handle, rect);
358 	int width = rect.right - rect.left;
359 	int height = rect.bottom - rect.top;
360 	OS.GetClientRect (hwndTrack, clientRect);
361 	int newX = lastX, newY = lastY;
362 	if ((style & SWT.VERTICAL) != 0) {
363 		int clientWidth = clientRect.right - clientRect.left;
364 		newX = Math.min (Math.max (0, pt.x - startX), clientWidth - width);
365 	} else {
366 		int clientHeight = clientRect.bottom - clientRect.top;
367 		newY = Math.min (Math.max (0, pt.y - startY), clientHeight - height);
368 	}
369 	if (newX == lastX && newY == lastY) return result;
370 	drawBand (lastX, lastY, width, height);
371 
372 	/* The event must be sent because doit flag is used */
373 	Event event = new Event ();
374 	event.setBoundsInPixels(new Rectangle(newX, newY, width, height));
375 	if ((style & SWT.SMOOTH) == 0) {
376 		event.detail = SWT.DRAG;
377 	}
378 	sendSelectionEvent (SWT.Selection, event, true);
379 	if (isDisposed ()) return LRESULT.ZERO;
380 	if (event.doit) {
381 		Rectangle boundsInPixels = event.getBoundsInPixels();
382 		lastX = boundsInPixels.x;
383 		lastY = boundsInPixels.y;
384 	}
385 	int flags = OS.RDW_UPDATENOW | OS.RDW_ALLCHILDREN;
386 	OS.RedrawWindow (hwndTrack, null, 0, flags);
387 	drawBand (lastX, lastY, width, height);
388 	if ((style & SWT.SMOOTH) != 0) {
389 		setBoundsInPixels (lastX, lastY, width, height);
390 		// widget could be disposed at this point
391 	}
392 	return result;
393 }
394 
395 @Override
396 LRESULT WM_SETCURSOR (long wParam, long lParam) {
397 	LRESULT result = super.WM_SETCURSOR (wParam, lParam);
398 	if (result != null) return result;
399 	int hitTest = (short) OS.LOWORD (lParam);
400 	if (hitTest == OS.HTCLIENT) {
401 		long hCursor = 0;
402 		if ((style & SWT.HORIZONTAL) != 0) {
403 			hCursor = OS.LoadCursor (0, OS.IDC_SIZENS);
404 		} else {
405 			hCursor = OS.LoadCursor (0, OS.IDC_SIZEWE);
406 		}
407 		OS.SetCursor (hCursor);
408 		return LRESULT.ONE;
409 	}
410 	return result;
411 }
412 
413 }
414