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