tab_view_cell.cpp
1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <escher/tab_view_cell.h>
#include <escher/palette.h>
extern "C" {
#include <assert.h>
}
TabViewCell::TabViewCell() :
View(),
m_active(false),
m_selected(false),
m_controller(nullptr)
{
}
void TabViewCell::setNamedController(ViewController * controller) {
m_controller = controller;
markRectAsDirty(bounds());
}
void TabViewCell::setActive(bool active) {
m_active = active;
markRectAsDirty(bounds());
}
void TabViewCell::setSelected(bool selected) {
m_selected = selected;
markRectAsDirty(bounds());
}
KDSize TabViewCell::minimalSizeForOptimalDisplay() const {
return KDText::stringSize(m_controller->title(), KDText::FontSize::Small);
}
void TabViewCell::drawRect(KDContext * ctx, KDRect rect) const {
KDCoordinate height = bounds().height();
KDCoordinate width = bounds().width();
// choose the background color
KDColor text = m_active ? Palette::PurpleBright : KDColorWhite;
KDColor background = m_active ? KDColorWhite : Palette::PurpleBright;
KDColor selection = m_active ? Palette::Select : Palette::SelectDark;
background = m_selected ? selection : background;
// Color the background according to the state of the tab cell
if (m_active || m_selected) {
ctx->fillRect(KDRect(0, 0, width, 1), Palette::PurpleBright);
ctx->fillRect(KDRect(0, 1, width, height-1), background);
} else {
ctx->fillRect(KDRect(0, 0, width, height), background);
}
// Write title
KDSize textSize = KDText::stringSize(m_controller->title(), KDText::FontSize::Small);
KDPoint origin((m_frame.width() - textSize.width())/2, (m_frame.height() - textSize.height())/2);
ctx->drawString(m_controller->title(), origin, KDText::FontSize::Small, text, background);
}
#if ESCHER_VIEW_LOGGING
const char * TabViewCell::className() const {
return "TabViewCell";
}
void TabViewCell::logAttributes(std::ostream &os) const {
View::logAttributes(os);
os << " active=\"" << m_active << "\"";
os << " name=\"" << m_controller->title() << "\"";
}
#endif