xref: /haiku/src/preferences/appearance/ColorWhichItem.cpp (revision 4c8e85b316c35a9161f5a1c50ad70bc91c83a76f)
1 /*
2  * Copyright 2001-2016 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		DarkWyrm, darkwyrm@earthlink.net
7  *		Rene Gollent, rene@gollent.com
8  *		Ryan Leavengood, leavengood@gmail.com
9  *		John Scipione, jscipione@gmail.com
10  */
11 
12 
13 #include "ColorWhichItem.h"
14 
15 #include <math.h>
16 
17 #include <ControlLook.h>
18 
19 
20 // golden ratio
21 #ifdef M_PHI
22 #	undef M_PHI
23 #endif
24 #define M_PHI 1.61803398874989484820
25 
26 
27 ColorWhichItem::ColorWhichItem(const char* text, color_which which,
28 	rgb_color color)
29 	:
30 	BStringItem(text, 0, false),
31 	fColorWhich(which),
32 	fColor(color)
33 {
34 }
35 
36 
37 void
38 ColorWhichItem::DrawItem(BView* owner, BRect frame, bool complete)
39 {
40 	rgb_color highColor = owner->HighColor();
41 	rgb_color lowColor = owner->LowColor();
42 
43 	if (IsSelected() || complete) {
44 		if (IsSelected()) {
45 			owner->SetHighUIColor(B_LIST_SELECTED_BACKGROUND_COLOR);
46 			owner->SetLowColor(owner->HighColor());
47 		} else
48 			owner->SetHighColor(lowColor);
49 
50 		owner->FillRect(frame);
51 	}
52 
53 	float spacer = ceilf(be_control_look->DefaultItemSpacing() / 2);
54 
55 	BRect colorRect(frame);
56 	colorRect.InsetBy(2.0f, 2.0f);
57 	colorRect.left += spacer;
58 	colorRect.right = colorRect.left + floorf(colorRect.Height() * M_PHI);
59 	owner->SetHighColor(fColor);
60 	owner->FillRect(colorRect);
61 	owner->SetHighUIColor(B_CONTROL_BORDER_COLOR);
62 	owner->StrokeRect(colorRect);
63 
64 	owner->MovePenTo(colorRect.right + spacer, frame.top + BaselineOffset());
65 
66 	if (!IsEnabled()) {
67 		rgb_color textColor = ui_color(B_LIST_ITEM_TEXT_COLOR);
68 		if (textColor.red + textColor.green + textColor.blue > 128 * 3)
69 			owner->SetHighColor(tint_color(textColor, B_DARKEN_2_TINT));
70 		else
71 			owner->SetHighColor(tint_color(textColor, B_LIGHTEN_2_TINT));
72 	} else {
73 		if (IsSelected())
74 			owner->SetHighUIColor(B_LIST_SELECTED_ITEM_TEXT_COLOR);
75 		else
76 			owner->SetHighUIColor(B_LIST_ITEM_TEXT_COLOR);
77 	}
78 
79 	owner->DrawString(Text());
80 
81 	owner->SetHighColor(highColor);
82 	owner->SetLowColor(lowColor);
83 }
84