1 /* 2 * Copyright 2001-2009, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Ulrich Wimboeck 7 * Marc Flerackers (mflerackers@androme.be) 8 * Rene Gollent 9 */ 10 11 12 #include <StringItem.h> 13 14 #include <stdlib.h> 15 #include <string.h> 16 17 #include <ControlLook.h> 18 #include <Message.h> 19 #include <View.h> 20 21 22 BStringItem::BStringItem(const char* text, uint32 level, bool expanded) 23 : BListItem(level, expanded), 24 fText(NULL), 25 fBaselineOffset(0) 26 { 27 SetText(text); 28 } 29 30 31 BStringItem::BStringItem(BMessage* archive) 32 : BListItem(archive), 33 fText(NULL), 34 fBaselineOffset(0) 35 { 36 const char* string; 37 if (archive->FindString("_label", &string) == B_OK) 38 SetText(string); 39 } 40 41 42 BStringItem::~BStringItem() 43 { 44 free(fText); 45 } 46 47 48 BArchivable* 49 BStringItem::Instantiate(BMessage* archive) 50 { 51 if (validate_instantiation(archive, "BStringItem")) 52 return new BStringItem(archive); 53 54 return NULL; 55 } 56 57 58 status_t 59 BStringItem::Archive(BMessage *archive, bool deep) const 60 { 61 status_t status = BListItem::Archive(archive); 62 63 if (status == B_OK && fText != NULL) 64 status = archive->AddString("_label", fText); 65 66 return status; 67 } 68 69 70 void 71 BStringItem::DrawItem(BView *owner, BRect frame, bool complete) 72 { 73 if (fText == NULL) 74 return; 75 76 rgb_color highColor = owner->HighColor(); 77 rgb_color lowColor = owner->LowColor(); 78 79 if (IsSelected() || complete) { 80 if (IsSelected()) { 81 owner->SetHighColor(ui_color(B_LIST_SELECTED_BACKGROUND_COLOR)); 82 owner->SetLowColor(owner->HighColor()); 83 } else 84 owner->SetHighColor(lowColor); 85 86 owner->FillRect(frame); 87 } 88 89 owner->MovePenTo(frame.left + be_control_look->DefaultLabelSpacing(), 90 frame.top + fBaselineOffset); 91 92 if (!IsEnabled()) { 93 rgb_color textColor = ui_color(B_LIST_ITEM_TEXT_COLOR); 94 if (textColor.red + textColor.green + textColor.blue > 128 * 3) 95 owner->SetHighColor(tint_color(textColor, B_DARKEN_2_TINT)); 96 else 97 owner->SetHighColor(tint_color(textColor, B_LIGHTEN_2_TINT)); 98 } else { 99 if (IsSelected()) 100 owner->SetHighColor(ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR)); 101 else 102 owner->SetHighColor(ui_color(B_LIST_ITEM_TEXT_COLOR)); 103 } 104 105 owner->DrawString(fText); 106 107 owner->SetHighColor(highColor); 108 owner->SetLowColor(lowColor); 109 } 110 111 112 void 113 BStringItem::SetText(const char *text) 114 { 115 free(fText); 116 fText = NULL; 117 118 if (text) 119 fText = strdup(text); 120 } 121 122 123 const char* 124 BStringItem::Text() const 125 { 126 return fText; 127 } 128 129 130 void 131 BStringItem::Update(BView *owner, const BFont *font) 132 { 133 if (fText) { 134 SetWidth(font->StringWidth(fText) 135 + be_control_look->DefaultLabelSpacing()); 136 } 137 138 font_height fheight; 139 font->GetHeight(&fheight); 140 141 fBaselineOffset = 2 + ceilf(fheight.ascent + fheight.leading / 2); 142 143 SetHeight(ceilf(fheight.ascent) + ceilf(fheight.descent) 144 + ceilf(fheight.leading) + 4); 145 } 146 147 148 status_t 149 BStringItem::Perform(perform_code d, void *arg) 150 { 151 return BListItem::Perform(d, arg); 152 } 153 154 155 float 156 BStringItem::BaselineOffset() const 157 { 158 return fBaselineOffset; 159 } 160 161 162 void BStringItem::_ReservedStringItem1() {} 163 void BStringItem::_ReservedStringItem2() {} 164 165 166 BStringItem::BStringItem(const BStringItem &) 167 { 168 } 169 170 171 BStringItem & 172 BStringItem::operator=(const BStringItem &) 173 { 174 return *this; 175 } 176