KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > gtk > BluecurveEngine


1 /*
2  * @(#)BluecurveEngine.java 1.6 04/01/09
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package com.sun.java.swing.plaf.gtk;
8
9 import javax.swing.plaf.synth.*;
10 import java.awt.*;
11 import javax.swing.*;
12 import javax.swing.plaf.*;
13
14 /**
15  * A bluecurve like engine.
16  *
17  * @version 1.6, 01/09/04
18  * @author Scott Violet
19  */

20 class BluecurveEngine extends GTKEngine {
21     public void paintSlider(SynthContext context, Graphics g, int state,
22                             int shadowType, String JavaDoc info,
23                             int x, int y, int w, int h, int orientation) {
24         Region region = context.getRegion();
25         if (region == Region.SLIDER_THUMB) {
26             BluecurveStyle style = (BluecurveStyle)context.getStyle();
27             JComponent c = context.getComponent();
28             paintBackground(context, g, state, style.getGTKColor(
29                      c, region, state, GTKColorType.BACKGROUND), x, y, w, h);
30             g.setColor(style.getGTKColor(c, region, state,
31                                          BluecurveColorType.OUTER3));
32             g.drawLine(x + 2, y, x + w - 3, y);
33             g.drawLine(x, y + 2, x, y + h - 3);
34             g.drawLine(x + w - 1, y + 2, x + w - 1, y + h - 3);
35             g.drawLine(x + 2, y + h - 1, x + w - 3, y + h - 1);
36             g.fillRect(x + 1, y + 1, 1, 1);
37             g.fillRect(x + w - 2, y + 1, 1, 1);
38             g.fillRect(x + 1, y + h - 2, 1, 1);
39             g.fillRect(x + w - 2, y + h - 2, 1, 1);
40
41             g.setColor(style.getGTKColor(c, region, state,
42                                          BluecurveColorType.WHITE));
43             g.drawLine(x + 2, y + 1, x + w - 3, y + 1);
44             g.drawLine(x + 1, y + 2, x + 1, y + h - 3);
45
46             g.setColor(style.getGTKColor(c, region, state,
47                                          BluecurveColorType.INNER_RIGHT2));
48             g.drawLine(x + 2, y + h - 2, x + w - 3, y + h - 2);
49             g.drawLine(x + w - 2, y + 2, x + w - 2, y + h - 3);
50             g.drawLine(x, y + 1, x + 1, y);
51             g.drawLine(x, y + h - 2, x + 1, y + h - 1);
52             g.drawLine(x + w - 2, y + h - 1, x + w - 1, y + h - 2);
53             g.drawLine(x + w - 2, y, x + w - 1, y + 1);
54
55             if (((JSlider)c).getOrientation() == SwingConstants.HORIZONTAL &&
56                      w > 12) {
57                 paintHash(context, g, state, x + w / 2 - 5, y + h / 2 - 2, 3);
58                 paintHash(context, g, state, x + w / 2 - 3, y + h / 2 - 3, 6);
59                 paintHash(context, g, state, x + w / 2 +2, y + h / 2 - 1, 3);
60             }
61             else if (((JSlider)c).getOrientation() ==
62                                SwingConstants.VERTICAL && h > 12) {
63                 paintHash(context, g, state, x + w / 2 - 2, y + h / 2 - 5, 3);
64                 paintHash(context, g, state, x + w / 2 - 3, y + h / 2 - 3, 6);
65                 paintHash(context, g, state, x + w / 2 - 1, y + h / 2 + 2, 3);
66             }
67         }
68         else {
69             super.paintSlider(context, g, state, shadowType, info, x, y, w, h,
70                               orientation);
71             if (context.getRegion() == Region.SCROLL_BAR_THUMB) {
72                 paintHashes(context, g, state, x, y, w, h, orientation, 3, 5);
73             }
74         }
75     }
76
77     private void paintHash(SynthContext context, Graphics g, int state,
78                              int x, int y, int size) {
79         GTKStyle style = (GTKStyle)context.getStyle();
80
81         g.setColor(style.getGTKColor(context.getComponent(),
82                                      context.getRegion(), state,
83                                      BluecurveColorType.OUTER2));
84         g.drawLine(x, y + size, x + size, y);
85
86         g.setColor(style.getGTKColor(context.getComponent(),
87                                      context.getRegion(), state,
88                                      GTKColorType.WHITE));
89         g.drawLine(x + 1, y + size, x + size, y + 1);
90     }
91
92     private void paintHashes(SynthContext context, Graphics g, int state,
93                              int x, int y, int w, int h, int orientation,
94                              int count, int size) {
95         // 3 diagonal lines 5x5
96
GTKStyle style = (GTKStyle)context.getStyle();
97         if (orientation == GTKConstants.HORIZONTAL) {
98             if (w < size * count + 4) {
99                 return;
100             }
101             int x0 = x + (w - size * count) / 2;
102             int y0 = y + (h - size) / 2;
103
104             g.setColor(style.getGTKColor(context.getComponent(),
105                                          context.getRegion(), state,
106                                          BluecurveColorType.OUTER2));
107             for (int counter = 0; counter < count; counter++) {
108                 g.drawLine(x0 + counter * size, y0 + size,
109                            x0 + (counter + 1) * size, y0);
110             }
111
112             g.setColor(style.getGTKColor(context.getComponent(),
113                                          context.getRegion(), state,
114                                          GTKColorType.WHITE));
115             for (int counter = 0; counter < count; counter++) {
116                 g.drawLine(x0 + counter * size + 1, y0 + size,
117                            x0 + (counter + 1) * size, y0 + 1);
118             }
119         }
120         else if (orientation == GTKConstants.VERTICAL) {
121             if (h < size * count + 4) {
122                 return;
123             }
124             int x0 = x + (w - size) / 2;
125             int y0 = y + (h - size * count) / 2;
126
127             g.setColor(style.getGTKColor(context.getComponent(),
128                                          context.getRegion(), state,
129                                          BluecurveColorType.OUTER2));
130             for (int counter = 0; counter < count; counter++) {
131                 g.drawLine(x0, y0 + (counter + 1) * size, x0 + size,
132                            y0 + (counter * size));
133             }
134             g.setColor(style.getGTKColor(context.getComponent(),
135                                          context.getRegion(), state,
136                                          GTKColorType.WHITE));
137             for (int counter = 0; counter < count; counter++) {
138                 g.drawLine(x0 + 1, y0 + (counter + 1) * size, x0 + size,
139                            y0 + counter * size + 1);
140             }
141         }
142     }
143
144     public void paintBox(SynthContext context, Graphics g, int state,
145                          int shadowType, String JavaDoc info, int x, int y,
146                          int w, int h) {
147         GTKStyle style = (GTKStyle)context.getStyle();
148         Region region = context.getRegion();
149         if (info != "trough" || region != Region.SLIDER_TRACK) {
150             paintBackground(context, g, state,
151                       style.getGTKColor(context.getComponent(), region, state,
152                                         GTKColorType.BACKGROUND), x, y, w, h);
153         }
154         paintShadow(context, g, state, shadowType, info, x, y, w, h);
155     }
156
157     public void paintShadow(SynthContext context, Graphics g, int state,
158                             int shadowType, String JavaDoc info, int x, int y,
159                             int w, int h) {
160         if (info == "menubar") {
161             // This isn't really dark, but not sure what color they're using
162
// here
163
g.setColor(((GTKStyle)context.getStyle()).getGTKColor(
164                            context.getComponent(), context.getRegion(), state,
165                            BluecurveColorType.OUTER4));
166             g.drawLine(x, y + h - 1, x + w, y + h - 1);
167             return;
168         }
169         if (info == "buttondefault") {
170             // YES, this appears to be special cased.
171
g.setColor(((GTKStyle)context.getStyle()).getGTKColor(
172                            context.getComponent(), context.getRegion(),
173                            state, GTKColorType.BLACK));
174             g.drawRect(x, y, w - 1, h - 1);
175             return;
176         }
177         BluecurveStyle style = (BluecurveStyle)context.getStyle();
178         JComponent c = context.getComponent();
179         Region region = context.getRegion();
180         int xThickness = style.getXThickness();
181         int yThickness = style.getYThickness();
182
183         if (info == "trough") {
184             // YES, this appears to be special cased.
185
xThickness = yThickness = 1;
186             if (region == Region.SLIDER_TRACK) {
187                 if (((JSlider)c).getOrientation() ==SwingConstants.HORIZONTAL){
188                     if (h > 5) {
189                         y = y + h / 2 - 2;
190                         h = 5;
191                     }
192                 }
193                 else if (w > 5) {
194                     x = x + w / 2 - 2;
195                     w = 5;
196                 }
197             }
198         }
199         else if (info == "bar") {
200             if (xThickness < 2) {
201                 x -= xThickness;
202                 y -= yThickness;
203                 w += xThickness + xThickness;
204                 h += yThickness + yThickness;
205                 xThickness = yThickness = 2;
206             }
207         }
208         if (xThickness < 0 && yThickness < 0) {
209             // nothing to paint.
210
return;
211         }
212         Color upperLeft = null, innerLeft = null, bottomRight = null,
213               innerRight = null;
214         if (info == "menu" || (info == "trough" &&
215                     (region == Region.PROGRESS_BAR || region ==
216                      Region.SLIDER_TRACK)) || info == "entry") {
217             if (info != "menu" && info != "entry") {
218                 g.setColor(style.getGTKColor(c, region, state,
219                                              BluecurveColorType.OUTER4));
220                 g.fillRect(x, y, w, h);
221             }
222             upperLeft = bottomRight = style.getGTKColor(c, region, state,
223                               BluecurveColorType.OUTER2);
224             if (shadowType == GTKConstants.SHADOW_OUT) {
225                 innerLeft = style.getGTKColor(c, region, state,
226                                               BluecurveColorType.WHITE);
227                 innerRight = style.getGTKColor(c, region, state,
228                                             BluecurveColorType.INNER_RIGHT2);
229             }
230             else {
231                 innerLeft = style.getGTKColor(c, region, state,
232                                               BluecurveColorType.INNER_RIGHT2);
233                 innerRight = style.getGTKColor(c, region, state,
234                                             BluecurveColorType.WHITE);
235             }
236         }
237         else if (info != "menuitem" && info != "bar") {
238             upperLeft = bottomRight = style.getGTKColor(c, region, state,
239                               BluecurveColorType.OUTER3);
240             if (shadowType == GTKConstants.SHADOW_OUT) {
241                 innerLeft = style.getGTKColor(c, region, state,
242                                               BluecurveColorType.WHITE);
243                 innerRight = style.getGTKColor(c, region, state,
244                                               BluecurveColorType.INNER_RIGHT2);
245             }
246             else {
247                 innerLeft = style.getGTKColor(c, region, state,
248                                               BluecurveColorType.INNER_RIGHT2);
249                 innerRight = style.getGTKColor(c, region, state,
250                                               BluecurveColorType.WHITE);
251             }
252         }
253         else {
254             upperLeft = bottomRight = style.getGTKColor(c, region,
255                  SynthConstants.SELECTED, BluecurveColorType.OUTER);
256             switch (shadowType) {
257             case GTKConstants.SHADOW_OUT:
258                 innerLeft = style.getGTKColor(c, region,
259                      SynthConstants.SELECTED, BluecurveColorType.INNER_LEFT);
260                 innerRight = style.getGTKColor(c, region,
261                      SynthConstants.SELECTED, BluecurveColorType.INNER_RIGHT);
262                 break;
263             case GTKConstants.SHADOW_IN:
264                 innerRight = style.getGTKColor(c, region,
265                      SynthConstants.SELECTED, BluecurveColorType.INNER_LEFT);
266                 innerLeft = style.getGTKColor(c, region,
267                      SynthConstants.SELECTED, BluecurveColorType.INNER_RIGHT);
268                 break;
269             default:
270                 assert true : "Unknown shadow type!";
271             }
272         }
273         _paintShadow(g, x, y, w, h, xThickness, yThickness, upperLeft,
274                 innerLeft, bottomRight, innerRight);
275         if (info == "menuitem" || info == "bar") {
276             // Draw the GradientPaint
277
int gw = Math.min(2, xThickness);
278             int gh = Math.min(2, yThickness);
279             Color topColor = style.getGTKColor(c, region,
280                   SynthConstants.SELECTED,BluecurveColorType. TOP_GRADIENT);
281             Color bottomColor = style.getGTKColor(c, region,
282                   SynthConstants.SELECTED,BluecurveColorType. BOTTOM_GRADIENT);
283             GradientPaint paint = new GradientPaint((float)gw, (float)gh,
284                        topColor, (float)gw, (float)(h - gh - gh), bottomColor);
285             g.translate(x, y);
286             ((Graphics2D)g).setPaint(paint);
287             g.fillRect(gw, gh, w - gw - gw, h - gh - gh);
288             ((Graphics2D)g).setPaint(null);
289             g.translate(-x, -y);
290         }
291     }
292
293     public void paintArrow(SynthContext context, Graphics g, int state,
294                            int shadowType, int direction, String JavaDoc info,
295                            int x, int y, int w, int h) {
296         // Draw the arrow
297
int sizeW = w / 4 + 1;
298         int sizeH = h / 4 + 1;
299         int size = Math.max(2, Math.min(sizeW, sizeH));
300
301         switch (direction) {
302         case GTKConstants.ARROW_UP:
303             x += w / 2 - 1;
304             y += (h - size) / 2;
305             break;
306         case GTKConstants.ARROW_DOWN:
307             x += w / 2 - 1;
308             y += (h - size) / 2 + 1;
309             break;
310         case GTKConstants.ARROW_LEFT:
311             x += (w - size) / 2;
312             y += h / 2 - 1;
313             break;
314         case GTKConstants.ARROW_RIGHT:
315             x += (w - size) / 2 + 1;
316             y += h / 2 - 1;
317             break;
318         }
319
320         GTKStyle style = (GTKStyle)context.getStyle();
321         int mid, i, j;
322
323         j = 0;
324         mid = (size / 2) - 1;
325
326         g.translate(x, y);
327
328         // PENDING: this isn't the right color.
329
g.setColor(style.getGTKColor(context.getComponent(),
330                    context.getRegion(), state, BluecurveColorType.OUTER5));
331
332         switch(direction) {
333         case GTKConstants.ARROW_UP:
334             for(i = 0; i < size; i++) {
335                 g.drawLine(mid-i, i, mid+i, i);
336             }
337             g.fillRect(mid - size + 2, size, 1, 1);
338             g.fillRect(mid + size - 2, size, 1, 1);
339             break;
340         case GTKConstants.ARROW_DOWN:
341             j = 0;
342             for (i = size-1; i >= 0; i--) {
343                 g.drawLine(mid-i, j, mid+i, j);
344                 j++;
345             }
346             g.fillRect(mid - size + 2, -1, 1, 1);
347             g.fillRect(mid + size - 2, -1, 1, 1);
348             break;
349         case GTKConstants.ARROW_LEFT:
350             for (i = 0; i < size; i++) {
351                 g.drawLine(i, mid-i, i, mid+i);
352             }
353             g.fillRect(size, mid - size + 2, 1, 1);
354             g.fillRect(size, mid + size - 2, 1, 1);
355             break;
356         case GTKConstants.ARROW_RIGHT:
357             j = 0;
358             for (i = size-1; i >= 0; i--) {
359                 g.drawLine(j, mid-i, j, mid+i);
360                 j++;
361             }
362             g.fillRect(-1, mid - size + 2, 1, 1);
363             g.fillRect(-1, mid + size - 2, 1, 1);
364             break;
365         }
366         g.translate(-x, -y);
367     }
368
369     public void paintHandle(SynthContext context, Graphics g, int paintState,
370                             int shadowType, String JavaDoc info, int x, int y,
371                             int w, int h, int orientation) {
372         paintHashes(context, g, paintState, x, y, w, h, orientation, 5, 4);
373     }
374
375     public void paintOption(SynthContext context, Graphics g, int paintState,
376                             int shadowType, String JavaDoc info, int x, int y,
377                             int w, int h) {
378         if (info == "option") {
379             int componentState = context.getComponentState();
380             if ((componentState & SynthConstants.SELECTED) != 0) {
381                 g.translate(x, y);
382                 int centerY = h / 2 - 1;
383                 JComponent component = context.getComponent();
384                 Region region = context.getRegion();
385                 GTKStyle style = (GTKStyle)context.getStyle();
386
387                 if ((componentState & SynthConstants.MOUSE_OVER) != 0) {
388                     g.setColor(style.getGTKColor(component, region, paintState,
389                                                  GTKColorType.WHITE));
390                 }
391                 else {
392                     g.setColor(style.getGTKColor(component, region, paintState,
393                                                  GTKColorType.BLACK));
394                 }
395                 g.fillRect(5, centerY, 5, 3);
396                 g.drawLine(6, centerY - 1, 8, centerY - 1);
397                 g.drawLine(6, centerY + 3, 8, centerY + 3);
398                 g.translate(-x, -y);
399             }
400             return;
401         }
402         super.paintOption(context, g, paintState, shadowType, info, x, y,
403                           w, h);
404         if (info == "radiobutton") {
405             if ((context.getComponentState() & SynthConstants.SELECTED) != 0) {
406                 // PENDING: this should be a gradient.
407
int centerY = h / 2 - 1;
408                 g.translate(x, y);
409                 g.setColor(((GTKStyle)context.getStyle()).getGTKColor(context.
410                            getComponent(), context.getRegion(), paintState,
411                            BluecurveColorType.OUTER));
412                 g.fillRect(5, centerY, 5, 3);
413                 g.drawLine(6, centerY - 1, 8, centerY - 1);
414                 g.drawLine(6, centerY + 3, 8, centerY + 3);
415                 g.translate(-x, -y);
416             }
417         }
418     }
419
420     public void paintExtension(SynthContext context, Graphics g, int state,
421                                int shadowType, String JavaDoc info, int x, int y,
422                                int w, int h, int placement, int tabIndex) {
423         _paintExtension(context, g, state, shadowType, x, y, w, h, placement,
424                    BluecurveColorType.OUTER3, GTKColorType.BACKGROUND,
425                    BluecurveColorType.OUTER3, BluecurveColorType.INNER_RIGHT2,
426                         true, tabIndex);
427     }
428
429     public void paintBoxGap(SynthContext context, Graphics g, int state,
430                             int shadowType, String JavaDoc info, int x, int y,
431                             int w, int h, int boxGapType, int tabBegin,
432                             int size) {
433         _paintBoxGap(context, g, state, shadowType, x, y, w, h, boxGapType,
434                      tabBegin, size, GTKColorType.BACKGROUND,
435                      BluecurveColorType.OUTER3, BluecurveColorType.OUTER3,
436                      BluecurveColorType.INNER_RIGHT2, true);
437     }
438
439     Color getFocusColor(SynthContext context, int state) {
440         return ((BluecurveStyle)context.getStyle()).getGTKColor(
441                context.getComponent(), context.getRegion(),
442                SynthConstants.SELECTED, BluecurveColorType.OUTER3);
443     }
444 }
445
Popular Tags