tcp: Fix 64 bit build with debugging features enabled.
[haiku.git] / src / kits / interface / StringItem.cpp
blob9d4c14b67520b046f15bd2b697eef193d9a16a28
1 /*
2 * Copyright 2001-2009, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Ulrich Wimboeck
7 * Marc Flerackers (mflerackers@androme.be)
8 * Rene Gollent
9 */
12 #include <StringItem.h>
14 #include <stdlib.h>
15 #include <string.h>
17 #include <ControlLook.h>
18 #include <Message.h>
19 #include <View.h>
22 BStringItem::BStringItem(const char* text, uint32 level, bool expanded)
24 BListItem(level, expanded),
25 fText(NULL),
26 fBaselineOffset(0)
28 SetText(text);
32 BStringItem::BStringItem(BMessage* archive)
34 BListItem(archive),
35 fText(NULL),
36 fBaselineOffset(0)
38 const char* string;
39 if (archive->FindString("_label", &string) == B_OK)
40 SetText(string);
44 BStringItem::~BStringItem()
46 free(fText);
50 BArchivable*
51 BStringItem::Instantiate(BMessage* archive)
53 if (validate_instantiation(archive, "BStringItem"))
54 return new BStringItem(archive);
56 return NULL;
60 status_t
61 BStringItem::Archive(BMessage* archive, bool deep) const
63 status_t status = BListItem::Archive(archive);
65 if (status == B_OK && fText != NULL)
66 status = archive->AddString("_label", fText);
68 return status;
72 void
73 BStringItem::DrawItem(BView* owner, BRect frame, bool complete)
75 if (fText == NULL)
76 return;
78 rgb_color lowColor = owner->LowColor();
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();
87 owner->SetLowColor(color);
88 owner->FillRect(frame, B_SOLID_LOW);
89 } else
90 owner->SetLowColor(owner->ViewColor());
92 owner->MovePenTo(frame.left + be_control_look->DefaultLabelSpacing(),
93 frame.top + fBaselineOffset);
95 owner->DrawString(fText);
97 owner->SetLowColor(lowColor);
101 void
102 BStringItem::SetText(const char* text)
104 free(fText);
105 fText = NULL;
107 if (text)
108 fText = strdup(text);
112 const char*
113 BStringItem::Text() const
115 return fText;
119 void
120 BStringItem::Update(BView* owner, const BFont* font)
122 if (fText != NULL) {
123 SetWidth(font->StringWidth(fText)
124 + be_control_look->DefaultLabelSpacing());
127 font_height fheight;
128 font->GetHeight(&fheight);
130 fBaselineOffset = 2 + ceilf(fheight.ascent + fheight.leading / 2);
132 SetHeight(ceilf(fheight.ascent) + ceilf(fheight.descent)
133 + ceilf(fheight.leading) + 4);
137 status_t
138 BStringItem::Perform(perform_code d, void* arg)
140 return BListItem::Perform(d, arg);
144 float
145 BStringItem::BaselineOffset() const
147 return fBaselineOffset;
151 void BStringItem::_ReservedStringItem1() {}
152 void BStringItem::_ReservedStringItem2() {}
155 BStringItem::BStringItem(const BStringItem &)
160 BStringItem &
161 BStringItem::operator=(const BStringItem &)
163 return *this;