0
|
1 // Written in the D programming language
|
|
2 // www.digitalmars.com/d/
|
|
3
|
|
4 /*
|
|
5 * The contents of this file are subject to the Mozilla Public License Version
|
|
6 * 1.1 (the "License"); you may not use this file except in compliance with
|
|
7 * the License. You may obtain a copy of the License at
|
|
8 * http://www.mozilla.org/MPL/
|
|
9 *
|
|
10 * Software distributed under the License is distributed on an "AS IS" basis,
|
|
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
12 * for the specific language governing rights and limitations under the
|
|
13 * License.
|
|
14 *
|
|
15 * The Original Code is the Dynamin library.
|
|
16 *
|
|
17 * The Initial Developer of the Original Code is Jordan Miner.
|
|
18 * Portions created by the Initial Developer are Copyright (C) 2006-2009
|
|
19 * the Initial Developer. All Rights Reserved.
|
|
20 *
|
|
21 * Contributor(s):
|
|
22 * Jordan Miner <jminer7@gmail.com>
|
|
23 *
|
|
24 */
|
|
25
|
|
26 module dynamin.gui.button;
|
|
27
|
|
28 import dynamin.all_core;
|
|
29 import dynamin.all_gui;
|
|
30 import dynamin.all_painting;
|
|
31 import tango.io.Stdout;
|
|
32 import dynamin.c.cairo; // TODO: remove
|
|
33
|
|
34 // TODO: maybe change to ControlState and add Disabled ?
|
|
35 enum ButtonState {
|
|
36 Normal, Hot, Pressed
|
|
37 }
|
|
38 // TODO: move to another file
|
|
39 enum TextImageRelation {
|
|
40 Overlay, TextBeforeImage, ImageBeforeText, TextAboveImage, ImageAboveText
|
|
41 }
|
|
42
|
|
43 /**
|
|
44 * A button is a control that can be clicked to perform an action.
|
|
45 *
|
|
46 * The appearance of a button with Windows Classic:
|
|
47 *
|
|
48 * $(IMAGE ../web/example_button.png)
|
|
49 */
|
|
50 class Button : Control {
|
|
51 protected:
|
|
52 ButtonState _state;
|
|
53 override void whenMouseDragged(MouseEventArgs e) {
|
|
54 if(contains(e.x, e.y))
|
|
55 state = ButtonState.Pressed;
|
|
56 else
|
|
57 state = ButtonState.Normal;
|
|
58 }
|
|
59 override void whenMouseEntered(EventArgs e) {
|
|
60 state = ButtonState.Hot;
|
|
61 }
|
|
62 override void whenMouseLeft(EventArgs e) {
|
|
63 state = ButtonState.Normal;
|
|
64 }
|
|
65 override void whenMouseDown(MouseEventArgs e) {
|
|
66 if(e.button == MouseButton.Left) {
|
|
67 state = ButtonState.Pressed;
|
|
68 focus();
|
|
69 }
|
|
70 }
|
|
71 override void whenMouseUp(MouseEventArgs e) {
|
|
72 if(state != ButtonState.Pressed) return;
|
|
73 state = ButtonState.Hot;
|
|
74 clicked(new EventArgs);
|
|
75 }
|
|
76 override void whenKeyDown(KeyEventArgs e) {
|
|
77 if(e.key == Key.Space)
|
|
78 state = ButtonState.Pressed;
|
|
79 }
|
|
80 override void whenKeyUp(KeyEventArgs e) {
|
|
81 if(e.key == Key.Space) {
|
|
82 if(state != ButtonState.Pressed) return;
|
|
83 state = ButtonState.Normal;
|
|
84 clicked(new EventArgs);
|
|
85 }
|
|
86 }
|
|
87 override void whenPainting(PaintingEventArgs e) {
|
|
88 Theme.current.Button_paint(this, e.graphics);
|
|
89 return;
|
|
90 // TODO: move to a theme or delete
|
|
91 with(e.graphics) {
|
|
92 if(_state == ButtonState.Hot)
|
|
93 source = Color(200, 0, 0);
|
|
94 else if(_state == ButtonState.Pressed)
|
|
95 source = Color(110, 0, 0);
|
|
96 else
|
|
97 source = Color(220, 80, 80);
|
|
98 moveTo(3, 0);
|
|
99 lineTo(width-3, 0);
|
|
100 lineTo(width, 3);
|
|
101 lineTo(width, height-3);
|
|
102 lineTo(width-3, height);
|
|
103 lineTo(3, height);
|
|
104 lineTo(0, height-3);
|
|
105 lineTo(0, 3);
|
|
106 closePath();
|
|
107 fill();
|
|
108 auto grad = cairo_pattern_create_linear(0, 0, 0, height/2+3);
|
|
109 cairo_pattern_add_color_stop_rgba(grad, 0, 1, 1, 1, .4);
|
|
110 cairo_pattern_add_color_stop_rgba(grad, 1, 1, 1, 1, .05);
|
|
111 cairo_set_source(handle, grad);
|
|
112 cairo_pattern_destroy(grad);
|
|
113 moveTo(3, 0);
|
|
114 lineTo(width-3, 0);
|
|
115 lineTo(width, 3);
|
|
116 lineTo(width, cast(int)height/2-3);
|
|
117 lineTo(width-3, cast(int)height/2);
|
|
118 lineTo(3, cast(int)height/2);
|
|
119 lineTo(0, cast(int)height/2+3);
|
|
120 lineTo(0, 3);
|
|
121 closePath();
|
|
122 fill();
|
|
123
|
|
124 source = _state == ButtonState.Pressed ? Color.White : Color.Black;
|
|
125 fontSize = 13;
|
|
126 drawText(text, 5, height/2);
|
|
127 }
|
|
128 return;
|
|
129
|
|
130 }
|
|
131 public void paintFore(Graphics g) {
|
|
132 auto extents = g.getTextExtents(text);
|
|
133 g.drawText(text, (width-extents.width)/2, (height-extents.height)/2);
|
|
134 }
|
|
135
|
|
136 public:
|
|
137 /// Override this method in a subclass to handle the Clicked event.
|
|
138 protected void whenClicked(EventArgs e) { }
|
|
139 /// This event occurs after the button has been clicked.
|
|
140 Event!() clicked;
|
|
141
|
|
142 this() {
|
|
143 clicked = new Event!()(&whenClicked);
|
|
144 _focusable = true;
|
|
145 }
|
|
146 this(string text) {
|
|
147 this();
|
|
148 this.text = text;
|
|
149 }
|
|
150 override Size bestSize() { return Theme.current.Button_bestSize(this); }
|
|
151 ButtonState state() { return _state; }
|
|
152 void state(ButtonState s) { _state = s; repaint(); }
|
|
153 }
|