comparison ext/guichan-0.8.1/include/guichan/widgets/button.hpp @ 89:fa33cda75471

* Reverting back to 2543 as requested by sleek
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 19 Jul 2008 11:38:52 +0000
parents 4a0efb7baf70
children
comparison
equal deleted inserted replaced
88:1c2842ebe393 89:fa33cda75471
1 /* _______ __ __ __ ______ __ __ _______ __ __
2 * / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
3 * / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
4 * / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
5 * / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
6 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
7 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
8 *
9 * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
10 *
11 *
12 * Per Larsson a.k.a finalman
13 * Olof Naessén a.k.a jansem/yakslem
14 *
15 * Visit: http://guichan.sourceforge.net
16 *
17 * License: (BSD)
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in
25 * the documentation and/or other materials provided with the
26 * distribution.
27 * 3. Neither the name of Guichan nor the names of its contributors may
28 * be used to endorse or promote products derived from this software
29 * without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
37 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
38 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
39 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 #ifndef GCN_BUTTON_HPP
45 #define GCN_BUTTON_HPP
46
47 #include <string>
48
49 #include "guichan/focuslistener.hpp"
50 #include "guichan/graphics.hpp"
51 #include "guichan/keylistener.hpp"
52 #include "guichan/mouseevent.hpp"
53 #include "guichan/mouselistener.hpp"
54 #include "guichan/platform.hpp"
55 #include "guichan/widget.hpp"
56
57 namespace gcn
58 {
59 /**
60 * An implementation of a regular clickable button. A button is capable of
61 * displaying a caption.
62 *
63 * If a button is clicked an action event will be sent to all action listener's
64 * of the button.
65 *
66 * @see ImageButton
67 */
68 class GCN_CORE_DECLSPEC Button : public Widget,
69 public MouseListener,
70 public KeyListener,
71 public FocusListener
72 {
73 public:
74 /**
75 * Constructor.
76 */
77 Button();
78
79 /**
80 * Constructor. The button will be automatically resized
81 * to fit the caption.
82 *
83 * @param caption The caption of the button.
84 */
85 Button(const std::string& caption);
86
87 /**
88 * Sets the caption of the button. It's advisable to call
89 * adjustSize after setting of the caption to adjust the
90 * button's size to fit the caption.
91 *
92 * @param caption The caption of the button.
93 * @see getCaption, adjustSize
94 */
95 void setCaption(const std::string& caption);
96
97 /**
98 * Gets the caption of the button.
99 *
100 * @return The caption of the button.
101 */
102 const std::string& getCaption() const;
103
104 /**
105 * Sets the alignment of the caption. The alignment is relative
106 * to the center of the button.
107 *
108 * @param alignment The alignment of the caption.
109 * @see getAlignment, Graphics
110 */
111 void setAlignment(Graphics::Alignment alignment);
112
113 /**
114 * Gets the alignment of the caption.
115 *
116 * @return The alignment of the caption.
117 * @see setAlignment, Graphics
118 */
119 Graphics::Alignment getAlignment() const;
120
121 /**
122 * Sets the spacing between the border of the button and its caption.
123 *
124 * @param spacing The default value for spacing is 4 and can be changed
125 * using this method.
126 * @see getSpacing
127 */
128 void setSpacing(unsigned int spacing);
129
130 /**
131 * Gets the spacing between the border of the button and its caption.
132 *
133 * @return spacing.
134 * @see setSpacing
135 */
136 unsigned int getSpacing() const;
137
138 /**
139 * Adjusts the button's size to fit the caption.
140 */
141 void adjustSize();
142
143
144 //Inherited from Widget
145
146 virtual void draw(Graphics* graphics);
147
148
149 // Inherited from FocusListener
150
151 virtual void focusLost(const Event& event);
152
153
154 // Inherited from MouseListener
155
156 virtual void mousePressed(MouseEvent& mouseEvent);
157
158 virtual void mouseReleased(MouseEvent& mouseEvent);
159
160 virtual void mouseEntered(MouseEvent& mouseEvent);
161
162 virtual void mouseExited(MouseEvent& mouseEvent);
163
164 virtual void mouseDragged(MouseEvent& mouseEvent);
165
166
167 // Inherited from KeyListener
168
169 virtual void keyPressed(KeyEvent& keyEvent);
170
171 virtual void keyReleased(KeyEvent& keyEvent);
172
173 protected:
174 /**
175 * Checks if the button is pressed. Convenient method to use
176 * when overloading the draw method of the button.
177 *
178 * @return True if the button is pressed, false otherwise.
179 */
180 bool isPressed() const;
181
182 /**
183 * Holds the caption of the button.
184 */
185 std::string mCaption;
186
187 /**
188 * True if the mouse is ontop of the button, false otherwise.
189 */
190 bool mHasMouse;
191
192 /**
193 * True if a key has been pressed, false otherwise.
194 */
195 bool mKeyPressed;
196
197 /**
198 * True if a mouse has been pressed, false otherwise.
199 */
200 bool mMousePressed;
201
202 /**
203 * Holds the alignment of the caption.
204 */
205 Graphics::Alignment mAlignment;
206
207 /**
208 * Holds the spacing between the border and the caption.
209 */
210 unsigned int mSpacing;
211 };
212 }
213
214 #endif // end GCN_BUTTON_HPP