1 #include<Message.h> 2 #include <String.h> 3 #include <stdio.h> 4 5 #include <math.h> 6 7 #include "TimeMessages.h" 8 #include "TZDisplay.h" 9 10 11 TTZDisplay::TTZDisplay(BRect frame, const char *name, 12 uint32 resizingmode, uint32 flags, 13 const char *label, const char *text, 14 int32 hour, int32 minute) 15 : BView(frame, name, resizingmode, flags) 16 { 17 f_label = new BString(label); 18 f_text = new BString(text); 19 f_time = new BString(); 20 21 SetTo(hour, minute); 22 } 23 24 25 TTZDisplay::~TTZDisplay() 26 { 27 } 28 29 30 void 31 TTZDisplay::AttachedToWindow() 32 { 33 if (Parent()) 34 SetViewColor(Parent()->ViewColor()); 35 } 36 37 38 void 39 TTZDisplay::MessageReceived(BMessage *message) 40 { 41 switch(message->what) { 42 default: 43 BView::MessageReceived(message); 44 break; 45 } 46 } 47 48 49 static float 50 fontheight() 51 { 52 font_height finfo; 53 be_plain_font->GetHeight(&finfo); 54 float height = ceil(finfo.descent) +ceil(finfo.ascent) +ceil(finfo.leading); 55 return height; 56 } 57 58 59 void 60 TTZDisplay::ResizeToPreferred() 61 { 62 float height = fontheight(); 63 ResizeTo(Bounds().Width(), height *2); 64 } 65 66 67 void 68 TTZDisplay::Draw(BRect updaterect) 69 { 70 BRect bounds(Bounds()); 71 SetLowColor(ViewColor()); 72 FillRect(bounds, B_SOLID_LOW); 73 74 float height = fontheight(); 75 76 BPoint drawpt(bounds.left +2, height/2.0 +1); 77 DrawString(f_label->String(), drawpt); 78 79 drawpt.y += fontheight() +2; 80 DrawString(f_text->String(), drawpt); 81 82 drawpt.x = bounds.right -be_plain_font->StringWidth(f_time->String()) - 2; 83 DrawString(f_time->String(), drawpt); 84 } 85 86 87 void 88 TTZDisplay::SetLabel(const char *label) 89 { 90 f_label->SetTo(label); 91 Draw(Bounds()); 92 } 93 94 void 95 TTZDisplay::SetText(const char *text) 96 { 97 f_text->SetTo(text); 98 Draw(Bounds()); 99 } 100 101 void 102 TTZDisplay::SetTo(int32 hour, int32 minute) 103 { 104 // format time into f_time 105 if (f_time == NULL) 106 f_time = new BString(); 107 else 108 f_time->SetTo(""); 109 110 int32 ahour = hour; 111 if (hour> 12) 112 ahour = hour -12; 113 114 if (ahour == 0) 115 ahour = 12; 116 117 char *ap; 118 if (hour> 11) 119 ap = "PM"; 120 else 121 ap = "AM"; 122 123 char *time = f_time->LockBuffer(8); 124 sprintf(time, "%02lu:%02lu %s", ahour, minute, ap); 125 f_time->UnlockBuffer(8); 126 127 Draw(Bounds()); 128 } 129 130 const char * 131 TTZDisplay::Text() const 132 { 133 return f_text->String(); 134 } 135 136 const char * 137 TTZDisplay::Label() const 138 { 139 return f_label->String(); 140 } 141 142 const char * 143 TTZDisplay::Time() const 144 { 145 return f_time->String(); 146 } 147 148 149 150