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 95 void 96 TTZDisplay::SetText(const char *text) 97 { 98 f_text->SetTo(text); 99 Draw(Bounds()); 100 } 101 102 103 void 104 TTZDisplay::SetTo(int32 hour, int32 minute) 105 { 106 // format time into f_time 107 if (f_time == NULL) 108 f_time = new BString(); 109 else 110 f_time->SetTo(""); 111 112 int32 ahour = hour; 113 if (hour> 12) 114 ahour = hour -12; 115 116 if (ahour == 0) 117 ahour = 12; 118 119 char *ap; 120 if (hour> 11) 121 ap = "PM"; 122 else 123 ap = "AM"; 124 125 char *time = f_time->LockBuffer(8); 126 sprintf(time, "%02lu:%02lu %s", ahour, minute, ap); 127 f_time->UnlockBuffer(8); 128 129 Invalidate(); 130 } 131 132 133 const char * 134 TTZDisplay::Text() const 135 { 136 return f_text->String(); 137 } 138 139 140 const char * 141 TTZDisplay::Label() const 142 { 143 return f_label->String(); 144 } 145 146 147 const char * 148 TTZDisplay::Time() const 149 { 150 return f_time->String(); 151 } 152