1 /* 2 * Copyright 2016-2022 Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * John Scipione, jscipione@gmail.com 7 * 8 * Based on ColorWhichItem by DarkWyrm (bpmagic@columbus.rr.com) 9 */ 10 11 12 #include "ColorItem.h" 13 14 #include <math.h> 15 16 #include <ControlLook.h> 17 #include <View.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 // #pragma mark - ColorItem 28 29 30 ColorItem::ColorItem(const char* string, rgb_color color) 31 : 32 BStringItem(string, 0, false), 33 fColor(color) 34 { 35 } 36 37 38 void 39 ColorItem::DrawItem(BView* owner, BRect frame, bool complete) 40 { 41 rgb_color highColor = owner->HighColor(); 42 rgb_color lowColor = owner->LowColor(); 43 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); 50 51 owner->FillRect(frame); 52 } 53 54 float spacer = ceilf(be_control_look->DefaultItemSpacing() / 2); 55 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); 60 61 // draw the colored box 62 owner->SetHighColor(fColor); 63 owner->FillRect(colorRect); 64 65 // draw the border 66 owner->SetHighUIColor(B_CONTROL_BORDER_COLOR); 67 owner->StrokeRect(colorRect); 68 69 // draw the string 70 owner->MovePenTo(colorRect.right + spacer, frame.top + BaselineOffset()); 71 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); 83 } 84 85 owner->DrawString(Text()); 86 87 owner->SetHighColor(highColor); 88 owner->SetLowColor(lowColor); 89 } 90