KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > internal > theme > ScrollBarDrawData


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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  *******************************************************************************/

11 package org.eclipse.swt.internal.theme;
12
13 import org.eclipse.swt.*;
14 import org.eclipse.swt.graphics.*;
15 import org.eclipse.swt.internal.win32.*;
16
17 public class ScrollBarDrawData extends RangeDrawData {
18     public int thumb;
19     public int increment;
20     public int pageIncrement;
21     
22 public ScrollBarDrawData() {
23     state = new int[6];
24 }
25
26 void draw(Theme theme, GC gc, Rectangle bounds) {
27     if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
28         int hTheme = OS.OpenThemeData(0, getClassId());
29         RECT rect = new RECT ();
30         if ((style & SWT.VERTICAL) != 0) {
31             int width = OS.GetThemeSysSize(hTheme, OS.SM_CXVSCROLL);
32             rect.left = bounds.x;
33             rect.right = rect.left + bounds.width;
34             rect.top = bounds.y;
35             rect.bottom = rect.top + width;
36             int[] part = getPartId(DrawData.SCROLLBAR_UP_ARROW);
37             OS.DrawThemeBackground (hTheme, gc.handle, part[0], part[1], rect, null);
38             rect.bottom = bounds.y + bounds.height;
39             rect.top = rect.bottom - width;
40             part = getPartId(DrawData.SCROLLBAR_DOWN_ARROW);
41             OS.DrawThemeBackground (hTheme, gc.handle, part[0], part[1], rect, null);
42             int totalWidth = bounds.height - 2 * width;
43             int thumbWidth = Math.max(width / 2, (totalWidth * thumb) / Math.max(1, (maximum - minimum)));//BAD
44
int thumbPos = bounds.y + width + Math.max(0, (totalWidth * selection) / Math.max(1, (maximum - minimum)));
45             rect.top = bounds.y + width;
46             rect.bottom = thumbPos;
47             part = getPartId(DrawData.SCROLLBAR_UP_TRACK);
48             OS.DrawThemeBackground (hTheme, gc.handle, part[0], part[1], rect, null);
49             rect.top = rect.bottom;
50             rect.bottom = rect.top + thumbWidth;
51             part = getPartId(DrawData.SCROLLBAR_THUMB);
52             OS.DrawThemeBackground (hTheme, gc.handle, part[0], part[1], rect, null);
53             OS.DrawThemeBackground (hTheme, gc.handle, OS.SBP_GRIPPERVERT, part[1], rect, null);
54             rect.top = rect.bottom;
55             rect.bottom = bounds.y + bounds.height - width;
56             part = getPartId(DrawData.SCROLLBAR_DOWN_TRACK);
57             OS.DrawThemeBackground (hTheme, gc.handle, part[0], part[1], rect, null);
58         } else {
59             //TODO - why SM_CXHSCROLL = 0?
60
int height = OS.GetThemeSysSize(hTheme, OS.SM_CXVSCROLL);
61             rect.top = bounds.y;
62             rect.bottom = rect.top + bounds.height;
63             rect.left = bounds.x;
64             rect.right = rect.left + height;
65             int[] part = getPartId(DrawData.SCROLLBAR_LEFT_ARROW);
66             OS.DrawThemeBackground (hTheme, gc.handle, part[0], part[1], rect, null);
67             rect.right = bounds.x + bounds.width;
68             rect.left = rect.right - height;
69             part = getPartId(DrawData.SCROLLBAR_RIGHT_ARROW);
70             OS.DrawThemeBackground (hTheme, gc.handle, part[0], part[1], rect, null);
71             int totalWidth = bounds.width - 2 * height;
72             int thumbWidth = Math.max(height / 2, (totalWidth * thumb) / (maximum - minimum));//BAD
73
int thumbPos = bounds.x + height + Math.max(0, (totalWidth * selection) / Math.max(1, (maximum - minimum)));
74             rect.left = bounds.x + height;
75             rect.right = thumbPos;
76             part = getPartId(DrawData.SCROLLBAR_UP_TRACK);
77             OS.DrawThemeBackground (hTheme, gc.handle, part[0], part[1], rect, null);
78             rect.left = rect.right;
79             rect.right = rect.left + thumbWidth;
80             part = getPartId(DrawData.SCROLLBAR_THUMB);
81             OS.DrawThemeBackground (hTheme, gc.handle, part[0], part[1], rect, null);
82             OS.DrawThemeBackground (hTheme, gc.handle, OS.SBP_GRIPPERHORZ, part[1], rect, null);
83             rect.left = rect.right;
84             rect.right = bounds.x + bounds.width - height;
85             part = getPartId(DrawData.SCROLLBAR_DOWN_TRACK);
86             OS.DrawThemeBackground (hTheme, gc.handle, part[0], part[1], rect, null);
87         }
88         OS.CloseThemeData (hTheme);
89     }
90 }
91
92 char[] getClassId() {
93     return SCROLLBAR;
94 }
95
96 int[] getPartId(int part) {
97     int iPartId = 0, iStateId = 0;
98     int state = this.state[part];
99     switch (part) {
100         case DrawData.SCROLLBAR_UP_ARROW:
101             iPartId = OS.SBP_ARROWBTN;
102             if ((style & SWT.VERTICAL) != 0) {
103                 iStateId = OS.ABS_UPNORMAL;
104                 if ((state & DrawData.HOT) != 0) iStateId = OS.ABS_UPHOT;
105                 if ((state & DrawData.PRESSED) != 0) iStateId = OS.ABS_UPPRESSED;
106                 if ((state & DrawData.DISABLED) != 0) iStateId = OS.ABS_UPDISABLED;
107             } else {
108                 iStateId = OS.ABS_LEFTNORMAL;
109                 if ((state & DrawData.HOT) != 0) iStateId = OS.ABS_LEFTHOT;
110                 if ((state & DrawData.PRESSED) != 0) iStateId = OS.ABS_LEFTPRESSED;
111                 if ((state & DrawData.DISABLED) != 0) iStateId = OS.ABS_LEFTDISABLED;
112             }
113             break;
114         case DrawData.SCROLLBAR_DOWN_ARROW:
115             iPartId = OS.SBP_ARROWBTN;
116             if ((style & SWT.VERTICAL) != 0) {
117                 iStateId = OS.ABS_DOWNNORMAL;
118                 if ((state & DrawData.HOT) != 0) iStateId = OS.ABS_DOWNHOT;
119                 if ((state & DrawData.PRESSED) != 0) iStateId = OS.ABS_DOWNPRESSED;
120                 if ((state & DrawData.DISABLED) != 0) iStateId = OS.ABS_DOWNDISABLED;
121             } else {
122                 iStateId = OS.ABS_RIGHTNORMAL;
123                 if ((state & DrawData.HOT) != 0) iStateId = OS.ABS_RIGHTHOT;
124                 if ((state & DrawData.PRESSED) != 0) iStateId = OS.ABS_RIGHTPRESSED;
125                 if ((state & DrawData.DISABLED) != 0) iStateId = OS.ABS_RIGHTDISABLED;
126             }
127             break;
128         case DrawData.WIDGET_WHOLE:
129         case DrawData.SCROLLBAR_THUMB:
130             if ((style & SWT.VERTICAL) != 0) {
131                 iPartId = OS.SBP_THUMBBTNVERT;
132             } else {
133                 iPartId = OS.SBP_THUMBBTNHORZ;
134             }
135             break;
136         case DrawData.SCROLLBAR_UP_TRACK:
137             if ((style & SWT.VERTICAL) != 0) {
138                 iPartId = OS.SBP_UPPERTRACKVERT;
139             } else {
140                 iPartId = OS.SBP_UPPERTRACKHORZ;
141             }
142             break;
143         case DrawData.SCROLLBAR_DOWN_TRACK:
144             if ((style & SWT.VERTICAL) != 0) {
145                 iPartId = OS.SBP_LOWERTRACKVERT;
146             } else {
147                 iPartId = OS.SBP_LOWERTRACKHORZ;
148             }
149             break;
150     }
151     if (part != DrawData.SCROLLBAR_DOWN_ARROW && part != DrawData.SCROLLBAR_UP_ARROW) {
152         iStateId = OS.SCRBS_NORMAL;
153         if ((state & DrawData.HOT) != 0) iStateId = OS.SCRBS_HOT;
154         if ((state & DrawData.PRESSED) != 0) iStateId = OS.SCRBS_PRESSED;
155         if ((state & DrawData.DISABLED) != 0) iStateId = OS.SCRBS_DISABLED;
156     }
157     return new int[]{iPartId, iStateId};
158 }
159
160 Rectangle getBounds(int part, Rectangle bounds) {
161     if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
162         int hTheme = OS.OpenThemeData(0, getClassId());
163         if ((style & SWT.VERTICAL) != 0) {
164             int width = OS.GetThemeSysSize(hTheme, OS.SM_CXVSCROLL);
165             int totalWidth = bounds.height - 2 * width;
166             int thumbWidth = Math.max(width / 2, (totalWidth * thumb) / Math.max(1, (maximum - minimum)));//BAD
167
int thumbPos = bounds.y + width + Math.max(0, (totalWidth * selection) / Math.max(1, (maximum - minimum)));
168             switch (part) {
169                 case DrawData.SCROLLBAR_DOWN_ARROW:
170                     return new Rectangle(bounds.x, bounds.y + bounds.height - width, bounds.width, width);
171                 case DrawData.SCROLLBAR_UP_ARROW:
172                     return new Rectangle(bounds.x, bounds.y, bounds.width, width);
173                 case DrawData.SCROLLBAR_UP_TRACK:
174                     return new Rectangle(bounds.x, bounds.y + width, bounds.width, thumbPos - bounds.y - width);
175                 case DrawData.SCROLLBAR_THUMB:
176                     return new Rectangle(bounds.x, thumbPos, bounds.width, thumbWidth);
177                 case DrawData.SCROLLBAR_DOWN_TRACK:
178                     return new Rectangle(bounds.x, thumbPos + thumbWidth, bounds.width, bounds.y + bounds.height - width - thumbPos - thumbWidth);
179             }
180         } else {
181             
182         }
183         OS.CloseThemeData (hTheme);
184     }
185     return super.getBounds(part, bounds);
186 }
187
188 int getSelection(Point position, Rectangle bounds) {
189     if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
190         int hTheme = OS.OpenThemeData(0, getClassId());
191         if ((style & SWT.VERTICAL) != 0) {
192             int width = OS.GetThemeSysSize(hTheme, OS.SM_CXVSCROLL);
193             int totalWidth = bounds.height - 2 * width;
194             int thumbPos = bounds.y + width + Math.max(0, (totalWidth * selection) / Math.max(1, (maximum - minimum)));
195             thumbPos += position.y;
196             int selection = ((thumbPos - bounds.y - width) * (maximum - minimum)) / totalWidth;
197             return Math.max(0, Math.min(selection, maximum - thumb));
198         } else {
199             
200         }
201         OS.CloseThemeData (hTheme);
202     }
203     return 0;
204 }
205
206 int hit(Theme theme, Point position, Rectangle bounds) {
207     if (!(OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ())) return -1;
208     int hTheme = OS.OpenThemeData(0, getClassId());
209     //TODO - should we take a GC?
210
int hDC = 0;
211     RECT rect = new RECT ();
212     POINT pt = new POINT();
213     pt.x = position.x;
214     pt.y = position.y;
215     short[] code = new short[1];
216     try {
217         if ((style & SWT.VERTICAL) != 0) {
218             int width = OS.GetThemeSysSize(hTheme, OS.SM_CXVSCROLL);
219             rect.left = bounds.x;
220             rect.right = rect.left + bounds.width;
221             rect.top = bounds.y;
222             rect.bottom = rect.top + width;
223             int[] part = getPartId(DrawData.SCROLLBAR_UP_ARROW);
224             OS.HitTestThemeBackground(hTheme, hDC, part[0], part[1], 0, rect, 0, pt, code);
225             if (code[0] != OS.HTNOWHERE) return DrawData.SCROLLBAR_UP_ARROW;
226             rect.bottom = bounds.y + bounds.height;
227             rect.top = rect.bottom - width;
228             part = getPartId(DrawData.SCROLLBAR_DOWN_ARROW);
229             OS.HitTestThemeBackground(hTheme, hDC, part[0], part[1], 0, rect, 0, pt, code);
230             if (code[0] != OS.HTNOWHERE) return DrawData.SCROLLBAR_DOWN_ARROW;
231             int totalWidth = bounds.height - 2 * width;
232             int thumbWidth = Math.max(width / 2, (totalWidth * thumb) / Math.max(1, (maximum - minimum)));//BAD
233
int thumbPos = bounds.y + width + Math.max(0, (totalWidth * selection) / Math.max(1, (maximum - minimum)));
234             rect.top = bounds.y + width;
235             rect.bottom = thumbPos;
236             part = getPartId(DrawData.SCROLLBAR_THUMB);
237             OS.HitTestThemeBackground(hTheme, hDC, part[0], part[1], 0, rect, 0, pt, code);
238             if (code[0] != OS.HTNOWHERE) return DrawData.SCROLLBAR_UP_TRACK;
239             rect.top = rect.bottom;
240             rect.bottom = rect.top + thumbWidth;
241             part = getPartId(DrawData.SCROLLBAR_UP_TRACK);
242             OS.HitTestThemeBackground(hTheme, hDC, part[0], part[1], 0, rect, 0, pt, code);
243             if (code[0] != OS.HTNOWHERE) return DrawData.SCROLLBAR_THUMB;
244             rect.top = rect.bottom;
245             rect.bottom = bounds.y + bounds.height - width;
246             part = getPartId(DrawData.SCROLLBAR_DOWN_TRACK);
247             OS.HitTestThemeBackground(hTheme, hDC, part[0], part[1], 0, rect, 0, pt, code);
248             if (code[0] != OS.HTNOWHERE) return DrawData.SCROLLBAR_DOWN_TRACK;
249         } else {
250             int height = OS.GetThemeSysSize(hTheme, OS.SM_CXVSCROLL);//BAD - why SM_CXHSCROLL = 0?
251
rect.top = bounds.y;
252             rect.bottom = rect.top + bounds.height;
253             rect.left = bounds.x;
254             rect.right = rect.left + height;
255             int[] part = getPartId(DrawData.SCROLLBAR_LEFT_ARROW);
256             OS.HitTestThemeBackground(hTheme, hDC, part[0], part[1], 0, rect, 0, pt, code);
257             if (code[0] != OS.HTNOWHERE) return DrawData.SCROLLBAR_UP_ARROW;
258             rect.right = bounds.x + bounds.width;
259             rect.left = rect.right - height;
260             part = getPartId(DrawData.SCROLLBAR_RIGHT_ARROW);
261             OS.HitTestThemeBackground(hTheme, hDC, part[0], part[1], 0, rect, 0, pt, code);
262             if (code[0] != OS.HTNOWHERE) return DrawData.SCROLLBAR_DOWN_ARROW;
263             int totalWidth = bounds.width - 2 * height;
264             int thumbWidth = Math.max(height / 2, (totalWidth * thumb) / (maximum - minimum));//BAD
265
int thumbPos = bounds.x + height + Math.max(0, (totalWidth * selection) / Math.max(1, (maximum - minimum)));
266             rect.left = bounds.x + height;
267             rect.right = thumbPos;
268             part = getPartId(DrawData.SCROLLBAR_LEFT_TRACK);
269             OS.HitTestThemeBackground(hTheme, hDC, part[0], part[1], 0, rect, 0, pt, code);
270             if (code[0] != OS.HTNOWHERE) return DrawData.SCROLLBAR_UP_TRACK;
271             rect.left = rect.right;
272             rect.right = rect.left + thumbWidth;
273             part = getPartId(DrawData.SCROLLBAR_THUMB);
274             OS.HitTestThemeBackground(hTheme, hDC, part[0], part[1], 0, rect, 0, pt, code);
275             if (code[0] != OS.HTNOWHERE) return DrawData.SCROLLBAR_THUMB;
276             rect.left = rect.right;
277             rect.right = bounds.x + bounds.width - height;
278             part = getPartId(DrawData.SCROLLBAR_RIGHT_TRACK);
279             OS.HitTestThemeBackground(hTheme, hDC, part[0], part[1], 0, rect, 0, pt, code);
280             if (code[0] != OS.HTNOWHERE) return DrawData.SCROLLBAR_DOWN_TRACK;
281         }
282     } finally {
283         OS.CloseThemeData (hTheme);
284     }
285     return DrawData.WIDGET_NOWHERE;
286 }
287
288 }
289
Popular Tags