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 BView::ResizeTo(width, height); 155 } 156 157 158 void 159 BStringView::GetPreferredSize(float* _width, float* _height) 160 { 161 if (!fText) { 162 BView::GetPreferredSize(_width, _height); 163 return; 164 } 165 166 if (_width) 167 *_width = 4.0f + ceil(StringWidth(fText)); 168 169 if (_height) { 170 font_height fontHeight; 171 GetFontHeight(&fontHeight); 172 *_height = ceil(fontHeight.ascent + fontHeight.descent + fontHeight.leading) + 2.0f; 173 } 174 } 175 176 177 void 178 BStringView::MessageReceived(BMessage* message) 179 { 180 BView::MessageReceived(message); 181 } 182 183 184 void 185 BStringView::MouseDown(BPoint point) 186 { 187 BView::MouseDown(point); 188 } 189 190 191 void 192 BStringView::MouseUp(BPoint point) 193 { 194 BView::MouseUp(point); 195 } 196 197 198 void 199 BStringView::MouseMoved(BPoint point, uint32 transit, const BMessage* msg) 200 { 201 BView::MouseMoved(point, transit, msg); 202 } 203 204 205 void 206 BStringView::DetachedFromWindow() 207 { 208 BView::DetachedFromWindow(); 209 } 210 211 212 void 213 BStringView::FrameMoved(BPoint newPosition) 214 { 215 BView::FrameMoved(newPosition); 216 } 217 218 219 void 220 BStringView::FrameResized(float newWidth, float newHeight) 221 { 222 BView::FrameResized(newWidth, newHeight); 223 } 224 225 226 BHandler* 227 BStringView::ResolveSpecifier(BMessage* msg, int32 index, 228 BMessage* specifier, int32 form, const char* property) 229 { 230 return NULL; 231 } 232 233 234 void 235 BStringView::MakeFocus(bool state) 236 { 237 BView::MakeFocus(state); 238 } 239 240 241 void 242 BStringView::AllAttached() 243 { 244 BView::AllAttached(); 245 } 246 247 248 void 249 BStringView::AllDetached() 250 { 251 BView::AllDetached(); 252 } 253 254 255 status_t 256 BStringView::GetSupportedSuites(BMessage* data) 257 { 258 return B_OK; 259 } 260 261 262 status_t 263 BStringView::Perform(perform_code d, void* arg) 264 { 265 return B_ERROR; 266 } 267 268 269 void BStringView::_ReservedStringView1() {} 270 void BStringView::_ReservedStringView2() {} 271 void BStringView::_ReservedStringView3() {} 272 273 274 BStringView& 275 BStringView::operator=(const BStringView&) 276 { 277 // Assignment not allowed (private) 278 return *this; 279 } 280