comparison org.eclipse.draw2d/src/org/eclipse/draw2d/ToggleButton.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
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 org.eclipse.draw2d.ToggleButton;
14
15 import java.lang.all;
16
17
18
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.draw2d.geometry.Insets;
21 import org.eclipse.draw2d.geometry.Rectangle;
22 import org.eclipse.draw2d.Toggle;
23 import org.eclipse.draw2d.IFigure;
24 import org.eclipse.draw2d.Graphics;
25 import org.eclipse.draw2d.Label;
26 import org.eclipse.draw2d.ColorConstants;
27
28 /**
29 * A Toggle that appears like a 3-dimensional button.
30 */
31 public class ToggleButton
32 : Toggle
33 {
34
35 /** This ToggleButton's Label **/
36 protected Label label = null;
37
38 /**
39 * Constructs a new ToggleButton with no initial contents.
40 */
41 public this() {
42 setStyle(STYLE_BUTTON | STYLE_TOGGLE);
43 }
44
45 /**
46 * Constructs a ToggleButton with the passed IFigure as its contents.
47 *
48 * @param contents the contents of the toggle button
49 * @since 2.0
50 */
51 public this(IFigure contents) {
52 super(contents, STYLE_BUTTON | STYLE_TOGGLE);
53 }
54
55 /**
56 * Constructs a ToggleButton with the passed string as its text.
57 *
58 * @param text the text to be displayed on the button
59 * @since 2.0
60 */
61 public this(String text) {
62 this(text, null);
63 }
64
65 /**
66 * Constructs a ToggleButton with a Label containing the passed text and icon.
67 *
68 * @param text the text
69 * @param normalIcon the icon
70 * @since 2.0
71 */
72 public this(String text, Image normalIcon) {
73 super(new Label(text, normalIcon), STYLE_BUTTON | STYLE_TOGGLE);
74 }
75
76 /**
77 * @see org.eclipse.draw2d.Figure#paintFigure(Graphics)
78 */
79 protected void paintFigure(Graphics graphics) {
80 if (isSelected() && isOpaque()) {
81 fillCheckeredRectangle(graphics);
82 } else {
83 super.paintFigure(graphics);
84 }
85 }
86
87 /**
88 * Draws a checkered pattern to emulate a toggle button that is in the selected state.
89 * @param graphics The Graphics object used to paint
90 */
91 protected void fillCheckeredRectangle(Graphics graphics) {
92 graphics.setBackgroundColor(ColorConstants.button);
93 graphics.setForegroundColor(ColorConstants.buttonLightest);
94 Rectangle rect = getClientArea(Rectangle.SINGLETON).crop(new Insets(1, 1, 0, 0));
95 graphics.fillRectangle(rect.x, rect.y, rect.width, rect.height);
96
97 graphics.clipRect(rect);
98 graphics.translate(rect.x, rect.y);
99 int n = rect.width + rect.height;
100 for (int i = 1; i < n; i += 2) {
101 graphics.drawLine(0, i, i, 0);
102 }
103 graphics.restoreState();
104 }
105
106 }