1 /**
2 * File name: RkButton.cpp
3 * Project: Redkite (A small GUI toolkit)
4 *
5 * Copyright (C) 2020 Iurie Nistor <http://geontime.com>
6 *
7 * This file is part of Redkite.
8 *
9 * Redkite is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24 #include "RkButton.h"
25 #include "RkButtonImpl.h"
26 #include "RkPainter.h"
27 #include "RkEvent.h"
28
RkButton(RkWidget * parent)29 RkButton::RkButton(RkWidget *parent)
30 : RkWidget(parent, std::make_unique<RkButtonImpl>(this, parent))
31 , impl_ptr{static_cast<RkButtonImpl*>(o_ptr.get())}
32 {
33 if (parent)
34 setBackgroundColor(parent->background());
35 }
36
setText(const std::string & text)37 void RkButton::setText(const std::string &text)
38 {
39 impl_ptr->setText(text);
40 update();
41 }
42
text() const43 std::string RkButton::text() const
44 {
45 return impl_ptr->text();
46 }
47
setImage(const RkImage & img,RkButton::State state)48 void RkButton::setImage(const RkImage &img, RkButton::State state)
49 {
50 impl_ptr->setImage(img, state);
51 update();
52 }
53
isPressed() const54 bool RkButton::isPressed() const
55 {
56 return impl_ptr->isPressed();
57 }
58
setPressed(bool pressed)59 void RkButton::setPressed(bool pressed)
60 {
61 if (isPressed() != pressed) {
62 impl_ptr->setPressed(pressed);
63 update();
64 }
65 }
66
setCheckable(bool b)67 void RkButton::setCheckable(bool b)
68 {
69 if (isCheckable() != b) {
70 setType(b ? ButtonType::ButtonCheckable : ButtonType::ButtonUncheckable);
71 update();
72 }
73 }
74
isCheckable() const75 bool RkButton::isCheckable() const
76 {
77 return type() == ButtonType::ButtonCheckable;
78 }
79
type() const80 RkButton::ButtonType RkButton::type() const
81 {
82 return impl_ptr->type();
83 }
84
setType(RkButton::ButtonType type)85 void RkButton::setType(RkButton::ButtonType type)
86 {
87 if (impl_ptr->type() != type) {
88 impl_ptr->setType(type);
89 update();
90 }
91 }
92
mouseButtonPressEvent(RkMouseEvent * event)93 void RkButton::mouseButtonPressEvent(RkMouseEvent *event)
94 {
95 RK_UNUSED(event);
96 if (type() == ButtonType::ButtonCheckable) {
97 setPressed(!isPressed());
98 action toggled(isPressed());
99 } else if ((type() == ButtonType::ButtonUncheckable || type() == ButtonType::ButtonPush)
100 && !isPressed()) {
101 setPressed(true);
102 action toggled(true);
103 }
104 action pressed();
105 }
106
mouseButtonReleaseEvent(RkMouseEvent * event)107 void RkButton::mouseButtonReleaseEvent(RkMouseEvent *event)
108 {
109 if (type() == ButtonType::ButtonPush) {
110 setPressed(false);
111 action toggled(false);
112 }
113 action released();
114 }
115
hoverEvent(RkHoverEvent * event)116 void RkButton::hoverEvent(RkHoverEvent *event)
117 {
118 impl_ptr->setEmphasize(event->isHover());
119 update();
120 }
121
paintEvent(RkPaintEvent * event)122 void RkButton::paintEvent(RkPaintEvent *event)
123 {
124 RK_UNUSED(event);
125 RkPainter painter(this);
126 painter.fillRect(rect(), background());
127 impl_ptr->drawButton(painter);
128 }
129