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