1 /* 2 * Copyright 2013, Stephan Aßmus <superstippi@gmx.de>. 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 6 #include "TextView.h" 7 8 9 TextView::TextView(const char* name) 10 : 11 BView(name, B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE | B_FRAME_EVENTS) 12 { 13 BFont font; 14 GetFont(&font); 15 16 ::ParagraphStyle style; 17 style.SetLineSpacing(ceilf(font.Size() * 0.2)); 18 19 SetParagraphStyle(style); 20 } 21 22 23 TextView::~TextView() 24 { 25 } 26 27 28 void 29 TextView::Draw(BRect updateRect) 30 { 31 FillRect(updateRect, B_SOLID_LOW); 32 33 fTextLayout.SetWidth(Bounds().Width()); 34 fTextLayout.Draw(this, B_ORIGIN); 35 } 36 37 38 void 39 TextView::AttachedToWindow() 40 { 41 AdoptParentColors(); 42 } 43 44 45 void 46 TextView::FrameResized(float width, float height) 47 { 48 fTextLayout.SetWidth(width); 49 } 50 51 52 BSize 53 TextView::MinSize() 54 { 55 return BSize(50.0f, 0.0f); 56 } 57 58 59 BSize 60 TextView::MaxSize() 61 { 62 return BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED); 63 } 64 65 66 BSize 67 TextView::PreferredSize() 68 { 69 return BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED); 70 } 71 72 73 bool 74 TextView::HasHeightForWidth() 75 { 76 return true; 77 } 78 79 80 void 81 TextView::GetHeightForWidth(float width, float* min, float* max, 82 float* preferred) 83 { 84 ParagraphLayout layout(fTextLayout); 85 layout.SetWidth(width); 86 87 float height = layout.Height() + 1; 88 89 if (min != NULL) 90 *min = height; 91 if (max != NULL) 92 *max = height; 93 if (preferred != NULL) 94 *preferred = height; 95 } 96 97 98 void 99 TextView::SetText(const BString& text) 100 { 101 fText.Clear(); 102 103 CharacterStyle regularStyle; 104 fText.Append(TextSpan(text, regularStyle)); 105 106 fTextLayout.SetParagraph(fText); 107 108 InvalidateLayout(); 109 Invalidate(); 110 } 111 112 113 void 114 TextView::SetParagraphStyle(const ::ParagraphStyle& style) 115 { 116 fText.SetStyle(style); 117 fTextLayout.SetParagraph(fText); 118 119 InvalidateLayout(); 120 Invalidate(); 121 } 122 123