KickJava   Java API By Example, From Geeks To Geeks.

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


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 ToolItemDrawData extends DrawData {
18
19     public ToolBarDrawData parent;
20     
21     static final int INSET = 1;
22     
23 public ToolItemDrawData() {
24     state = new int[2];
25 }
26
27 Rectangle computeTrim(Theme theme, GC gc) {
28     if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
29         int hTheme = OS.OpenThemeData(0, getClassId());
30         RECT rect = new RECT ();
31         rect.left = clientArea.x;
32         rect.right = clientArea.x + clientArea.width;
33         rect.top = clientArea.y;
34         rect.bottom = clientArea.y + clientArea.height;
35         RECT extent = new RECT ();
36         int[] part = getPartId(DrawData.WIDGET_WHOLE);
37         OS.GetThemeBackgroundExtent(hTheme, gc.handle, part[0], part[1], rect, extent);
38         OS.CloseThemeData(hTheme);
39         if ((style & SWT.DROP_DOWN) != 0) {
40             SIZE size = new SIZE();
41             part = getPartId(DrawData.TOOLITEM_ARROW);
42             OS.GetThemePartSize(hTheme, 0, part[0], part[1], null, OS.TS_TRUE, size);
43             extent.right = Math.max(extent.left, extent.right + size.cx);
44         } else {
45             extent.left -= INSET;
46             extent.top -= INSET;
47             extent.right += INSET;
48             extent.bottom += INSET;
49         }
50         return new Rectangle(extent.left, extent.top, extent.right - extent.left, extent.bottom - extent.top);
51     }
52     return new Rectangle(0, 0, 0, 0);
53 }
54
55 void draw(Theme theme, GC gc, Rectangle bounds) {
56     if (OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ()) {
57         int hTheme = OS.OpenThemeData(0, getClassId());
58         RECT rect = new RECT ();
59         rect.left = bounds.x;
60         rect.right = bounds.x + bounds.width;
61         rect.top = bounds.y;
62         rect.bottom = bounds.y + bounds.height;
63         SIZE size = null;
64         int[] dropPart = null;
65         if ((style & SWT.DROP_DOWN) != 0) {
66             size = new SIZE();
67             dropPart = getPartId(DrawData.TOOLITEM_ARROW);
68             OS.GetThemePartSize(hTheme, gc.handle, dropPart[0], dropPart[1], rect, OS.TS_TRUE, size);
69             rect.right -= size.cx;
70             if (rect.right < rect.left) rect.right = rect.left;
71         }
72         int[] part = getPartId(DrawData.WIDGET_WHOLE);
73         OS.DrawThemeBackground(hTheme, gc.handle, part[0], part[1], rect, null);
74         Rectangle clientArea = this.clientArea;
75         if (clientArea != null) {
76             RECT contentRect = new RECT();
77             OS.GetThemeBackgroundContentRect(hTheme, gc.handle, part[0], part[1], rect, contentRect);
78             clientArea.x = contentRect.left;
79             clientArea.y = contentRect.top;
80             clientArea.width = contentRect.right - contentRect.left;
81             clientArea.height = contentRect.bottom - contentRect.top;
82         }
83         if ((style & SWT.DROP_DOWN) != 0) {
84             rect.left = rect.right;
85             rect.right = rect.left + size.cx;
86             OS.DrawThemeBackground(hTheme, gc.handle, dropPart[0], dropPart[1], rect, null);
87         }
88         OS.CloseThemeData(hTheme);
89     }
90 }
91
92 char[] getClassId() {
93     return TOOLBAR;
94 }
95
96 int[] getPartId(int part) {
97     int state = this.state[part];
98     int iPartId = 0, iStateId = 0;
99     switch (part) {
100         case DrawData.WIDGET_WHOLE:
101             if ((style & (SWT.PUSH | SWT.CHECK | SWT.RADIO)) != 0) {
102                 iPartId = OS.TP_BUTTON;
103             } else if ((style & SWT.DROP_DOWN) != 0) {
104                 iPartId = OS.TP_SPLITBUTTON;
105             } else if ((style & SWT.SEPARATOR) != 0) {
106                 if ((parent.style & SWT.VERTICAL) != 0) {
107                     iPartId = OS.TP_SEPARATORVERT;
108                 } else {
109                     iPartId = OS.TP_SEPARATOR;
110                 }
111             }
112             if ((style & SWT.SEPARATOR) == 0) {
113                 if ((state & DrawData.HOT) != 0) {
114                     if ((style & (SWT.RADIO | SWT.CHECK)) != 0 && (state & DrawData.SELECTED) != 0) {
115                         iStateId = OS.TS_HOTCHECKED;
116                     } else {
117                         iStateId = OS.TS_HOT;
118                     }
119                 }
120                 if ((style & (SWT.RADIO | SWT.CHECK)) != 0 && (state & DrawData.SELECTED) != 0) {
121                     iStateId = OS.TS_CHECKED;
122                 }
123                 if ((state & DrawData.PRESSED) != 0) iStateId = OS.TS_PRESSED;
124                 if ((state & DrawData.DISABLED) != 0) iStateId = OS.TS_DISABLED;
125             }
126             break;
127         case DrawData.TOOLITEM_ARROW:
128             iPartId = OS.TP_SPLITBUTTONDROPDOWN;
129             if ((state & DrawData.HOT) != 0) iStateId = OS.TS_HOT;
130             if ((state & DrawData.PRESSED) != 0) iStateId = OS.TS_PRESSED;
131             if ((state & DrawData.DISABLED) !=0) iStateId = OS.TS_DISABLED;
132             break;
133     }
134     return new int[]{iPartId, iStateId};
135 }
136
137 int hit(Theme theme, Point position, Rectangle bounds) {
138     if (!(OS.COMCTL32_MAJOR >= 6 && OS.IsAppThemed ())) return DrawData.WIDGET_NOWHERE;
139     if (!bounds.contains(position)) return DrawData.WIDGET_NOWHERE;
140     int hTheme = OS.OpenThemeData(0, getClassId());
141     try {
142         RECT rect = new RECT ();
143         rect.left = bounds.x;
144         rect.right = bounds.x + bounds.width;
145         rect.top = bounds.y;
146         rect.bottom = bounds.y + bounds.height;
147         POINT pt = new POINT();
148         pt.x = position.x;
149         pt.y = position.y;
150         short[] code = new short[1];
151         int[] part = getPartId(DrawData.WIDGET_WHOLE);
152         OS.HitTestThemeBackground(hTheme, 0, part[0], part[1], 0, rect, 0, pt, code);
153         if (code[0] == OS.HTNOWHERE) return DrawData.WIDGET_NOWHERE;
154         int style = this.style;
155         if ((style & SWT.DROP_DOWN) != 0) {
156             SIZE size = new SIZE();
157             part = getPartId(DrawData.TOOLITEM_ARROW);
158             OS.GetThemePartSize(hTheme, 0, part[0], part[1], rect, OS.TS_TRUE, size);
159             rect.left = Math.max(rect.left, rect.right - size.cx);
160             OS.HitTestThemeBackground(hTheme, 0, part[0], part[1], 0, rect, 0, pt, code);
161             if (code[0] != OS.HTNOWHERE) return DrawData.TOOLITEM_ARROW;
162         }
163     } finally {
164         OS.CloseThemeData(hTheme);
165     }
166     return DrawData.WIDGET_WHOLE;
167 }
168
169 }
170
Popular Tags