001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.swt.internal.theme;
011:
012: import org.eclipse.swt.*;
013: import org.eclipse.swt.graphics.*;
014: import org.eclipse.swt.internal.win32.*;
015:
016: public class ScrollBarDrawData extends RangeDrawData {
017: public int thumb;
018: public int increment;
019: public int pageIncrement;
020:
021: public ScrollBarDrawData() {
022: state = new int[6];
023: }
024:
025: void draw(Theme theme, GC gc, Rectangle bounds) {
026: if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed()) {
027: int /*long*/hTheme = OS.OpenThemeData(0, getClassId());
028: RECT rect = new RECT();
029: if ((style & SWT.VERTICAL) != 0) {
030: int width = OS.GetThemeSysSize(hTheme, OS.SM_CXVSCROLL);
031: rect.left = bounds.x;
032: rect.right = rect.left + bounds.width;
033: rect.top = bounds.y;
034: rect.bottom = rect.top + width;
035: int[] part = getPartId(DrawData.SCROLLBAR_UP_ARROW);
036: OS.DrawThemeBackground(hTheme, gc.handle, part[0],
037: part[1], rect, null);
038: rect.bottom = bounds.y + bounds.height;
039: rect.top = rect.bottom - width;
040: part = getPartId(DrawData.SCROLLBAR_DOWN_ARROW);
041: OS.DrawThemeBackground(hTheme, gc.handle, part[0],
042: part[1], rect, null);
043: int totalWidth = bounds.height - 2 * width;
044: int thumbWidth = Math.max(width / 2,
045: (totalWidth * thumb)
046: / Math.max(1, (maximum - minimum)));//BAD
047: int thumbPos = bounds.y
048: + width
049: + Math.max(0, (totalWidth * selection)
050: / Math.max(1, (maximum - minimum)));
051: rect.top = bounds.y + width;
052: rect.bottom = thumbPos;
053: part = getPartId(DrawData.SCROLLBAR_UP_TRACK);
054: OS.DrawThemeBackground(hTheme, gc.handle, part[0],
055: part[1], rect, null);
056: rect.top = rect.bottom;
057: rect.bottom = rect.top + thumbWidth;
058: part = getPartId(DrawData.SCROLLBAR_THUMB);
059: OS.DrawThemeBackground(hTheme, gc.handle, part[0],
060: part[1], rect, null);
061: OS.DrawThemeBackground(hTheme, gc.handle,
062: OS.SBP_GRIPPERVERT, part[1], rect, null);
063: rect.top = rect.bottom;
064: rect.bottom = bounds.y + bounds.height - width;
065: part = getPartId(DrawData.SCROLLBAR_DOWN_TRACK);
066: OS.DrawThemeBackground(hTheme, gc.handle, part[0],
067: part[1], rect, null);
068: } else {
069: //TODO - why SM_CXHSCROLL = 0?
070: int height = OS
071: .GetThemeSysSize(hTheme, OS.SM_CXVSCROLL);
072: rect.top = bounds.y;
073: rect.bottom = rect.top + bounds.height;
074: rect.left = bounds.x;
075: rect.right = rect.left + height;
076: int[] part = getPartId(DrawData.SCROLLBAR_LEFT_ARROW);
077: OS.DrawThemeBackground(hTheme, gc.handle, part[0],
078: part[1], rect, null);
079: rect.right = bounds.x + bounds.width;
080: rect.left = rect.right - height;
081: part = getPartId(DrawData.SCROLLBAR_RIGHT_ARROW);
082: OS.DrawThemeBackground(hTheme, gc.handle, part[0],
083: part[1], rect, null);
084: int totalWidth = bounds.width - 2 * height;
085: int thumbWidth = Math.max(height / 2,
086: (totalWidth * thumb) / (maximum - minimum));//BAD
087: int thumbPos = bounds.x
088: + height
089: + Math.max(0, (totalWidth * selection)
090: / Math.max(1, (maximum - minimum)));
091: rect.left = bounds.x + height;
092: rect.right = thumbPos;
093: part = getPartId(DrawData.SCROLLBAR_UP_TRACK);
094: OS.DrawThemeBackground(hTheme, gc.handle, part[0],
095: part[1], rect, null);
096: rect.left = rect.right;
097: rect.right = rect.left + thumbWidth;
098: part = getPartId(DrawData.SCROLLBAR_THUMB);
099: OS.DrawThemeBackground(hTheme, gc.handle, part[0],
100: part[1], rect, null);
101: OS.DrawThemeBackground(hTheme, gc.handle,
102: OS.SBP_GRIPPERHORZ, part[1], rect, null);
103: rect.left = rect.right;
104: rect.right = bounds.x + bounds.width - height;
105: part = getPartId(DrawData.SCROLLBAR_DOWN_TRACK);
106: OS.DrawThemeBackground(hTheme, gc.handle, part[0],
107: part[1], rect, null);
108: }
109: OS.CloseThemeData(hTheme);
110: }
111: }
112:
113: char[] getClassId() {
114: return SCROLLBAR;
115: }
116:
117: int[] getPartId(int part) {
118: int iPartId = 0, iStateId = 0;
119: int state = this .state[part];
120: switch (part) {
121: case DrawData.SCROLLBAR_UP_ARROW:
122: iPartId = OS.SBP_ARROWBTN;
123: if ((style & SWT.VERTICAL) != 0) {
124: iStateId = OS.ABS_UPNORMAL;
125: if ((state & DrawData.HOT) != 0)
126: iStateId = OS.ABS_UPHOT;
127: if ((state & DrawData.PRESSED) != 0)
128: iStateId = OS.ABS_UPPRESSED;
129: if ((state & DrawData.DISABLED) != 0)
130: iStateId = OS.ABS_UPDISABLED;
131: } else {
132: iStateId = OS.ABS_LEFTNORMAL;
133: if ((state & DrawData.HOT) != 0)
134: iStateId = OS.ABS_LEFTHOT;
135: if ((state & DrawData.PRESSED) != 0)
136: iStateId = OS.ABS_LEFTPRESSED;
137: if ((state & DrawData.DISABLED) != 0)
138: iStateId = OS.ABS_LEFTDISABLED;
139: }
140: break;
141: case DrawData.SCROLLBAR_DOWN_ARROW:
142: iPartId = OS.SBP_ARROWBTN;
143: if ((style & SWT.VERTICAL) != 0) {
144: iStateId = OS.ABS_DOWNNORMAL;
145: if ((state & DrawData.HOT) != 0)
146: iStateId = OS.ABS_DOWNHOT;
147: if ((state & DrawData.PRESSED) != 0)
148: iStateId = OS.ABS_DOWNPRESSED;
149: if ((state & DrawData.DISABLED) != 0)
150: iStateId = OS.ABS_DOWNDISABLED;
151: } else {
152: iStateId = OS.ABS_RIGHTNORMAL;
153: if ((state & DrawData.HOT) != 0)
154: iStateId = OS.ABS_RIGHTHOT;
155: if ((state & DrawData.PRESSED) != 0)
156: iStateId = OS.ABS_RIGHTPRESSED;
157: if ((state & DrawData.DISABLED) != 0)
158: iStateId = OS.ABS_RIGHTDISABLED;
159: }
160: break;
161: case DrawData.WIDGET_WHOLE:
162: case DrawData.SCROLLBAR_THUMB:
163: if ((style & SWT.VERTICAL) != 0) {
164: iPartId = OS.SBP_THUMBBTNVERT;
165: } else {
166: iPartId = OS.SBP_THUMBBTNHORZ;
167: }
168: break;
169: case DrawData.SCROLLBAR_UP_TRACK:
170: if ((style & SWT.VERTICAL) != 0) {
171: iPartId = OS.SBP_UPPERTRACKVERT;
172: } else {
173: iPartId = OS.SBP_UPPERTRACKHORZ;
174: }
175: break;
176: case DrawData.SCROLLBAR_DOWN_TRACK:
177: if ((style & SWT.VERTICAL) != 0) {
178: iPartId = OS.SBP_LOWERTRACKVERT;
179: } else {
180: iPartId = OS.SBP_LOWERTRACKHORZ;
181: }
182: break;
183: }
184: if (part != DrawData.SCROLLBAR_DOWN_ARROW
185: && part != DrawData.SCROLLBAR_UP_ARROW) {
186: iStateId = OS.SCRBS_NORMAL;
187: if ((state & DrawData.HOT) != 0)
188: iStateId = OS.SCRBS_HOT;
189: if ((state & DrawData.PRESSED) != 0)
190: iStateId = OS.SCRBS_PRESSED;
191: if ((state & DrawData.DISABLED) != 0)
192: iStateId = OS.SCRBS_DISABLED;
193: }
194: return new int[] { iPartId, iStateId };
195: }
196:
197: Rectangle getBounds(int part, Rectangle bounds) {
198: if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed()) {
199: int /*long*/hTheme = OS.OpenThemeData(0, getClassId());
200: if ((style & SWT.VERTICAL) != 0) {
201: int width = OS.GetThemeSysSize(hTheme, OS.SM_CXVSCROLL);
202: int totalWidth = bounds.height - 2 * width;
203: int thumbWidth = Math.max(width / 2,
204: (totalWidth * thumb)
205: / Math.max(1, (maximum - minimum)));//BAD
206: int thumbPos = bounds.y
207: + width
208: + Math.max(0, (totalWidth * selection)
209: / Math.max(1, (maximum - minimum)));
210: switch (part) {
211: case DrawData.SCROLLBAR_DOWN_ARROW:
212: return new Rectangle(bounds.x, bounds.y
213: + bounds.height - width, bounds.width,
214: width);
215: case DrawData.SCROLLBAR_UP_ARROW:
216: return new Rectangle(bounds.x, bounds.y,
217: bounds.width, width);
218: case DrawData.SCROLLBAR_UP_TRACK:
219: return new Rectangle(bounds.x, bounds.y + width,
220: bounds.width, thumbPos - bounds.y - width);
221: case DrawData.SCROLLBAR_THUMB:
222: return new Rectangle(bounds.x, thumbPos,
223: bounds.width, thumbWidth);
224: case DrawData.SCROLLBAR_DOWN_TRACK:
225: return new Rectangle(bounds.x, thumbPos
226: + thumbWidth, bounds.width, bounds.y
227: + bounds.height - width - thumbPos
228: - thumbWidth);
229: }
230: } else {
231:
232: }
233: OS.CloseThemeData(hTheme);
234: }
235: return super .getBounds(part, bounds);
236: }
237:
238: int getSelection(Point position, Rectangle bounds) {
239: if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed()) {
240: int /*long*/hTheme = OS.OpenThemeData(0, getClassId());
241: if ((style & SWT.VERTICAL) != 0) {
242: int width = OS.GetThemeSysSize(hTheme, OS.SM_CXVSCROLL);
243: int totalWidth = bounds.height - 2 * width;
244: int thumbPos = bounds.y
245: + width
246: + Math.max(0, (totalWidth * selection)
247: / Math.max(1, (maximum - minimum)));
248: thumbPos += position.y;
249: int selection = ((thumbPos - bounds.y - width) * (maximum - minimum))
250: / totalWidth;
251: return Math
252: .max(0, Math.min(selection, maximum - thumb));
253: } else {
254:
255: }
256: OS.CloseThemeData(hTheme);
257: }
258: return 0;
259: }
260:
261: int hit(Theme theme, Point position, Rectangle bounds) {
262: if (!(OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed()))
263: return -1;
264: int /*long*/hTheme = OS.OpenThemeData(0, getClassId());
265: //TODO - should we take a GC?
266: int hDC = 0;
267: RECT rect = new RECT();
268: POINT pt = new POINT();
269: pt.x = position.x;
270: pt.y = position.y;
271: short[] code = new short[1];
272: try {
273: if ((style & SWT.VERTICAL) != 0) {
274: int width = OS.GetThemeSysSize(hTheme, OS.SM_CXVSCROLL);
275: rect.left = bounds.x;
276: rect.right = rect.left + bounds.width;
277: rect.top = bounds.y;
278: rect.bottom = rect.top + width;
279: int[] part = getPartId(DrawData.SCROLLBAR_UP_ARROW);
280: OS.HitTestThemeBackground(hTheme, hDC, part[0],
281: part[1], 0, rect, 0, pt, code);
282: if (code[0] != OS.HTNOWHERE)
283: return DrawData.SCROLLBAR_UP_ARROW;
284: rect.bottom = bounds.y + bounds.height;
285: rect.top = rect.bottom - width;
286: part = getPartId(DrawData.SCROLLBAR_DOWN_ARROW);
287: OS.HitTestThemeBackground(hTheme, hDC, part[0],
288: part[1], 0, rect, 0, pt, code);
289: if (code[0] != OS.HTNOWHERE)
290: return DrawData.SCROLLBAR_DOWN_ARROW;
291: int totalWidth = bounds.height - 2 * width;
292: int thumbWidth = Math.max(width / 2,
293: (totalWidth * thumb)
294: / Math.max(1, (maximum - minimum)));//BAD
295: int thumbPos = bounds.y
296: + width
297: + Math.max(0, (totalWidth * selection)
298: / Math.max(1, (maximum - minimum)));
299: rect.top = bounds.y + width;
300: rect.bottom = thumbPos;
301: part = getPartId(DrawData.SCROLLBAR_THUMB);
302: OS.HitTestThemeBackground(hTheme, hDC, part[0],
303: part[1], 0, rect, 0, pt, code);
304: if (code[0] != OS.HTNOWHERE)
305: return DrawData.SCROLLBAR_UP_TRACK;
306: rect.top = rect.bottom;
307: rect.bottom = rect.top + thumbWidth;
308: part = getPartId(DrawData.SCROLLBAR_UP_TRACK);
309: OS.HitTestThemeBackground(hTheme, hDC, part[0],
310: part[1], 0, rect, 0, pt, code);
311: if (code[0] != OS.HTNOWHERE)
312: return DrawData.SCROLLBAR_THUMB;
313: rect.top = rect.bottom;
314: rect.bottom = bounds.y + bounds.height - width;
315: part = getPartId(DrawData.SCROLLBAR_DOWN_TRACK);
316: OS.HitTestThemeBackground(hTheme, hDC, part[0],
317: part[1], 0, rect, 0, pt, code);
318: if (code[0] != OS.HTNOWHERE)
319: return DrawData.SCROLLBAR_DOWN_TRACK;
320: } else {
321: int height = OS
322: .GetThemeSysSize(hTheme, OS.SM_CXVSCROLL);//BAD - why SM_CXHSCROLL = 0?
323: rect.top = bounds.y;
324: rect.bottom = rect.top + bounds.height;
325: rect.left = bounds.x;
326: rect.right = rect.left + height;
327: int[] part = getPartId(DrawData.SCROLLBAR_LEFT_ARROW);
328: OS.HitTestThemeBackground(hTheme, hDC, part[0],
329: part[1], 0, rect, 0, pt, code);
330: if (code[0] != OS.HTNOWHERE)
331: return DrawData.SCROLLBAR_UP_ARROW;
332: rect.right = bounds.x + bounds.width;
333: rect.left = rect.right - height;
334: part = getPartId(DrawData.SCROLLBAR_RIGHT_ARROW);
335: OS.HitTestThemeBackground(hTheme, hDC, part[0],
336: part[1], 0, rect, 0, pt, code);
337: if (code[0] != OS.HTNOWHERE)
338: return DrawData.SCROLLBAR_DOWN_ARROW;
339: int totalWidth = bounds.width - 2 * height;
340: int thumbWidth = Math.max(height / 2,
341: (totalWidth * thumb) / (maximum - minimum));//BAD
342: int thumbPos = bounds.x
343: + height
344: + Math.max(0, (totalWidth * selection)
345: / Math.max(1, (maximum - minimum)));
346: rect.left = bounds.x + height;
347: rect.right = thumbPos;
348: part = getPartId(DrawData.SCROLLBAR_LEFT_TRACK);
349: OS.HitTestThemeBackground(hTheme, hDC, part[0],
350: part[1], 0, rect, 0, pt, code);
351: if (code[0] != OS.HTNOWHERE)
352: return DrawData.SCROLLBAR_UP_TRACK;
353: rect.left = rect.right;
354: rect.right = rect.left + thumbWidth;
355: part = getPartId(DrawData.SCROLLBAR_THUMB);
356: OS.HitTestThemeBackground(hTheme, hDC, part[0],
357: part[1], 0, rect, 0, pt, code);
358: if (code[0] != OS.HTNOWHERE)
359: return DrawData.SCROLLBAR_THUMB;
360: rect.left = rect.right;
361: rect.right = bounds.x + bounds.width - height;
362: part = getPartId(DrawData.SCROLLBAR_RIGHT_TRACK);
363: OS.HitTestThemeBackground(hTheme, hDC, part[0],
364: part[1], 0, rect, 0, pt, code);
365: if (code[0] != OS.HTNOWHERE)
366: return DrawData.SCROLLBAR_DOWN_TRACK;
367: }
368: } finally {
369: OS.CloseThemeData(hTheme);
370: }
371: return DrawData.WIDGET_NOWHERE;
372: }
373:
374: }
|