xref: /haiku/src/preferences/input/InputDeviceView.cpp (revision 4a55cc230cf7566cadcbb23b1928eefff8aea9a2)
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) +
107 			(fPrimaryIcon != NULL ? fPrimaryIcon->Bounds().Width() : 16.0f);
108 		return width;
109 	}
110 
111 private:
112 
113 	BString		fTitle;
114 	BBitmap*	fPrimaryIcon;
115 	bool		fSelected;
116 };
117 
118 
119 void
120 DeviceListItemView::Update(BView* owner, const BFont* font)
121 {
122 	BListItem::Update(owner, font);
123 
124 	float iconHeight = InputIcons::sBounds.Height() + 1;
125 	if ((Height() < iconHeight + kITEM_MARGIN * 2))
126 		SetHeight(iconHeight + kITEM_MARGIN * 2);
127 
128 	Renderer renderer;
129 	renderer.SetTitle(Label());
130 	renderer.SetTitle(fTitle);
131 	SetRenderParameters(renderer);
132 	SetWidth(renderer.ItemWidth());
133 }
134 
135 
136 void
137 DeviceListItemView::DrawItem(BView* owner, BRect frame, bool complete)
138 {
139 	Renderer renderer;
140 	renderer.SetSelected(IsSelected());
141 	renderer.SetTitle(Label());
142 	SetRenderParameters(renderer);
143 	renderer.Render(owner, frame, complete);
144 }
145 
146 
147 void
148 DeviceListItemView::SetRenderParameters(Renderer& renderer)
149 {
150 	if (Icons() != NULL) {
151 		if (fInputType == MOUSE_TYPE)
152 			renderer.AddIcon(&Icons()->mouseIcon);
153 		else if (fInputType == TOUCHPAD_TYPE)
154 			renderer.AddIcon(&Icons()->touchpadIcon);
155 		else if (fInputType == KEYBOARD_TYPE)
156 			renderer.AddIcon(&Icons()->keyboardIcon);
157 	}
158 }
159