1 /* 2 BaseView.cpp 3 by Mike Berg (inseculous) 4 */ 5 6 #include <Alert.h> 7 #include <OS.h> 8 9 #include "BaseView.h" 10 #include "TimeMessages.h" 11 12 13 TTimeBaseView::TTimeBaseView(BRect frame, const char *name) 14 : BView(frame, name, B_FOLLOW_ALL_SIDES, B_PULSE_NEEDED), 15 fMessage(NULL) 16 { 17 fMessage = new BMessage(H_TIME_UPDATE); 18 } 19 20 21 TTimeBaseView::~TTimeBaseView() 22 { 23 delete fMessage; 24 } 25 26 27 void 28 TTimeBaseView::Pulse() 29 { 30 if (IsWatched()) 31 DispatchMessage(); 32 } 33 34 35 void 36 TTimeBaseView::AttachedToWindow() 37 { 38 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 39 } 40 41 42 void 43 TTimeBaseView::SetGMTime(bool gmt) 44 { 45 fIsGMT = gmt; 46 } 47 48 49 void 50 TTimeBaseView::DispatchMessage() 51 { 52 if (fMessage == NULL) 53 return; 54 55 time_t current = time(NULL); 56 57 struct tm *ltime; 58 59 if (fIsGMT) 60 ltime = gmtime(¤t); 61 else 62 ltime = localtime(¤t); 63 64 int32 month = ltime->tm_mon; 65 int32 day = ltime->tm_mday; 66 int32 year = ltime->tm_year; 67 int32 hour = ltime->tm_hour; 68 int32 minute = ltime->tm_min; 69 int32 second = ltime->tm_sec; 70 71 fMessage->MakeEmpty(); 72 fMessage->AddInt32("month", month); 73 fMessage->AddInt32("day", day); 74 fMessage->AddInt32("year", year); 75 fMessage->AddInt32("hour", hour); 76 fMessage->AddInt32("minute", minute); 77 fMessage->AddInt32("second", second); 78 79 SendNotices(H_TM_CHANGED, fMessage); 80 } 81 82 83 void 84 TTimeBaseView::ChangeTime(BMessage *message) 85 { 86 bool istime; 87 if (message->FindBool("time", &istime) != B_OK) 88 return; 89 90 time_t atime = time(NULL); 91 struct tm *_tm = localtime(&atime); 92 93 int32 hour = 0; 94 int32 minute = 0; 95 int32 second = 0; 96 int32 month = 0; 97 int32 day = 0; 98 int32 year = 0; 99 bool isam = false; 100 if (istime) { 101 if (message->FindInt32("hour", &hour) == B_OK) 102 _tm->tm_hour = hour; 103 if (message->FindInt32("minute", &minute) == B_OK) 104 _tm->tm_min = minute; 105 if (message->FindInt32("second", &second) == B_OK) 106 _tm->tm_sec = second; 107 if (message->FindBool("isam", &isam) == B_OK) { 108 if (!isam) 109 _tm->tm_hour += 12; 110 } 111 } else { 112 if (message->FindInt32("month", &month) == B_OK) 113 _tm->tm_mon = month; 114 if (message->FindInt32("day", &day) == B_OK) 115 _tm->tm_mday = day; 116 if (message->FindInt32("year", &year) == B_OK) 117 _tm->tm_year = year; 118 } 119 120 time_t atime2 = mktime(_tm); 121 set_real_time_clock(atime2); 122 } 123