vfs: check userland buffers before reading them.
[haiku.git] / src / add-ons / screen_savers / gravity / ColorItem.cpp
blob586283a655fb1009814758d0615a691dc44e26ba
1 /*
2 * Copyright 2016 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * John Scipione, jscipione@gmail.com
8 * Based on ColorWhichItem by DarkWyrm (bpmagic@columbus.rr.com)
9 */
12 #include "ColorItem.h"
14 #include <math.h>
16 #include <ControlLook.h>
17 #include <View.h>
20 // golden ratio
21 #ifdef M_PHI
22 # undef M_PHI
23 #endif
24 #define M_PHI 1.61803398874989484820
27 // #pragma mark - ColorItem
30 ColorItem::ColorItem(const char* string, rgb_color color)
32 BStringItem(string, 0, false),
33 fColor(color)
38 void
39 ColorItem::DrawItem(BView* owner, BRect frame, bool complete)
41 rgb_color highColor = owner->HighColor();
42 rgb_color lowColor = owner->LowColor();
44 if (IsSelected() || complete) {
45 if (IsSelected()) {
46 owner->SetHighUIColor(B_LIST_SELECTED_BACKGROUND_COLOR);
47 owner->SetLowColor(owner->HighColor());
48 } else
49 owner->SetHighColor(lowColor);
51 owner->FillRect(frame);
54 float spacer = ceilf(be_control_look->DefaultItemSpacing() / 2);
56 BRect colorRect(frame);
57 colorRect.InsetBy(2.0f, 2.0f);
58 colorRect.left += spacer;
59 colorRect.right = colorRect.left + floorf(colorRect.Height() * M_PHI);
61 // draw the colored box
62 owner->SetHighColor(fColor);
63 owner->FillRect(colorRect);
65 // draw the border
66 owner->SetHighUIColor(B_CONTROL_BORDER_COLOR);
67 owner->StrokeRect(colorRect);
69 // draw the string
70 owner->MovePenTo(colorRect.right + spacer, frame.top + BaselineOffset());
72 if (!IsEnabled()) {
73 rgb_color textColor = ui_color(B_LIST_ITEM_TEXT_COLOR);
74 if (textColor.red + textColor.green + textColor.blue > 128 * 3)
75 owner->SetHighColor(tint_color(textColor, B_DARKEN_2_TINT));
76 else
77 owner->SetHighColor(tint_color(textColor, B_LIGHTEN_2_TINT));
78 } else {
79 if (IsSelected())
80 owner->SetHighUIColor(B_LIST_SELECTED_ITEM_TEXT_COLOR);
81 else
82 owner->SetHighUIColor(B_LIST_ITEM_TEXT_COLOR);
85 owner->DrawString(Text());
87 owner->SetHighColor(highColor);
88 owner->SetLowColor(lowColor);
92 void
93 ColorItem::SetColor(rgb_color color)
95 fColor = color;