xref: /haiku/src/preferences/input/InputDeviceView.cpp (revision 8c78892580f132d10e624aef96f835df8d94bf19)
1 /*
2  * Copyright 2019, Haiku, Inc.
3  * Distributed under the terms of the MIT License.
4  *
5  * Author:
6  *		Preetpal Kaur <preetpalok123@gmail.com>
7 */
8 
9 
10 #include "InputDeviceView.h"
11 
12 
13 #include <Catalog.h>
14 #include <DateFormat.h>
15 #include <Input.h>
16 #include <LayoutBuilder.h>
17 #include <ListView.h>
18 #include <Locale.h>
19 #include <ScrollView.h>
20 #include <String.h>
21 #include <StringItem.h>
22 
23 
24 #undef B_TRANSLATION_CONTEXT
25 #define B_TRANSLATION_CONTEXT "DeviceList"
26 
27 
28 InputIcons* DeviceListItemView::sIcons = NULL;
29 
30 
31 DeviceListItemView::DeviceListItemView(BString title, input_type type)
32 	:
33 	BListItem((uint32)0),
34 	fTitle(title),
35 	fInputType(type)
36 {
37 }
38 
39 struct DeviceListItemView::Renderer {
40 	Renderer()
41 		:
42 		fTitle(NULL),
43 		fPrimaryIcon(NULL),
44 		fSelected(false)
45 	{
46 	}
47 
48 	void AddIcon(BBitmap* icon)
49 	{
50 		if (!fPrimaryIcon)
51 			fPrimaryIcon = icon;
52 	}
53 
54 	void SetTitle(const char* title)
55 	{
56 		fTitle = title;
57 	}
58 
59 	void SetSelected(bool selected)
60 	{
61 		fSelected = selected;
62 	}
63 
64 	void Render(BView* onto, BRect frame, bool complete = false)
65 	{
66 		const rgb_color lowColor = onto->LowColor();
67 		const rgb_color highColor = onto->HighColor();
68 
69 		if (fSelected || complete) {
70 			if (fSelected)
71 				onto->SetLowColor(ui_color(B_LIST_SELECTED_BACKGROUND_COLOR));
72 			onto->FillRect(frame, B_SOLID_LOW);
73 		}
74 
75 		BPoint point(frame.left + 4.0f,
76 			frame.top + (frame.Height() - InputIcons::sBounds.Height()) / 2.0f);
77 
78 		BRect iconFrame(InputIcons::IconRectAt(point + BPoint(1, 0)));
79 
80 		onto->SetDrawingMode(B_OP_OVER);
81 		if (fPrimaryIcon) {
82 			onto->DrawBitmap(fPrimaryIcon, iconFrame);
83 			point.x = iconFrame.right + 1;
84 		}
85 
86 		onto->SetDrawingMode(B_OP_COPY);
87 
88 		BFont font = be_plain_font;
89 		font_height	fontInfo;
90 		font.GetHeight(&fontInfo);
91 
92 		onto->SetFont(&font);
93 		onto->MovePenTo(point.x + 8, frame.top
94 			+ fontInfo.ascent + (frame.Height()
95 			- ceilf(fontInfo.ascent + fontInfo.descent)) / 2.0f);
96 		onto->DrawString(fTitle);
97 
98 		onto->SetHighColor(highColor);
99 		onto->SetLowColor(lowColor);
100 	}
101 
102 	float ItemWidth()
103 	{
104 		float width = 4.0f;
105 		width += be_plain_font->StringWidth(fTitle) + 16.0f;
106 		return width;
107 	}
108 private:
109 
110 	BString		fTitle;
111 	BBitmap*	fPrimaryIcon;
112 	bool		fSelected;
113 };
114 
115 
116 void
117 DeviceListItemView::Update(BView* owner, const BFont* font)
118 {
119 	BListItem::Update(owner, font);
120 
121 	float iconHeight = InputIcons::sBounds.Height() + 1;
122 	if ((Height() < iconHeight + kITEM_MARGIN * 2)) {
123 		SetHeight(iconHeight + kITEM_MARGIN * 2);
124 	}
125 
126 	Renderer renderer;
127 	renderer.SetTitle(Label());
128 	renderer.SetTitle(fTitle);
129 	SetRenderParameters(renderer);
130 	SetWidth(renderer.ItemWidth());
131 };
132 
133 
134 void
135 DeviceListItemView::DrawItem(BView* owner, BRect frame, bool complete)
136 {
137 	Renderer renderer;
138 	renderer.SetSelected(IsSelected());
139 	renderer.SetTitle(Label());
140 	SetRenderParameters(renderer);
141 	renderer.Render(owner, frame, complete);
142 };
143 
144 
145 void
146 DeviceListItemView::SetRenderParameters(Renderer& renderer)
147 {
148 	if (fInputType == MOUSE_TYPE)
149 		renderer.AddIcon(&Icons()->mouseIcon);
150 
151 	else if (fInputType == TOUCHPAD_TYPE)
152 		renderer.AddIcon(&Icons()->touchpadIcon);
153 
154 	else if (fInputType == KEYBOARD_TYPE)
155 		renderer.AddIcon(&Icons()->keyboardIcon);
156 }
157