1 /* 2 * Copyright 2004-2007, 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 * 10 */ 11 12 #include <stdio.h> 13 14 #include "TZDisplay.h" 15 16 17 namespace { 18 float _FontHeight() 19 { 20 font_height fontHeight; 21 be_plain_font->GetHeight(&fontHeight); 22 float height = ceil(fontHeight.descent + fontHeight.ascent 23 + fontHeight.leading); 24 return height; 25 } 26 } 27 28 29 TTZDisplay::TTZDisplay(BRect frame, const char *name, const char *label) 30 : BView(frame, name, B_FOLLOW_NONE, B_WILL_DRAW), 31 fLabel(label), 32 fText(""), 33 fTime("") 34 { 35 } 36 37 38 TTZDisplay::~TTZDisplay() 39 { 40 } 41 42 43 void 44 TTZDisplay::AttachedToWindow() 45 { 46 if (Parent()) 47 SetViewColor(Parent()->ViewColor()); 48 } 49 50 51 void 52 TTZDisplay::ResizeToPreferred() 53 { 54 ResizeTo(Bounds().Width(), _FontHeight() * 2.0 + 4.0); 55 } 56 57 58 void 59 TTZDisplay::Draw(BRect /* updateRect */) 60 { 61 SetLowColor(ViewColor()); 62 63 BRect bounds = Bounds(); 64 FillRect(Bounds(), B_SOLID_LOW); 65 66 float fontHeight = _FontHeight(); 67 68 BPoint pt(bounds.left + 2.0, fontHeight / 2.0 + 2.0); 69 DrawString(fLabel.String(), pt); 70 71 pt.y += fontHeight; 72 DrawString(fText.String(), pt); 73 74 pt.x = bounds.right - StringWidth(fTime.String()) - 2.0; 75 DrawString(fTime.String(), pt); 76 } 77 78 79 const char* 80 TTZDisplay::Label() const 81 { 82 return fLabel.String(); 83 } 84 85 86 void 87 TTZDisplay::SetLabel(const char *label) 88 { 89 fLabel.SetTo(label); 90 Draw(Bounds()); 91 } 92 93 94 const char* 95 TTZDisplay::Text() const 96 { 97 return fText.String(); 98 } 99 100 101 void 102 TTZDisplay::SetText(const char *text) 103 { 104 fText.SetTo(text); 105 Draw(Bounds()); 106 } 107 108 109 const char* 110 TTZDisplay::Time() const 111 { 112 return fTime.String(); 113 } 114 115 116 void 117 TTZDisplay::SetTime(int32 hour, int32 minute) 118 { 119 int32 ahour = hour; 120 if (hour > 12) 121 ahour = hour -12; 122 123 if (ahour == 0) 124 ahour = 12; 125 126 char *ap = "AM"; 127 if (hour > 11) 128 ap = "PM"; 129 130 char buffer[32]; 131 snprintf(buffer, sizeof(buffer), "%ld:%02ld %s", ahour, minute, ap); 132 133 fTime.SetTo(buffer); 134 135 Invalidate(); 136 } 137 138