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