KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)GTKEngine.java 1.60 04/04/16
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
13 /**
14  * GTKEngine mirrors that of a a GtkStyleClass.
15  * <p>
16  * The states that are passed in correspond to gtk's set of states:
17  * <table>
18  * <tr><td>Synth<td>GTK
19  * <tr><td>GTKConstants.ENABLED<td>NORMAL
20  * <tr><td>GTKConstants.PRESSED<td>ACTIVE
21  * <tr><td>GTKConstants.SELECTED<td>SELECTED
22  * <tr><td>GTKConstants.MOUSE_OVER<td>PRELIGHT
23  * <tr><td>GTKConstants.DISABLED<td>INSENSITIVE
24  * </table>
25  * @version 1.60, 04/16/04
26  * @author Josh Outwater
27  * @author Scott Violet
28  */

29 public class GTKEngine {
30     static final GTKEngine INSTANCE = new GTKEngine();
31
32     static final int[] DEFAULT_FOCUS_PATTERN = new int[] {1, 1};
33
34     static int adjustAxisForComponentOrientation(JComponent c,
35                                                  int axis) {
36         if (c.getComponentOrientation().isLeftToRight()) {
37             return axis;
38         }
39         switch (axis) {
40         case SwingConstants.EAST:
41             return SwingConstants.WEST;
42         case SwingConstants.WEST:
43             return SwingConstants.EAST;
44         }
45         return axis;
46     }
47
48
49     public void paintArrow(SynthContext context, Graphics g, int state,
50                            int shadowType, int direction, String JavaDoc info,
51                            int x, int y, int w, int h) {
52         int sizeW = w / 4 + 1;
53         int sizeH = h / 4 + 1;
54         int size = Math.max(2, Math.min(sizeW, sizeH));
55
56         switch (direction) {
57         case GTKConstants.ARROW_UP:
58         case GTKConstants.ARROW_DOWN:
59             x += (w - size * 2 + 1) / 2;
60             y += (h - size) / 2;
61             break;
62         case GTKConstants.ARROW_LEFT:
63         case GTKConstants.ARROW_RIGHT:
64             x += (w - size) / 2;
65             y += (h - size * 2 + 1) / 2;
66             break;
67         }
68
69         GTKStyle style = (GTKStyle)context.getStyle();
70         int i;
71         int arW = size * 2 - 2;
72
73         g.translate(x, y);
74
75         if (state == SynthConstants.DISABLED) {
76             g.setColor(style.getGTKColor(context.getComponent(),
77                        context.getRegion(), state, GTKColorType.WHITE));
78         }
79         else {
80             g.setColor(style.getGTKColor(context.getComponent(),
81                        context.getRegion(), state, GTKColorType.FOREGROUND));
82         }
83
84         switch(direction) {
85         case GTKConstants.ARROW_UP:
86             for(i = 0; i < size; i++) {
87                 g.drawLine(i, size - i - 1, arW - i, size - i - 1);
88             }
89             break;
90         case GTKConstants.ARROW_DOWN:
91             for (i = 0; i < size; i++) {
92                 g.drawLine(i, i, arW - i, i);
93             }
94             break;
95         case GTKConstants.ARROW_LEFT:
96             int j;
97             for (j = 0, i = size - 1; i >= 0; i--, j++) {
98                 g.drawLine(i, j, i, arW - j);
99             }
100             break;
101         case GTKConstants.ARROW_RIGHT:
102             for (i = 0; i < size; i++) {
103                 g.drawLine(i, i, i, arW - i);
104             }
105             break;
106         }
107         g.translate(-x, -y);
108     }
109
110     public void paintBox(SynthContext context, Graphics g, int state,
111                          int shadowType, String JavaDoc info, int x, int y,
112                          int w, int h) {
113         GTKStyle style = (GTKStyle)context.getStyle();
114         Region region = context.getRegion();
115         // This should really happen after the shadow is drawn.
116
if (info == "menuitem" && state == SynthConstants.MOUSE_OVER &&
117                                   GTKLookAndFeel.is2_2()) {
118             paintBackground(context, g, state,
119                  style.getGTKColor(context.getComponent(), region,
120                  SynthConstants.MOUSE_OVER, GTKColorType.BACKGROUND), x, y, w,h);
121         }
122         else {
123             paintBackground(context, g, state,
124                  style.getGTKColor(context.getComponent(), region, state,
125                  GTKColorType.BACKGROUND), x, y, w, h);
126         }
127         paintShadow(context, g, state, shadowType, info, x, y, w, h);
128     }
129
130     public void paintBoxGap(SynthContext context, Graphics g, int state,
131                             int shadowType, String JavaDoc info, int x, int y,
132                             int w, int h, int boxGapType, int tabBegin,
133                             int size) {
134         _paintBoxGap(context, g, state, shadowType, x, y, w, h, boxGapType,
135                      tabBegin, size, GTKColorType.BACKGROUND,
136                      GTKColorType.LIGHT, GTKColorType.BLACK,
137                      GTKColorType.DARK, false);
138     }
139
140     void _paintBoxGap(SynthContext context, Graphics g, int state,
141                       int shadowType, int x, int y, int w, int h,
142                       int boxGapType, int tabBegin, int size,
143                       ColorType backgroundType, ColorType upperLeftType,
144                       ColorType bottomRightType, ColorType innerRightType,
145                       boolean paintWhite) {
146         GTKStyle style = (GTKStyle)context.getStyle();
147         JComponent c = context.getComponent();
148         Region region = context.getRegion();
149
150         paintBackground(context, g, state, style.getGTKColor(c, region, state,
151                         backgroundType), x, y, w, h);
152         int tabEnd = tabBegin + size;
153
154         Color upperLeft = style.getGTKColor(c, region, state, upperLeftType);
155         Color bottomRight = style.getGTKColor(c, region, state,
156                                               bottomRightType);
157         Color innerRight = style.getGTKColor(c, region, state, innerRightType);
158         Color white = style.getGTKColor(c, region, state, GTKColorType.WHITE);
159
160         g.translate(x, y);
161         if (boxGapType == GTKConstants.TOP) {
162             // Paint bottom.
163
g.setColor(bottomRight);
164             g.drawLine(0, h - 1, w - 1, h - 1);
165             g.setColor(innerRight);
166             g.drawLine(1, h - 2, w - 2, h - 2);
167
168             // Paint left side.
169
g.setColor(upperLeft);
170             g.drawLine(0, 0, 0, h - 2);
171             if (paintWhite) {
172                 g.setColor(white);
173                 g.drawLine(1, 0, 1, h - 3);
174             }
175
176             // Paint right side.
177
g.setColor(bottomRight);
178             g.drawLine(w - 1, 0, w - 1, h - 1);
179             g.setColor(innerRight);
180             g.drawLine(w - 2, 0, w - 2, h - 2);
181
182             // Paint top.
183
if (tabBegin > 1) {
184                 g.setColor(upperLeft);
185                 g.drawLine(0, 0, tabBegin - 2, 0);
186             }
187             g.setColor(upperLeft);
188             g.drawLine(tabEnd - 1, 0, w - 2, 0);
189             if (paintWhite) {
190                 g.setColor(white);
191                 if (tabBegin > 0) {
192                     g.drawLine(0, 1, tabBegin, 1);
193                 }
194                 g.drawLine(tabEnd, 1, w - 2, 1);
195             }
196         }
197         else if (boxGapType == GTKConstants.BOTTOM) {
198             // Paint top.
199
g.setColor(upperLeft);
200             g.drawLine(0, 0, w - 1, 0);
201             if (paintWhite) {
202                 g.setColor(white);
203                 g.drawLine(0, 1, w - 1, 1);
204             }
205
206             // Paint left side.
207
g.setColor(upperLeft);
208             g.drawLine(0, 0, 0, h - 1);
209             if (paintWhite) {
210                 g.setColor(white);
211                 g.drawLine(1, 1, 1, h - 2);
212             }
213
214             // Paint right side.
215
g.setColor(bottomRight);
216             g.drawLine(w - 1, 0, w - 1, h - 1);
217             g.setColor(innerRight);
218             g.drawLine(w - 2, 1, w - 2, h - 2);
219
220             // Paint bottom.
221
if (tabBegin > 1) {
222                 g.setColor(bottomRight);
223                 g.drawLine(0, h - 1, tabBegin - 2, h - 1);
224                 g.setColor(innerRight);
225                 g.drawLine(tabBegin - 1, h - 1, tabBegin - 1, h - 1);
226                 g.drawLine(1, h - 2, tabBegin - 2, h - 2);
227             }
228             g.setColor(bottomRight);
229             g.drawLine(tabEnd - 1, h - 1, w - 1, h - 1);
230             g.setColor(innerRight);
231             g.drawLine(tabEnd - 2, h - 1, tabEnd - 2, h - 1);
232             g.drawLine(tabEnd - 1, h - 2, w - 2, h - 2);
233         }
234         else if (boxGapType == GTKConstants.RIGHT) {
235             // Paint top.
236
g.setColor(upperLeft);
237             g.drawLine(0, 0, w - 1, 0);
238             if (paintWhite) {
239                 g.setColor(white);
240                 g.drawLine(0, 1, w - 1, 1);
241             }
242
243             // Paint bottom.
244
g.setColor(bottomRight);
245             g.drawLine(0, h - 1, w - 1, h - 1);
246             g.setColor(innerRight);
247             g.drawLine(1, h - 2, w - 2, h - 2);
248
249             // Paint left side.
250
g.setColor(upperLeft);
251             g.drawLine(0, 0, 0, h - 2);
252             if (paintWhite) {
253                 g.setColor(white);
254                 g.drawLine(1, 1, 1, h - 3);
255             }
256
257             if (tabBegin > 1) {
258                 g.setColor(bottomRight);
259                 g.drawLine(w - 1, 0, w - 1, tabBegin - 2);
260                 g.setColor(innerRight);
261                 g.drawLine(w - 1, tabBegin - 1, w - 1, tabBegin - 1);
262                 g.drawLine(w - 2, 1, w - 2, tabBegin - 2);
263             }
264             g.setColor(bottomRight);
265             g.drawLine(w - 1, tabEnd - 1, w - 1, h - 1);
266             g.setColor(innerRight);
267             g.drawLine(w - 1, tabEnd - 2, w - 1, tabEnd - 2);
268             g.drawLine(w - 2, tabEnd - 1, w - 2, h - 2);
269         }
270         else if (boxGapType == GTKConstants.LEFT) {
271             // Paint top.
272
g.setColor(upperLeft);
273             g.drawLine(0, 0, w - 2, 0);
274             if (paintWhite) {
275                 g.setColor(white);
276                 g.drawLine(0, 1, w - 2, 1);
277             }
278
279             // Paint bottom.
280
g.setColor(bottomRight);
281             g.drawLine(0, h - 1, w - 1, h - 1);
282             g.setColor(innerRight);
283             g.drawLine(1, h - 2, w - 2, h - 2);
284
285             // Paint right side.
286
g.setColor(bottomRight);
287             g.drawLine(w - 1, 1, w - 1, h - 1);
288             g.setColor(innerRight);
289             g.drawLine(w - 2, 0, w - 2, h - 2);
290
291             // Paint left side.
292
if (tabBegin > 1) {
293                 g.setColor(upperLeft);
294                 g.drawLine(0, 0, 0, tabBegin - 2);
295             }
296             g.setColor(upperLeft);
297             g.drawLine(0, tabEnd - 1, 0, h - 2);
298             if (paintWhite) {
299                 g.setColor(white);
300                 if (tabBegin > 1) {
301                     g.drawLine(1, 0, 1, tabBegin - 1);
302                 }
303                 g.drawLine(1, tabEnd - 1, 1, h - 3);
304             }
305         }
306         g.translate(-x, -y);
307     }
308
309     public void paintCheck(SynthContext context, Graphics g, int state,
310                            int shadowType, String JavaDoc info, int x, int y,
311                            int w, int h) {
312         int componentState = context.getComponentState();
313         JComponent component = context.getComponent();
314         Region r = context.getRegion();
315         GTKStyle style = (GTKStyle)context.getStyle();
316
317         if ("checkbutton" == info) {
318             int checkState = ((componentState & SynthConstants.PRESSED) == 0) ?
319                     SynthConstants.ENABLED : SynthConstants.PRESSED;
320
321             paintFlatBoxText(context, g, checkState, info, x, y, w, h);
322             paintShadow(context, g, checkState, GTKConstants.SHADOW_IN, info, x, y, w, h);
323         }
324
325         g.translate(x, y);
326
327         int centerY = h / 2 - 1;
328
329         // Paint check mark in center if selected.
330
if ((componentState & SynthConstants.SELECTED) != 0) {
331             g.setColor(style.getGTKColor(component, r, state,
332                                              GTKColorType.FOREGROUND));
333             g.drawLine(3, centerY + 2, 4, centerY + 2);
334             g.drawLine(5, centerY + 3, 6, centerY + 3);
335             g.drawLine(6, centerY + 2, 6, centerY + 4);
336             g.drawLine(7, centerY + 1, 7, centerY);
337             g.drawLine(7, centerY, 8, centerY);
338             g.drawLine(8, centerY, 8, centerY - 1);
339             g.drawLine(8, centerY - 1, 9, centerY - 1);
340             g.drawLine(9, centerY - 1, 9, centerY - 2);
341
342             if (!GTKLookAndFeel.is2_2()) {
343                 if ((componentState & SynthConstants.PRESSED) != 0) {
344                     g.setColor(style.getGTKColor(component, r,
345                                SynthConstants.PRESSED, GTKColorType.DARK));
346                 } else {
347                     g.setColor(style.getGTKColor(component, r,
348                                SynthConstants.ENABLED, GTKColorType.DARK));
349                 }
350             }
351             g.drawLine(3, centerY + 1, 4, centerY + 1);
352             g.drawLine(4, centerY + 3, 4, centerY + 3);
353             g.drawLine(5, centerY + 2, 5, centerY + 2);
354             g.drawLine(6, centerY + 1, 6, centerY + 1);
355             g.drawLine(7, centerY + 2, 7, centerY + 2);
356             g.drawLine(10, centerY - 2, 10, centerY - 2);
357         }
358         g.translate(-x, -y);
359     }
360
361     public void paintExtension(SynthContext context, Graphics g, int state,
362                                int shadowType, String JavaDoc info, int x, int y,
363                                int w, int h, int placement, int tabIndex) {
364         _paintExtension(context, g, state, shadowType, x, y, w, h, placement,
365                         GTKColorType.LIGHT, GTKColorType.BACKGROUND,
366                         GTKColorType.BLACK, GTKColorType.DARK, false,tabIndex);
367     }
368
369     void _paintExtension(SynthContext context, Graphics g, int state,
370                     int shadowType, int x, int y,
371                     int w, int h, int placement, ColorType upperLeftType,
372                     ColorType backgroundType, ColorType bottomRightType,
373                     ColorType innerRightType, boolean paintWhite,
374                     int tabIndex) {
375         GTKStyle style = (GTKStyle)context.getStyle();
376         JComponent c = context.getComponent();
377         Region region = context.getRegion();
378         int xThickness = style.getXThickness();
379         int yThickness = style.getYThickness();
380
381         if (xThickness < 0 && yThickness < 0) {
382             // nothing to paint.
383
return;
384         }
385
386         Color upperLeft =
387             style.getGTKColor(c, region, state, upperLeftType);
388         Color background =
389             style.getGTKColor(c, region, state, backgroundType);
390         Color bottomRight =
391             style.getGTKColor(c, region, state, bottomRightType);
392         Color innerRight =
393             style.getGTKColor(c, region, state, innerRightType);
394         Color white = style.getGTKColor(c, region, state, GTKColorType.WHITE);
395         
396         int selectedIndex = ((JTabbedPane)c).getSelectedIndex();
397
398         g.translate(x, y);
399
400         // NOTE: placement refers to the size of the extension, so that
401
// BOTTOM corresponds to a TabbedPane with the tabs placed at the top.
402
if (placement == GTKConstants.BOTTOM) {
403             // Fill the background.
404
paintBackground(context, g, state, background, 0, 1, w, h);
405
406             if (tabIndex == selectedIndex || tabIndex == 0) {
407                 g.setColor(upperLeft);
408                 g.drawLine(0, 1, 0, h - 1);
409                 g.drawLine(1, 0, w - 2, 0);
410                 if (paintWhite) {
411                     g.setColor(white);
412                     g.drawLine(1, 1, 1, h - 1);
413                     g.drawLine(1, 1, w - 3, 1);
414                 }
415             } else {
416                 g.setColor(upperLeft);
417                 g.drawLine(0, 0, w - 2, 0);
418                 if (paintWhite) {
419                     g.setColor(white);
420                     g.drawLine(0, 1, w - 3, 1);
421                 }
422             }
423             if ((tabIndex + 1) != selectedIndex) {
424                 g.setColor(innerRight);
425                 g.drawLine(w - 2, 2, w - 2, h - 1);
426                 g.setColor(bottomRight);
427                 g.drawLine(w - 1, 1, w - 1, h - 1);
428             }
429             
430         } else if (placement == GTKConstants.TOP) {
431             // Fill the background.
432
paintBackground(context, g, state, background, 0, 0, w, h - 1);
433
434             if (tabIndex == selectedIndex || tabIndex == 0) {
435                 g.setColor(upperLeft);
436                 g.drawLine(0, 0, 0, h - 2);
437                 g.setColor(bottomRight);
438                 g.drawLine(1, h - 1, w - 2, h - 1);
439                 g.setColor(innerRight);
440                 g.drawLine(2, h - 2, w - 2, h - 2);
441                 if (paintWhite) {
442                     g.setColor(white);
443                     g.drawLine(1, 0, 1, h - 2);
444                 }
445             } else {
446                 g.setColor(bottomRight);
447                 g.drawLine(0, h - 1, w - 2, h - 1);
448                 g.setColor(innerRight);
449                 g.drawLine(0, h - 2, w - 2, h - 2);
450                 if (paintWhite) {
451                     g.setColor(white);
452                     g.drawLine(0, 0, 0, h - 2);
453                 }
454             }
455             if ((tabIndex + 1) != selectedIndex) {
456                 g.setColor(bottomRight);
457                 g.drawLine(w - 1, 0, w - 1, h - 2);
458                 g.setColor(innerRight);
459                 g.drawLine(w - 2, 0, w - 2, h - 2);
460             }
461         } else if (placement == GTKConstants.RIGHT) {
462             // Fill the background.
463
paintBackground(context, g, state, background, 1, 0, w, h);
464
465             if (tabIndex == selectedIndex || tabIndex == 0) {
466                 g.setColor(upperLeft);
467                 g.drawLine(1, 0, w - 1, 0);
468                 g.drawLine(0, 1, 0, h - 2);
469                 if (paintWhite) {
470                     g.setColor(white);
471                     g.drawLine(1, 1, w, 1);
472                     g.drawLine(1, 1, 1, h - 3);
473                 }
474             } else {
475                 g.setColor(upperLeft);
476                 g.drawLine(0, 0, 0, h - 2);
477                 if (paintWhite) {
478                     g.setColor(white);
479                     g.drawLine(1, 1, 1, h - 3);
480                 }
481             }
482             if ((tabIndex + 1) != selectedIndex) {
483                 g.setColor(bottomRight);
484                 g.drawLine(1, h - 1, w - 1, h - 1);
485                 g.setColor(innerRight);
486                 g.drawLine(2, h - 2, w - 1, h - 2);
487             }
488         } else if (placement == GTKConstants.LEFT) {
489             // Fill the background.
490
paintBackground(context, g, state, background, 0, 0, w - 1, h);
491
492             if (tabIndex == selectedIndex || tabIndex == 0) {
493                 g.setColor(upperLeft);
494                 g.drawLine(0, 0, w - 2, 0);
495                 g.setColor(bottomRight);
496                 g.drawLine(w - 1, 1, w - 1, h - 2);
497                 g.setColor(innerRight);
498                 g.drawLine(w - 2, 2, w - 2, h - 2);
499                 if (paintWhite) {
500                     g.setColor(white);
501                     g.drawLine(0, 1, w - 2, 1);
502                     g.drawLine(w - 2, 1, w - 2, h - 3);
503                 }
504             } else {
505                 g.setColor(bottomRight);
506                 g.drawLine(w - 1, 0, w - 1, h - 2);
507                 g.setColor(innerRight);
508                 g.drawLine(w - 2, 0, w - 2, h - 2);
509                 if (paintWhite) {
510                     g.setColor(white);
511                     g.drawLine(w - 2, 1, w - 2, h - 3);
512                 }
513             }
514             if ((tabIndex + 1) != selectedIndex) {
515                 g.setColor(bottomRight);
516                 g.drawLine(0, h - 1, w - 2, h - 1);
517                 g.setColor(innerRight);
518                 g.drawLine(0, h - 2, w - 2, h - 2);
519
520             }
521         }
522         g.translate(-x, -y);
523     }
524
525     void paintFlatBoxNormal(SynthContext context, Graphics g, int state,
526                              String JavaDoc key, int x, int y, int w, int h) {
527         g.setColor(((GTKStyle)context.getStyle()).getGTKColor(
528                 context.getComponent(), context.getRegion(), state,
529                 GTKColorType.BACKGROUND));
530         paintFlatBox(context, g, state, key, x, y, w, h);
531     }
532
533     void paintFlatBoxText(SynthContext context, Graphics g, int state,
534                              String JavaDoc key, int x, int y, int w, int h) {
535         g.setColor(((GTKStyle)context.getStyle()).getGTKColor(
536                 context.getComponent(), context.getRegion(), state,
537                 GTKColorType.TEXT_BACKGROUND));
538         paintFlatBox(context, g, state, key, x, y, w, h);
539     }
540
541     // NOTE: this assumes you have set the right color before calling this
542
public void paintFlatBox(SynthContext context, Graphics g, int state,
543                              String JavaDoc key, int x, int y, int w, int h) {
544         GTKStyle style = (GTKStyle)context.getStyle();
545
546         if (key == "cell_odd") {
547             g.fillRect(x, y, w, h);
548         }
549         else {
550             paintBackground(context, g, state, g.getColor(), x, y, w, h);
551         }
552
553         if (key == "tooltip") {
554             g.setColor(Color.BLACK);
555             g.drawRect(x, y, w - 1, h - 1);
556         }
557     }
558
559     public void paintFocus(SynthContext context, Graphics g, int state,
560                            String JavaDoc key, int x, int y, int w, int h) {
561         GTKStyle style = (GTKStyle)context.getStyle();
562         int[] pattern = (int[])style.getClassSpecificValue(
563                                      context, "focus-line-pattern");
564         int size = style.getClassSpecificIntValue(context,
565                                                   "focus-line-width", 1);
566
567         if (pattern == null) {
568             pattern = DEFAULT_FOCUS_PATTERN;
569         }
570         if (size <= 0) {
571             return;
572         }
573         g.setColor(getFocusColor(context, state));
574
575         _paintFocus(g, x, y, w, h, pattern, size);
576     }
577
578     void _paintFocus(Graphics g, int x, int y, int w, int h, int[] pattern,
579                      int size) {
580         g.translate(x, y);
581
582         // PENDING: This will need to be optimized
583
int initial = size / 2;
584         int offset = initial;
585         int index = 0;
586
587         int max1 = w - size;
588         int max2 = max1 + h - size;
589         int max3 = max2 + w - size;
590         int max4 = max3 + h;
591
592         while (offset < max4) {
593             int next = offset + pattern[index];
594
595             if (index % 2 == 0) {
596                 if (offset < max1) {
597                     // top
598
g.fillRect(offset, 0, Math.min(next, w) - offset, size);
599                     offset = Math.min(next, max1);
600                 }
601                 if (offset != next && offset < max2) {
602                     // right
603
g.fillRect(w - size, offset - max1, size,
604                                Math.min(max2, next) - offset);
605                     offset = Math.min(max2, next);
606                 }
607                 if (offset != next && offset < max3) {
608                     // bottom
609
int x2 = w - (offset - max2);
610                     int temp = Math.min(next - offset, max3 - offset);
611                     g.fillRect(x2 - temp, h - size, temp, size);
612                     offset += temp;
613                 }
614                 if (offset != next) {
615                     int y2 = h - (offset - max3);
616                     int temp = Math.min(next - offset, max4 - offset);
617                     g.fillRect(0, y2 - temp, size, temp);
618                     offset += temp;
619                 }
620             }
621             else {
622                 offset = next;
623             }
624             index = (index + 1) % pattern.length;
625         }
626
627         g.translate(-x, -y);
628     }
629
630     Color getFocusColor(SynthContext context, int state) {
631         return ((GTKStyle)context.getStyle()).getGTKColor(
632                context.getComponent(), context.getRegion(), state,
633                ColorType.FOREGROUND);
634     }
635
636     public void paintHandle(SynthContext context, Graphics g, int paintState,
637                             int shadowType, String JavaDoc info, int x, int y,
638                             int w, int h, int orientation) {
639         paintBox(context, g, paintState, shadowType, info, x, y, w, h);
640
641         g.translate(x, y);
642
643         Region region = context.getRegion();
644         GTKStyle style = (GTKStyle)context.getStyle();
645         JComponent component = context.getComponent();
646         Color lightColor = style.getGTKColor(component, region, paintState,
647                                           GTKColorType.LIGHT);
648         Color darkColor = style.getGTKColor(component, region, paintState,
649                                           GTKColorType.DARK);
650         Color blackColor = style.getGTKColor(component, region, paintState,
651                                           GTKColorType.BLACK);
652         int startX;
653         int startY;
654         if (info == "paned") {
655             JSplitPane splitPane = (JSplitPane)component;
656
657             int markerLength = 7 * 3 + 6 * 2; // 7 markers width of 3
658
// + 6 spacers width of 2.
659
if (orientation == GTKConstants.HORIZONTAL) {
660                 // Components are above/below.
661
startX = w / 2 - markerLength / 2;
662                 startY = splitPane.getDividerSize() / 2 - 1;
663             } else {
664                 // Components are left/right
665
startX = splitPane.getDividerSize() / 2 - 1;
666                 startY = h / 2 - markerLength / 2;
667             }
668
669             for (int i = 0; i < 7; i++) {
670                 // Add length of one marker and spacing.
671
if (orientation == GTKConstants.HORIZONTAL) {
672                     startX += 5;
673                 } else {
674                     startY += 5;
675                 }
676                 g.setColor(lightColor);
677                 g.fillRect(startX, startY, 2, 2);
678                 g.setColor(blackColor);
679                 g.fillRect(startX + 1, startY + 1, 2, 2);
680                 g.setColor(darkColor);
681                 g.fillRect(startX + 1, startY + 1, 1, 1);
682             }
683         } else if (info == "handlebox") {
684             JToolBar tb = (JToolBar)component;
685             startX = 2;
686             startY = 2;
687             if (tb.getOrientation() == JToolBar.HORIZONTAL) {
688                 while (startY < h) {
689                     g.setColor(darkColor);
690                     g.fillRect(startX + 1, startY + 1, 2, 2);
691                     g.fillRect(startX + 4, startY + 2, 2, 2);
692                     g.setColor(lightColor);
693                     g.fillRect(startX, startY, 2, 2);
694                     g.fillRect(startX + 3, startY + 1, 2, 2);
695                     startY += 3;
696                 }
697             } else {
698                 boolean toggle = true;
699                 while (startX < w) {
700                     g.setColor(darkColor);
701                     g.fillRect(startX + 1, startY + 1, 2, 2);
702                     g.fillRect(startX + 1, startY + 4, 2, 2);
703                     g.setColor(lightColor);
704                     g.fillRect(startX, startY, 2, 2);
705                     g.fillRect(startX, startY + 3, 2, 2);
706                     startX += 3;
707                     if (toggle) {
708                         startY++;
709                         toggle = false;
710                     } else {
711                         startY--;
712                         toggle = true;
713                     }
714                 }
715             }
716             // Fix up the shadow.
717
paintShadow(context, g, paintState, shadowType, info, 0, 0, w, h);
718         }
719         g.translate(-x, -y);
720     }
721
722     public void paintOption(SynthContext context, Graphics g, int paintState,
723                             int shadowType, String JavaDoc info, int x, int y,
724                             int w, int h) {
725         g.translate(x, y);
726
727         int componentState = context.getComponentState();
728         int centerY = h / 2 - 1;
729         JComponent component = context.getComponent();
730         Region region = context.getRegion();
731         GTKStyle style = (GTKStyle)context.getStyle();
732
733         // We need to use the state from the context since the state passed in may have been modified.
734
int optionState = ((componentState & SynthConstants.PRESSED) == 0) ?
735                 SynthConstants.ENABLED : SynthConstants.PRESSED;
736         g.setColor(style.getGTKColor(component, region, optionState, GTKColorType.TEXT_BACKGROUND));
737
738         if (region != Region.RADIO_BUTTON_MENU_ITEM) {
739             // Paint center portion.
740
g.fillRect(3, centerY - 3, 9, 9);
741             g.fillRect(13, centerY - 1, 1, 4);
742             g.fillRect(12, centerY + 3, 1, 2);
743             g.fillRect(9, centerY + 6, 2, 1);
744             g.fillRect(5, centerY + 7, 4, 1);
745     
746             // Paint dark shadow.
747
g.setColor(style.getGTKColor(component, region, SynthConstants.ENABLED,
748                                          GTKColorType.DARK));
749             g.fillRect(5, centerY - 5, 4, 1);
750             g.fillRect(3, centerY - 4, 3, 1);
751             g.fillRect(2, centerY - 3, 1, 2);
752             g.fillRect(1, centerY - 1, 1, 4);
753             g.fillRect(2, centerY + 4, 1, 1);
754             g.fillRect(3, centerY + 5, 2, 1);
755             g.fillRect(5, centerY + 6, 4, 1);
756             g.fillRect(9, centerY + 5, 2, 1);
757             g.fillRect(11, centerY + 3, 1, 2);
758             g.fillRect(12, centerY - 1, 1, 4);
759             g.fillRect(11, centerY - 3, 1, 2);
760             g.fillRect(10, centerY - 4, 1, 1);
761             
762             // Paint black shadow.
763
g.setColor(style.getGTKColor(component, region, paintState,
764                                          GTKColorType.BLACK));
765             g.fillRect(5, centerY - 4, 5, 1);
766             g.fillRect(10, centerY - 3, 1, 1);
767             g.fillRect(3, centerY - 3, 2, 1);
768             g.fillRect(3, centerY - 2, 1, 1);
769             g.fillRect(2, centerY - 1, 1, 5);
770             g.fillRect(3, centerY + 4, 1, 1);
771         }
772
773         // Fill in center if selected.
774
if ((componentState & SynthConstants.SELECTED) != 0) {
775             g.setColor(style.getGTKColor(component, region, SynthConstants.ENABLED,
776                                          GTKColorType.FOREGROUND));
777             g.fillRect(5, centerY, 5, 3);
778             g.fillRect(6, centerY - 1, 3, 1);
779             g.fillRect(6, centerY + 3, 3, 1);
780         }
781         g.translate(-x, -y);
782     }
783
784     public void paintShadow(SynthContext context, Graphics g, int state,
785                             int shadowType, String JavaDoc info, int x, int y,
786                             int w, int h) {
787         if (info == "buttondefault") {
788             // YES, this appears to be special cased.
789
g.setColor(((GTKStyle)context.getStyle()).getGTKColor(
790                            context.getComponent(), context.getRegion(),
791                            state, GTKColorType.BLACK));
792             g.drawRect(x, y, w - 1, h - 1);
793             return;
794         }
795         GTKStyle style = (GTKStyle)context.getStyle();
796         JComponent c = context.getComponent();
797         Region region = context.getRegion();
798         int xThickness = style.getXThickness();
799         int yThickness = style.getYThickness();
800
801         if (info == "trough" ||
802                 (info == "spinbutton" && c instanceof JButton)) {
803             // YES, this appears to be special cased.
804
xThickness = yThickness = 1;
805         }
806
807         if (xThickness < 0 && yThickness < 0) {
808             // nothing to paint.
809
return;
810         }
811         Color upperLeft = null, innerLeft = null, bottomRight = null,
812               innerRight = null;
813         switch (shadowType) {
814         case GTKConstants.SHADOW_IN:
815             if (info == "spinbutton" && c instanceof JButton) {
816                 upperLeft = style.getGTKColor(c, region, state,
817                         GTKColorType.DARK);
818                 innerRight = upperLeft;
819                 bottomRight = style.getGTKColor(c, region, state,
820                         GTKColorType.BLACK);
821             } else {
822                 upperLeft = style.getGTKColor(c, region, state,
823                         GTKColorType.DARK);
824                 innerLeft = style.getGTKColor(c, region, state,
825                         GTKColorType.BLACK);
826                 bottomRight = style.getGTKColor(c, region, state,
827                         GTKColorType.LIGHT);
828                 innerRight = style.getGTKColor(c, region, state,
829                         GTKColorType.BACKGROUND);
830             }
831             break;
832         case GTKConstants.SHADOW_OUT:
833             upperLeft = style.getGTKColor(c, region, state,GTKColorType.LIGHT);
834             innerLeft = style.getGTKColor(c, region, state,
835                                           GTKColorType.BACKGROUND);
836             bottomRight = style.getGTKColor(c, region, state,
837                                             GTKColorType.BLACK);
838             innerRight = style.getGTKColor(c, region, state,GTKColorType.DARK);
839             break;
840         default:
841             assert true : "Unknown shadow type!";
842         }
843         if (info == "spinbutton" && c instanceof JButton) {
844             _paintSpinButtonShadow(g, x, y, w, h, xThickness, yThickness,
845                     upperLeft, innerLeft, bottomRight, innerRight);
846         } else {
847             _paintShadow(g, x, y, w, h, xThickness, yThickness,
848                     upperLeft, innerLeft, bottomRight, innerRight);
849         }
850     }
851
852     void _paintShadow(Graphics g, int x, int y, int w, int h,
853                  int xThickness, int yThickness, Color upperLeft,
854                  Color innerLeft, Color bottomRight, Color innerRight) {
855         g.translate(x, y);
856         // left/top
857
g.setColor(upperLeft);
858         if (xThickness > 0) {
859             g.drawLine(0, 0, 0, h - 1);
860         }
861         if (yThickness > 0) {
862             g.drawLine(1, 0, w - 1, 0);
863         }
864
865         // Inner left/top
866
if (xThickness > 1 || yThickness > 1) {
867             g.setColor(innerLeft);
868             if (xThickness > 1) {
869                 g.drawLine(1, 1, 1, h - 2);
870             }
871             if (yThickness > 1) {
872                 g.drawLine(2, 1, w - 2, 1);
873             }
874         }
875
876         // Bottom/right
877
g.setColor(bottomRight);
878         if (xThickness > 0) {
879             g.drawLine(w - 1, 1, w - 1, h - 1);
880         }
881         if (yThickness > 0) {
882             g.drawLine(1, h - 1, w - 2, h - 1);
883         }
884         // Inner bottom/right
885
if (xThickness > 1 || yThickness > 1) {
886             g.setColor(innerRight);
887             if (xThickness > 1) {
888                 g.drawLine(w - 2, 2, w - 2, h - 2);
889             }
890             if (yThickness > 1) {
891                 g.drawLine(2, h - 2, w - 3, h - 2);
892             }
893         }
894         g.translate(-x, -y);
895     }
896
897     void _paintSpinButtonShadow(Graphics g, int x, int y,
898                  int w, int h, int xThickness, int yThickness, Color upperLeft,
899                  Color innerLeft, Color bottomRight, Color innerRight) {
900         g.translate(x, y);
901
902         // top
903
g.setColor(upperLeft);
904         g.drawLine(0, 0, w - 1, 0);
905         // bottom
906
g.setColor(innerRight);
907         g.drawLine(0, h - 1, w - 1, h - 1);
908         // left/right
909
g.setColor(bottomRight);
910         g.drawLine(0, 0, 0, h - 1);
911         g.drawLine(w - 1, 0, w - 1, h - 1);
912
913         g.translate(-x, -y);
914     }
915
916     public void paintExpander(SynthContext context, Graphics g, int state,
917                               int expanderStyle, String JavaDoc info, int x,
918                               int y, int w, int h) {
919         if (expanderStyle == GTKConstants.EXPANDER_COLLAPSED) {
920             if (state != SynthConstants.MOUSE_OVER) {
921                 paintHollowTriangle(context, g, state,
922                     x, y, Math.min(w,h),
923                     adjustAxisForComponentOrientation(
924                     context.getComponent(), SwingConstants.EAST));
925             } else {
926             }
927         }
928         else {
929             if (state != SynthConstants.MOUSE_OVER) {
930                 paintHollowTriangle(context, g, state,
931                     x, y, Math.min(w,h), SwingConstants.SOUTH);
932             } else {
933             }
934         }
935     }
936
937     private void paintHollowTriangle(SynthContext ss, Graphics g, int state,
938                                      int x, int y, int size, int direction) {
939         GTKStyle style = (GTKStyle)ss.getStyle();
940         int mid, height, thick, i, j, up, down;
941
942         mid = (size / 2);
943         height = size / 2 + 1;
944         thick = Math.max(1, size / 7);
945
946         g.translate(x, y);
947
948         Color foreground = style.getGTKColor(ss.getComponent(), ss.getRegion(),
949                 state, GTKColorType.FOREGROUND);
950         Color background = style.getGTKColor(ss.getComponent(), ss.getRegion(),
951                 state, GTKColorType.BACKGROUND);
952
953         switch(direction) {
954         case SwingConstants.NORTH:
955             j = size / 2 + height / 2 - 1;
956             // Fill in the background of the expander icon.
957
g.setColor(background);
958             for(i = height - 1; i > 0; i--) {
959                 g.drawLine(mid - i + 1, j, mid + i - 1, j);
960                 j--;
961             }
962
963             g.setColor(foreground);
964             j = size / 2 + height / 2 - 1;
965             down = thick - 1;
966             // Draw the base of the triangle.
967
for (up = 0; up < thick; up++) {
968                 g.drawLine(0 - down, j - up, size + down, j - up);
969                 down--;
970             }
971             j--;
972
973             // Paint sides of triangle.
974
for(i = height - 1; i > 0; i--) {
975                 for (up = 0; up < thick; up++ ) {
976                     g.drawLine(mid - i + 1 - up, j, mid - i + 1 - up, j);
977                     g.drawLine(mid + i - 1 + up, j, mid + i - 1 + up, j);
978                 }
979                 j--;
980             }
981
982             // Paint remainder of tip if necessary.
983
if (thick > 1) {
984                 for (up = thick - 2; up >= 0; up--) {
985                     g.drawLine(mid - up, j, mid + up, j);
986                     j--;
987                 }
988             }
989             break;
990         case SwingConstants.SOUTH:
991             j = size / 2 - height / 2 - 1;
992             // Fill in the background of the expander icon.
993
g.setColor(background);
994             for (i = height - 1; i > 0; i--) {
995                 g.drawLine(mid - i + 1, j, mid + i - 1, j);
996                 j++;
997             }
998
999             g.setColor(foreground);
1000            j = size / 2 - height / 2 - 1;
1001            down = thick - 1;
1002            // Draw the base of the triangle.
1003
for (up = 0; up < thick; up++) {
1004                g.drawLine(0 - down, j + up, size + down, j + up);
1005                down--;
1006            }
1007            j++;
1008
1009            // Paint sides of triangle.
1010
for (i = height - 1; i > 0; i--) {
1011                for (up = 0; up < thick; up++ ) {
1012                    g.drawLine(mid - i + 1 - up, j, mid - i + 1 - up, j);
1013                    g.drawLine(mid + i - 1 + up, j, mid + i - 1 + up, j);
1014                }
1015                j++;
1016            }
1017
1018            // Paint remainder of tip if necessary.
1019
if (thick > 1) {
1020                for (up = thick - 2; up >= 0; up--) {
1021                    g.drawLine(mid - up, j, mid + up, j);
1022                    j++;
1023                }
1024            }
1025            break;
1026        case SwingConstants.WEST:
1027            i = size / 2 + height / 2 - 1;
1028            // Fill in the background of the expander icon.
1029
g.setColor(background);
1030            for (j = height - 1; j > 0; j--) {
1031                g.drawLine(i, mid - j + 1, i, mid + j - 1);
1032                i--;
1033            }
1034
1035            g.setColor(foreground);
1036            i = size / 2 + height / 2 - 1;
1037            down = thick - 1;
1038            // Draw the base of the triangle.
1039
for (up = 0; up < thick; up++) {
1040                g.drawLine(i - up, 0 - down, i - up, size + down);
1041                down--;
1042            }
1043            i--;
1044
1045            // Paint sides of triangle.
1046
for (j = height - 1; j > 0; j--) {
1047                for (up = 0; up < thick; up++) {
1048                    g.drawLine(i, mid - j + 1 - up, i, mid - j + 1 - up);
1049                    g.drawLine(i, mid + j - 1 + up, i, mid + j - 1 + up);
1050                }
1051                i--;
1052            }
1053
1054            // Paint remainder of tip if necessary.
1055
if (thick > 1) {
1056                for (up = thick - 2; up >= 0; up--) {
1057                    g.drawLine(i, mid - up, i, mid + up);
1058                    i--;
1059                }
1060            }
1061            break;
1062        case SwingConstants.EAST:
1063            i = size / 2 - height / 2 - 1;
1064
1065            // Fill in the background of the expander icon.
1066
g.setColor(background);
1067            for (j = height - 1; j > 0; j--) {
1068                g.drawLine(i, mid - j + 1, i, mid + j - 1);
1069                i++;
1070            }
1071
1072            g.setColor(foreground);
1073            i = size / 2 - height / 2 - 1;
1074            down = thick - 1;
1075            // Draw the base of the triangle.
1076
for (up = 0; up < thick; up++) {
1077                g.drawLine(i + up, 0 - down, i + up, size + down);
1078                down--;
1079            }
1080            i++;
1081
1082            // Paint sides of triangle.
1083
for (j = height - 1; j > 0; j--) {
1084                for (up = 0; up < thick; up++) {
1085                    g.drawLine(i, mid - j + 1 - up, i, mid - j + 1 - up);
1086                    g.drawLine(i, mid + j - 1 + up, i, mid + j - 1 + up);
1087                }
1088                i++;
1089            }
1090
1091            // Paint remainder of tip if necessary.
1092
if (thick > 1) {
1093                for (up = thick - 2; up >= 0; up--) {
1094                    g.drawLine(i, mid - up, i, mid + up);
1095                    i++;
1096                }
1097            }
1098            break;
1099        }
1100        g.translate(-x, -y);
1101    }
1102
1103    public void paintSlider(SynthContext context, Graphics g, int state,
1104                           int shadowType, String JavaDoc info,
1105                           int x, int y, int w, int h, int orientation) {
1106        paintBox(context, g, state, shadowType, info, x, y, w, h);
1107        if (context.getRegion() == Region.SLIDER_THUMB) {
1108            if (orientation == GTKConstants.HORIZONTAL) {
1109                paintVline(context, g, state, info, x + w / 2 - 1, y + 2, 2,
1110                           h - 4);
1111            } else {
1112                paintHline(context, g, state, info, x + 2, y + h / 2 - 1,
1113                           w - 4, 2);
1114            }
1115        }
1116    }
1117
1118    public void paintHline(SynthContext context, Graphics g, int state,
1119                           String JavaDoc info, int x, int y, int w, int h) {
1120        g.translate(x, y);
1121
1122        GTKStyle style = (GTKStyle)context.getStyle();
1123        int centerY = h / 2;
1124        if (h == 2) {
1125            centerY = 0;
1126        }
1127        g.setColor(style.getGTKColor(context.getComponent(),
1128                                     context.getRegion(), state,
1129                                     GTKColorType.DARK));
1130        g.drawLine(0, centerY, w - 2, centerY);
1131        g.setColor(style.getGTKColor(context.getComponent(),
1132                                     context.getRegion(), state,
1133                                     GTKColorType.LIGHT));
1134        g.drawLine(0, centerY + 1, w - 1, centerY + 1);
1135        g.drawLine(w - 1, centerY, w - 1, centerY + 1);
1136
1137        g.translate(-x, -y);
1138    }
1139    public void paintVline(SynthContext context, Graphics g, int state,
1140                           String JavaDoc info, int x, int y, int w, int h) {
1141        g.translate(x, y);
1142
1143        GTKStyle style = (GTKStyle)context.getStyle();
1144        int centerX = w / 2;
1145        g.setColor(style.getGTKColor(context.getComponent(),
1146                                     context.getRegion(),
1147                                     state, GTKColorType.DARK));
1148        g.drawLine(centerX, 0, centerX, h - 2);
1149        g.drawLine(centerX, 0, centerX + 1, 0);
1150        g.setColor(style.getGTKColor(context.getComponent(),
1151                                     context.getRegion(),
1152                                     state, GTKColorType.LIGHT));
1153        g.drawLine(centerX + 1, 1, centerX + 1, h - 1);
1154        g.drawLine(centerX, h - 1, centerX + 1, h - 1);
1155
1156        g.translate(-x, -y);
1157    }
1158
1159
1160    /**
1161     * If necessary paints the background.
1162     */

1163    void paintBackground(SynthContext context, Graphics g, int state,
1164                                 Color color, int x, int y, int w, int h) {
1165        GTKStyle style = (GTKStyle)context.getStyle();
1166
1167        if (style.fillBackground(context, state)) {
1168            g.setColor(color);
1169            g.fillRect(x, y, w, h);
1170        }
1171        else {
1172            Image image = style.getBackgroundImage(context, state);
1173            int iW;
1174            int iH;
1175
1176            if (image != null && (iW = image.getWidth(null)) > 0 &&
1177                                 (iH = image.getHeight(null)) > 0) {
1178                // Map the x/y location to originate from the origin of the
1179
// window
1180
int x2 = x;
1181                int y2 = y;
1182                Component parent = context.getComponent().getParent();
1183
1184                while (parent != null && !(parent instanceof Window) &&
1185                       !(parent instanceof java.applet.Applet JavaDoc)) {
1186                    Component nextParent = parent.getParent();
1187
1188                    if (parent instanceof JRootPane &&
1189                               !(nextParent instanceof Window) &&
1190                               !(nextParent instanceof java.applet.Applet JavaDoc)) {
1191                        x2 += parent.getX();
1192                        y2 += parent.getY();
1193                    }
1194                    parent = nextParent;
1195                }
1196                x2 = x2 % iW;
1197                y2 = y2 % iH;
1198
1199                Rectangle clip = g.getClipRect();
1200                int cx1 = clip.x;
1201                int cy1 = clip.y;
1202                int cx2 = cx1 + clip.width;
1203                int cy2 = cy1 + clip.height;
1204
1205                int lastIY = y2;
1206                for (int yCounter = y, maxY = y + h; yCounter < maxY;
1207                         yCounter += (iH - lastIY), lastIY = 0) {
1208                    int lastIX = x2;
1209                    for (int xCounter = x, maxX = x + w; xCounter < maxX;
1210                             xCounter += (iW - lastIX), lastIX = 0) {
1211                        int dx2 = Math.min(maxX, xCounter + iW - lastIX);
1212                        int dy2 = Math.min(maxY, yCounter + iH - lastIY);
1213                        if (dx2 > cx1 && dy2 > cy1 && cx2 > xCounter &&
1214                                         cy2 > yCounter) {
1215                            g.drawImage(image, xCounter, yCounter, dx2, dy2,
1216                                    lastIX, lastIY, lastIX + dx2 - xCounter,
1217                                    lastIY + dy2 - yCounter, null);
1218                        }
1219                    }
1220                }
1221            }
1222        }
1223    }
1224}
1225
Popular Tags