1 /*
2 * Copyright 2006, Haiku.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 * Stephan Aßmus <superstippi@gmx.de>
7 */
8
9 #include "BoolValueView.h"
10
11 #include <stdio.h>
12
13 #include "ui_defines.h"
14
15 // constructor
BoolValueView(BoolProperty * property)16 BoolValueView::BoolValueView(BoolProperty* property)
17 : PropertyEditorView(),
18 fProperty(property),
19 fCheckBoxRect(0.0, 0.0, -1.0, -1.0),
20 fEnabled(true)
21 {
22 }
23
24 // destructor
~BoolValueView()25 BoolValueView::~BoolValueView()
26 {
27 }
28
29 // Draw
30 void
Draw(BRect updateRect)31 BoolValueView::Draw(BRect updateRect)
32 {
33 BRect b(Bounds());
34 // focus indication
35 if (IsFocus()) {
36 SetHighColor(ui_color(B_KEYBOARD_NAVIGATION_COLOR));
37 StrokeRect(b);
38 b.InsetBy(1.0, 1.0);
39 }
40 // background
41 FillRect(b, B_SOLID_LOW);
42
43 // checkmark box
44 rgb_color crossOutline = kBlack;
45 rgb_color crossColor = ui_color(B_KEYBOARD_NAVIGATION_COLOR);
46
47 if (!fEnabled) {
48 crossOutline = tint_color(crossOutline, B_LIGHTEN_2_TINT);
49 crossColor = tint_color(crossColor, B_LIGHTEN_2_TINT);
50 }
51
52 SetHighColor(crossOutline);
53 b = fCheckBoxRect;
54 StrokeRect(b);
55
56 // checkmark
57 if (fProperty && fProperty->Value()) {
58 SetHighColor(crossColor);
59 b.InsetBy(3.0, 3.0);
60 SetPenSize(2.0);
61 StrokeLine(b.LeftTop(), b.RightBottom());
62 StrokeLine(b.LeftBottom(), b.RightTop());
63 }
64 }
65
66 // FrameResized
67 void
FrameResized(float width,float height)68 BoolValueView::FrameResized(float width, float height)
69 {
70 float radius = ceilf((height - 6.0) / 2.0);
71 float centerX = floorf(Bounds().left + width / 2.0);
72 float centerY = floorf(Bounds().top + height / 2.0);
73 fCheckBoxRect.Set(centerX - radius, centerY - radius,
74 centerX + radius, centerY + radius);
75 }
76
77 // MakeFocus
78 void
MakeFocus(bool focused)79 BoolValueView::MakeFocus(bool focused)
80 {
81 PropertyEditorView::MakeFocus(focused);
82 Invalidate();
83 }
84
85 // MouseDown
86 void
MouseDown(BPoint where)87 BoolValueView::MouseDown(BPoint where)
88 {
89 MakeFocus(true);
90 if (fCheckBoxRect.Contains(where)) {
91 _ToggleValue();
92 }
93 // NOTE: careful, when this function returns, the object might
94 // in fact have been deleted
95 }
96
97 // KeyDown
98 void
KeyDown(const char * bytes,int32 numBytes)99 BoolValueView::KeyDown(const char* bytes, int32 numBytes)
100 {
101 bool handled = true;
102 if (numBytes > 0) {
103 switch (bytes[0]) {
104 case B_RETURN:
105 case B_SPACE:
106 case B_UP_ARROW:
107 case B_DOWN_ARROW:
108 case B_LEFT_ARROW:
109 case B_RIGHT_ARROW:
110 _ToggleValue();
111 break;
112 default:
113 handled = false;
114 break;
115 }
116 }
117 if (!handled)
118 PropertyEditorView::KeyDown(bytes, numBytes);
119 }
120
121 // _ToggleValue
122 void
_ToggleValue()123 BoolValueView::_ToggleValue()
124 {
125 if (!fEnabled)
126 return;
127
128 if (fProperty) {
129 fProperty->SetValue(!fProperty->Value());
130 BRect b(fCheckBoxRect);
131 b.InsetBy(1.0, 1.0);
132 Invalidate(b);
133 ValueChanged();
134 }
135 }
136
137 // SetEnabled
138 void
SetEnabled(bool enabled)139 BoolValueView::SetEnabled(bool enabled)
140 {
141 if (fEnabled != enabled) {
142 fEnabled = enabled;
143 Invalidate();
144 }
145 }
146
147 // AdoptProperty
148 bool
AdoptProperty(Property * property)149 BoolValueView::AdoptProperty(Property* property)
150 {
151 BoolProperty* p = dynamic_cast<BoolProperty*>(property);
152 if (p) {
153 BRect b(fCheckBoxRect);
154 b.InsetBy(1.0, 1.0);
155 Invalidate(b);
156
157 fProperty = p;
158 return true;
159 }
160 return false;
161 }
162
163 // GetProperty
164 Property*
GetProperty() const165 BoolValueView::GetProperty() const
166 {
167 return fProperty;
168 }
169
170