75
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2005 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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.ui.internal.forms.widgets.ImageSegment;
|
|
14
|
|
15 import dwtx.ui.internal.forms.widgets.ObjectSegment;
|
|
16 import dwtx.ui.internal.forms.widgets.SelectionData;
|
|
17
|
|
18 import dwt.graphics.Color;
|
|
19 import dwt.graphics.GC;
|
|
20 import dwt.graphics.Image;
|
|
21 import dwt.graphics.Point;
|
|
22 import dwt.graphics.Rectangle;
|
|
23
|
|
24 import dwt.dwthelper.utils;
|
|
25 import tango.util.collection.HashMap;
|
|
26
|
|
27 /**
|
|
28 * @version 1.0
|
|
29 * @author
|
|
30 */
|
|
31 public class ImageSegment : ObjectSegment {
|
|
32 alias HashMap!(String,Object) Hashtable;
|
|
33 public static const String SEL_IMAGE_PREFIX = "isel."; //$NON-NLS-1$
|
|
34
|
|
35 public Image getImage(Hashtable objectTable) {
|
|
36 return getImage(getObjectId(), objectTable);
|
|
37 }
|
|
38
|
|
39 private Image getImage(String key, Hashtable objectTable) {
|
|
40 if (key is null)
|
|
41 return null;
|
|
42 Object obj = objectTable.get(key);
|
|
43 if (obj is null)
|
|
44 return null;
|
|
45 if ( auto image = cast(Image)obj )
|
|
46 return image;
|
|
47 return null;
|
|
48 }
|
|
49
|
|
50 private Image getSelectedImage(Hashtable objectTable, SelectionData selData) {
|
|
51 String key = SEL_IMAGE_PREFIX + getObjectId();
|
|
52 Image image = getImage(key, objectTable);
|
|
53 if (image is null) {
|
|
54 image = FormUtil.createAlphaMashImage(selData.display, getImage(objectTable));
|
|
55 if (image !is null)
|
|
56 objectTable.put(key, image);
|
|
57 }
|
|
58 return image;
|
|
59 }
|
|
60 /*
|
|
61 private String getSelectedImageId() {
|
|
62 if (getObjectId() is null)
|
|
63 return null;
|
|
64 return SEL_IMAGE_PREFIX + getObjectId();
|
|
65 }
|
|
66 */
|
|
67
|
|
68 public void paint(GC gc, bool hover, Hashtable resourceTable, bool selected, SelectionData selData, Rectangle repaintRegion) {
|
|
69 Image image = getImage(resourceTable);
|
|
70 int iwidth = 0;
|
|
71 int iheight = 0;
|
|
72 if (image !is null) {
|
|
73 Rectangle rect = image.getBounds();
|
|
74 iwidth = rect.width + (isSelectable()?2:0);
|
|
75 iheight = rect.height + (isSelectable()?2:0);
|
|
76 } else
|
|
77 return;
|
|
78 Rectangle bounds = getBounds();
|
|
79 int ix = bounds.x+(isSelectable()?1:0);
|
|
80 int iy = bounds.y+(isSelectable()?1:0);
|
|
81
|
|
82 if (selData !is null) {
|
|
83 int leftOffset = selData.getLeftOffset(bounds.height);
|
|
84 int rightOffset = selData.getRightOffset(bounds.height);
|
|
85 bool firstRow = selData.isFirstSelectionRow(bounds.y,
|
|
86 bounds.height);
|
|
87 bool lastRow = selData.isLastSelectionRow(bounds.y,
|
|
88 bounds.height);
|
|
89 bool selectedRow = selData
|
|
90 .isSelectedRow(bounds.y, bounds.height);
|
|
91 if (selectedRow) {
|
|
92 if ((firstRow && leftOffset > ix) ||
|
|
93 (lastRow && rightOffset < ix + iwidth/2)) {
|
|
94 drawClipImage(gc, image, ix, iy, repaintRegion);
|
|
95 }
|
|
96 else {
|
|
97 Color savedBg = gc.getBackground();
|
|
98 gc.setBackground(selData.bg);
|
|
99 int sx = ix;
|
|
100 int sy = iy;
|
|
101 if (repaintRegion !is null) {
|
|
102 sx -= repaintRegion.x;
|
|
103 sy -= repaintRegion.y;
|
|
104 }
|
|
105 gc.fillRectangle(sx, sy, iwidth, iheight);
|
|
106 Image selImage = getSelectedImage(resourceTable, selData);
|
|
107 gc.drawImage(selImage, sx, sy);
|
|
108 gc.setBackground(savedBg);
|
|
109 }
|
|
110 }
|
|
111 else
|
|
112 drawClipImage(gc, image, ix, iy, repaintRegion);
|
|
113 } else
|
|
114 drawClipImage(gc, image, ix, iy, repaintRegion);
|
|
115 if (selected) {
|
|
116 int fx = bounds.x;
|
|
117 int fy = bounds.y;
|
|
118 if (repaintRegion !is null) {
|
|
119 fx -= repaintRegion.x;
|
|
120 fy -= repaintRegion.y;
|
|
121 }
|
|
122 Color fg = gc.getForeground();
|
|
123 gc.setForeground(gc.getBackground());
|
|
124 // Clean up to avoid canceling out XOR if it is already
|
|
125 // selected.
|
|
126 gc.drawRectangle(bounds.x, bounds.y, bounds.width - 1,
|
|
127 bounds.height - 1);
|
|
128 gc.setForeground(fg);
|
|
129 gc.drawFocus(fx, fy, bounds.width, bounds.height);
|
|
130 }
|
|
131 }
|
|
132 private void drawClipImage(GC gc, Image image, int ix, int iy, Rectangle repaintRegion) {
|
|
133 if (repaintRegion !is null) {
|
|
134 ix -= repaintRegion.x;
|
|
135 iy -= repaintRegion.y;
|
|
136 }
|
|
137 gc.drawImage(image, ix, iy);
|
|
138 }
|
|
139
|
|
140 protected Point getObjectSize(Hashtable resourceTable, int wHint) {
|
|
141 Image image = getImage(resourceTable);
|
|
142 if (image is null)
|
|
143 return new Point(0, 0);
|
|
144 Rectangle ibounds = image.getBounds();
|
|
145 return new Point(ibounds.width, ibounds.height);
|
|
146 }
|
|
147 }
|