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 /*
45  * For comments regarding functions please see the header file.
46  */
47 
48 #include "guichan/widgets/checkbox.hpp"
49 
50 #include "guichan/font.hpp"
51 #include "guichan/graphics.hpp"
52 #include "guichan/key.hpp"
53 #include "guichan/mouseinput.hpp"
54 
55 namespace gcn
56 {
57 
CheckBox()58     CheckBox::CheckBox()
59     {
60         setSelected(false);
61 
62         setFocusable(true);
63         addMouseListener(this);
64         addKeyListener(this);
65     }
66 
CheckBox(const std::string & caption,bool selected)67     CheckBox::CheckBox(const std::string &caption, bool selected)
68     {
69         setCaption(caption);
70         setSelected(selected);
71 
72         setFocusable(true);
73         addMouseListener(this);
74         addKeyListener(this);
75 
76         adjustSize();
77     }
78 
draw(Graphics * graphics)79     void CheckBox::draw(Graphics* graphics)
80     {
81         drawBox(graphics);
82 
83         graphics->setFont(getFont());
84         graphics->setColor(getForegroundColor());
85 
86         const int h = getHeight() + getHeight() / 2;
87 
88         graphics->drawText(getCaption(), h - 2, 0);
89     }
90 
drawBox(Graphics * graphics)91     void CheckBox::drawBox(Graphics *graphics)
92     {
93         const int h = getHeight() - 2;
94         const int alpha = getBaseColor().a;
95         Color faceColor = getBaseColor();
96         faceColor.a = alpha;
97         Color highlightColor = faceColor + 0x303030;
98         highlightColor.a = alpha;
99         Color shadowColor = faceColor - 0x303030;
100         shadowColor.a = alpha;
101 
102         graphics->setColor(shadowColor);
103         graphics->drawLine(1, 1, h, 1);
104         graphics->drawLine(1, 1, 1, h);
105 
106         graphics->setColor(highlightColor);
107         graphics->drawLine(h, 1, h, h);
108         graphics->drawLine(1, h, h - 1, h);
109 
110         graphics->setColor(getBackgroundColor());
111         graphics->fillRectangle(Rectangle(2, 2, h - 2, h - 2));
112 
113         graphics->setColor(getForegroundColor());
114 
115         if (isFocused())
116         {
117             graphics->drawRectangle(Rectangle(0, 0, h + 2, h + 2));
118         }
119 
120         if (mSelected)
121         {
122             graphics->drawLine(3, 5, 3, h - 2);
123             graphics->drawLine(4, 5, 4, h - 2);
124 
125             graphics->drawLine(5, h - 3, h - 2, 4);
126             graphics->drawLine(5, h - 4, h - 4, 5);
127         }
128     }
129 
isSelected() const130     bool CheckBox::isSelected() const
131     {
132         return mSelected;
133     }
134 
setSelected(bool selected)135     void CheckBox::setSelected(bool selected)
136     {
137         mSelected = selected;
138     }
139 
getCaption() const140     const std::string &CheckBox::getCaption() const
141     {
142         return mCaption;
143     }
144 
setCaption(const std::string & caption)145     void CheckBox::setCaption(const std::string& caption)
146     {
147         mCaption = caption;
148     }
149 
keyPressed(KeyEvent & keyEvent)150     void CheckBox::keyPressed(KeyEvent& keyEvent)
151     {
152         Key key = keyEvent.getKey();
153 
154         if (key.getValue() == Key::ENTER ||
155             key.getValue() == Key::SPACE)
156         {
157             toggleSelected();
158             keyEvent.consume();
159         }
160     }
161 
mouseClicked(MouseEvent & mouseEvent)162     void CheckBox::mouseClicked(MouseEvent& mouseEvent)
163     {
164         if (mouseEvent.getButton() == MouseEvent::LEFT)
165         {
166             toggleSelected();
167         }
168     }
169 
mouseDragged(MouseEvent & mouseEvent)170     void CheckBox::mouseDragged(MouseEvent& mouseEvent)
171     {
172         mouseEvent.consume();
173     }
174 
adjustSize()175     void CheckBox::adjustSize()
176     {
177         int height = getFont()->getHeight();
178 
179         setHeight(height);
180         setWidth(getFont()->getWidth(mCaption) + height + height / 2);
181     }
182 
toggleSelected()183     void CheckBox::toggleSelected()
184     {
185         mSelected = !mSelected;
186         distributeActionEvent();
187     }
188 }
189 
190