1 /* 2 * Copyright 2001-2005, Haiku Inc. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Frans van Nispen (xlr8@tref.nl) 7 */ 8 9 /** BStringView draw a non-editable text string */ 10 11 12 #include <Message.h> 13 #include <StringView.h> 14 #include <View.h> 15 #include <Window.h> 16 17 #include <stdio.h> 18 #include <stdlib.h> 19 #include <string.h> 20 21 22 BStringView::BStringView(BRect frame, const char* name, const char* text, 23 uint32 resizeMask, uint32 flags) 24 : BView(frame, name, resizeMask, flags) 25 { 26 fText = text ? strdup(text) : NULL; 27 fAlign = B_ALIGN_LEFT; 28 } 29 30 31 BStringView::BStringView(BMessage* data) 32 : BView(data) 33 { 34 int32 align; 35 if (data->FindInt32("_align", &align) == B_OK) 36 fAlign = (alignment)align; 37 else 38 fAlign = B_ALIGN_LEFT; 39 40 const char* text; 41 if (data->FindString("_text", &text) != B_OK) 42 text = NULL; 43 44 SetText(text); 45 } 46 47 48 BArchivable* 49 BStringView::Instantiate(BMessage* data) 50 { 51 if (!validate_instantiation(data, "BStringView")) 52 return NULL; 53 54 return new BStringView(data); 55 } 56 57 58 status_t 59 BStringView::Archive(BMessage* data, bool deep) const 60 { 61 BView::Archive(data, deep); 62 63 if (fText) 64 data->AddString("_text", fText); 65 66 data->AddInt32("_align", fAlign); 67 68 return B_OK; 69 } 70 71 72 BStringView::~BStringView() 73 { 74 free(fText); 75 } 76 77 78 void 79 BStringView::SetText(const char* text) 80 { 81 free(fText); 82 fText = text ? strdup(text) : NULL; 83 Invalidate(); 84 } 85 86 87 const char* 88 BStringView::Text() const 89 { 90 return fText; 91 } 92 93 94 void 95 BStringView::SetAlignment(alignment flag) 96 { 97 fAlign = flag; 98 Invalidate(); 99 } 100 101 102 alignment 103 BStringView::Alignment() const 104 { 105 return fAlign; 106 } 107 108 109 void 110 BStringView::AttachedToWindow() 111 { 112 if (Parent()) 113 SetViewColor(Parent()->ViewColor()); 114 } 115 116 117 void 118 BStringView::Draw(BRect bounds) 119 { 120 if (!fText) 121 return; 122 123 SetLowColor(ViewColor()); 124 125 font_height fontHeight; 126 GetFontHeight(&fontHeight); 127 128 float y = Bounds().bottom - ceil(fontHeight.descent); 129 float x; 130 switch (fAlign) { 131 case B_ALIGN_RIGHT: 132 x = Bounds().Width() - StringWidth(fText) - 2.0f; 133 break; 134 135 case B_ALIGN_CENTER: 136 x = (Bounds().Width() - StringWidth(fText)) / 2.0f; 137 break; 138 139 default: 140 x = 2.0f; 141 break; 142 } 143 144 DrawString(fText, BPoint(x, y)); 145 } 146 147 148 void 149 BStringView::ResizeToPreferred() 150 { 151 float width, height; 152 GetPreferredSize(&width, &height); 153 154 // Resize the width only for B_ALIGN_LEFT (if its large enough already, that is) 155 if (Bounds().Width() > width && Alignment() != B_ALIGN_LEFT) 156 width = Bounds().Width(); 157 158 BView::ResizeTo(width, height); 159 } 160 161 162 void 163 BStringView::GetPreferredSize(float* _width, float* _height) 164 { 165 if (!fText) { 166 BView::GetPreferredSize(_width, _height); 167 return; 168 } 169 170 if (_width) 171 *_width = 4.0f + ceil(StringWidth(fText)); 172 173 if (_height) { 174 font_height fontHeight; 175 GetFontHeight(&fontHeight); 176 *_height = ceil(fontHeight.ascent + fontHeight.descent + fontHeight.leading) + 2.0f; 177 } 178 } 179 180 181 void 182 BStringView::MessageReceived(BMessage* message) 183 { 184 BView::MessageReceived(message); 185 } 186 187 188 void 189 BStringView::MouseDown(BPoint point) 190 { 191 BView::MouseDown(point); 192 } 193 194 195 void 196 BStringView::MouseUp(BPoint point) 197 { 198 BView::MouseUp(point); 199 } 200 201 202 void 203 BStringView::MouseMoved(BPoint point, uint32 transit, const BMessage* msg) 204 { 205 BView::MouseMoved(point, transit, msg); 206 } 207 208 209 void 210 BStringView::DetachedFromWindow() 211 { 212 BView::DetachedFromWindow(); 213 } 214 215 216 void 217 BStringView::FrameMoved(BPoint newPosition) 218 { 219 BView::FrameMoved(newPosition); 220 } 221 222 223 void 224 BStringView::FrameResized(float newWidth, float newHeight) 225 { 226 BView::FrameResized(newWidth, newHeight); 227 } 228 229 230 BHandler* 231 BStringView::ResolveSpecifier(BMessage* msg, int32 index, 232 BMessage* specifier, int32 form, const char* property) 233 { 234 return NULL; 235 } 236 237 238 void 239 BStringView::MakeFocus(bool state) 240 { 241 BView::MakeFocus(state); 242 } 243 244 245 void 246 BStringView::AllAttached() 247 { 248 BView::AllAttached(); 249 } 250 251 252 void 253 BStringView::AllDetached() 254 { 255 BView::AllDetached(); 256 } 257 258 259 status_t 260 BStringView::GetSupportedSuites(BMessage* data) 261 { 262 return B_OK; 263 } 264 265 266 status_t 267 BStringView::Perform(perform_code d, void* arg) 268 { 269 return B_ERROR; 270 } 271 272 273 void BStringView::_ReservedStringView1() {} 274 void BStringView::_ReservedStringView2() {} 275 void BStringView::_ReservedStringView3() {} 276 277 278 BStringView& 279 BStringView::operator=(const BStringView&) 280 { 281 // Assignment not allowed (private) 282 return *this; 283 } 284