xref: /haiku/src/preferences/input/InputDeviceView.cpp (revision cbe0a0c436162d78cc3f92a305b64918c839d079)
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,
94 			frame.top + fontInfo.ascent
95 				+ (frame.Height() - ceilf(fontInfo.ascent + fontInfo.descent))
96 					/ 2.0f);
97 		onto->DrawString(fTitle);
98 
99 		onto->SetHighColor(highColor);
100 		onto->SetLowColor(lowColor);
101 	}
102 
103 	float ItemWidth()
104 	{
105 		float width = 4.0f;
106 		width += be_plain_font->StringWidth(fTitle) + 16.0f;
107 		return width;
108 	}
109 
110 private:
111 
112 	BString		fTitle;
113 	BBitmap*	fPrimaryIcon;
114 	bool		fSelected;
115 };
116 
117 
118 void
119 DeviceListItemView::Update(BView* owner, const BFont* font)
120 {
121 	BListItem::Update(owner, font);
122 
123 	float iconHeight = InputIcons::sBounds.Height() + 1;
124 	if ((Height() < iconHeight + kITEM_MARGIN * 2))
125 		SetHeight(iconHeight + kITEM_MARGIN * 2);
126 
127 	Renderer renderer;
128 	renderer.SetTitle(Label());
129 	renderer.SetTitle(fTitle);
130 	SetRenderParameters(renderer);
131 	SetWidth(renderer.ItemWidth());
132 };
133 
134 
135 void
136 DeviceListItemView::DrawItem(BView* owner, BRect frame, bool complete)
137 {
138 	Renderer renderer;
139 	renderer.SetSelected(IsSelected());
140 	renderer.SetTitle(Label());
141 	SetRenderParameters(renderer);
142 	renderer.Render(owner, frame, complete);
143 };
144 
145 
146 void
147 DeviceListItemView::SetRenderParameters(Renderer& renderer)
148 {
149 	if (fInputType == MOUSE_TYPE)
150 		renderer.AddIcon(&Icons()->mouseIcon);
151 
152 	else if (fInputType == TOUCHPAD_TYPE)
153 		renderer.AddIcon(&Icons()->touchpadIcon);
154 
155 	else if (fInputType == KEYBOARD_TYPE)
156 		renderer.AddIcon(&Icons()->keyboardIcon);
157 }
158