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 22BStringItem::BStringItem(const char* text, uint32 level, bool expanded) 23 : 24 BListItem(level, expanded), 25 fText(NULL), 26 fBaselineOffset(0) 27{ 28 SetText(text); 29} 30 31 32BStringItem::BStringItem(BMessage* archive) 33 : 34 BListItem(archive), 35 fText(NULL), 36 fBaselineOffset(0) 37{ 38 const char* string; 39 if (archive->FindString("_label", &string) == B_OK) 40 SetText(string); 41} 42 43 44BStringItem::~BStringItem() 45{ 46 free(fText); 47} 48 49 50BArchivable* 51BStringItem::Instantiate(BMessage* archive) 52{ 53 if (validate_instantiation(archive, "BStringItem")) 54 return new BStringItem(archive); 55 56 return NULL; 57} 58 59 60status_t 61BStringItem::Archive(BMessage* archive, bool deep) const 62{ 63 status_t status = BListItem::Archive(archive); 64 65 if (status == B_OK && fText != NULL) 66 status = archive->AddString("_label", fText); 67 68 return status; 69} 70 71 72void 73BStringItem::DrawItem(BView* owner, BRect frame, bool complete) 74{ 75 if (fText == NULL) 76 return; 77 78 rgb_color lowColor = owner->LowColor(); 79 80 if (IsSelected() || complete) { 81 rgb_color color; 82 if (IsSelected()) 83 color = ui_color(B_LIST_SELECTED_BACKGROUND_COLOR); 84 else 85 color = owner->ViewColor(); 86 87 owner->SetLowColor(color); 88 owner->FillRect(frame, B_SOLID_LOW); 89 } else 90 owner->SetLowColor(owner->ViewColor()); 91 92 owner->MovePenTo(frame.left + be_control_look->DefaultLabelSpacing(), 93 frame.top + fBaselineOffset); 94 95 owner->DrawString(fText); 96 97 owner->SetLowColor(lowColor); 98} 99 100 101void 102BStringItem::SetText(const char* text) 103{ 104 free(fText); 105 fText = NULL; 106 107 if (text) 108 fText = strdup(text); 109} 110 111 112const char* 113BStringItem::Text() const 114{ 115 return fText; 116} 117 118 119void 120BStringItem::Update(BView* owner, const BFont* font) 121{ 122 if (fText != NULL) { 123 SetWidth(font->StringWidth(fText) 124 + be_control_look->DefaultLabelSpacing()); 125 } 126 127 font_height fheight; 128 font->GetHeight(&fheight); 129 130 fBaselineOffset = 2 + ceilf(fheight.ascent + fheight.leading / 2); 131 132 SetHeight(ceilf(fheight.ascent) + ceilf(fheight.descent) 133 + ceilf(fheight.leading) + 4); 134} 135 136 137status_t 138BStringItem::Perform(perform_code d, void* arg) 139{ 140 return BListItem::Perform(d, arg); 141} 142 143 144float 145BStringItem::BaselineOffset() const 146{ 147 return fBaselineOffset; 148} 149 150 151void BStringItem::_ReservedStringItem1() {} 152void BStringItem::_ReservedStringItem2() {} 153 154 155BStringItem::BStringItem(const BStringItem &) 156{ 157} 158 159 160BStringItem & 161BStringItem::operator=(const BStringItem &) 162{ 163 return *this; 164} 165