1 /* 2 * Copyright 2004-2011, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Andrew McCall <mccall@@digitalparadise.co.uk> 7 * Mike Berg <mike@berg-net.us> 8 * Julun <host.haiku@gmx.de> 9 * Hamish Morrison <hamish@lavabit.com> 10 */ 11 12 #include "TZDisplay.h" 13 14 #include <stdio.h> 15 16 #include <LayoutUtils.h> 17 18 19 20 TTZDisplay::TTZDisplay(const char* name, const char* label) 21 : 22 BView(name, B_WILL_DRAW), 23 fLabel(label), 24 fText(""), 25 fTime("") 26 { 27 } 28 29 30 TTZDisplay::~TTZDisplay() 31 { 32 } 33 34 35 void 36 TTZDisplay::AttachedToWindow() 37 { 38 if (Parent()) 39 SetViewColor(Parent()->ViewColor()); 40 } 41 42 43 void 44 TTZDisplay::ResizeToPreferred() 45 { 46 BSize size = _CalcPrefSize(); 47 ResizeTo(size.width, size.height); 48 } 49 50 51 void 52 TTZDisplay::Draw(BRect) 53 { 54 SetLowColor(ViewColor()); 55 56 BRect bounds = Bounds(); 57 FillRect(Bounds(), B_SOLID_LOW); 58 59 font_height height; 60 GetFontHeight(&height); 61 float fontHeight = ceilf(height.descent + height.ascent + 62 height.leading); 63 64 BPoint pt(bounds.left, ceilf(bounds.top + height.ascent)); 65 DrawString(fLabel.String(), pt); 66 67 pt.y += fontHeight; 68 DrawString(fText.String(), pt); 69 70 pt.y -= fontHeight; 71 pt.x = bounds.right - StringWidth(fTime.String()); 72 DrawString(fTime.String(), pt); 73 } 74 75 76 const char* 77 TTZDisplay::Label() const 78 { 79 return fLabel.String(); 80 } 81 82 83 void 84 TTZDisplay::SetLabel(const char* label) 85 { 86 fLabel.SetTo(label); 87 Invalidate(); 88 InvalidateLayout(); 89 } 90 91 92 const char* 93 TTZDisplay::Text() const 94 { 95 return fText.String(); 96 } 97 98 99 void 100 TTZDisplay::SetText(const char* text) 101 { 102 fText.SetTo(text); 103 Invalidate(); 104 InvalidateLayout(); 105 } 106 107 108 const char* 109 TTZDisplay::Time() const 110 { 111 return fTime.String(); 112 } 113 114 115 void 116 TTZDisplay::SetTime(const char* time) 117 { 118 fTime.SetTo(time); 119 Invalidate(); 120 InvalidateLayout(); 121 } 122 123 124 BSize 125 TTZDisplay::MaxSize() 126 { 127 BSize size = _CalcPrefSize(); 128 size.width = B_SIZE_UNLIMITED; 129 130 return BLayoutUtils::ComposeSize(ExplicitMaxSize(), 131 size); 132 } 133 134 135 BSize 136 TTZDisplay::MinSize() 137 { 138 return BLayoutUtils::ComposeSize(ExplicitMinSize(), 139 _CalcPrefSize()); 140 } 141 142 143 BSize 144 TTZDisplay::PreferredSize() 145 { 146 return BLayoutUtils::ComposeSize(ExplicitPreferredSize(), 147 _CalcPrefSize()); 148 } 149 150 151 BSize 152 TTZDisplay::_CalcPrefSize() 153 { 154 font_height fontHeight; 155 GetFontHeight(&fontHeight); 156 157 BSize size; 158 size.height = 2 * ceilf(fontHeight.ascent + fontHeight.descent + 159 fontHeight.leading); 160 161 // Add a little padding 162 float padding = 10.0; 163 float firstLine = ceilf(StringWidth(fLabel.String()) + 164 StringWidth(" ") + StringWidth(fTime.String()) + padding); 165 float secondLine = ceilf(StringWidth(fText.String()) + padding); 166 size.width = firstLine > secondLine ? firstLine : secondLine; 167 return size; 168 } 169