* The AM/PM value wasn't correctly shown when it was 12 o'clock. This fixes bug #1184.

* Minor cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@21490 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2007-06-21 22:38:16 +00:00
parent 6af4da5dcb
commit 66dae31027
2 changed files with 112 additions and 113 deletions
+101 -98
View File
@@ -9,8 +9,8 @@
TDateTimeSection::TDateTimeSection(BRect frame, uint32 data) TDateTimeSection::TDateTimeSection(BRect frame, uint32 data)
: TSection(frame) : TSection(frame),
, f_data(data) fData(data)
{ {
} }
@@ -23,17 +23,18 @@ TDateTimeSection::~TDateTimeSection()
uint32 uint32
TDateTimeSection::Data() const TDateTimeSection::Data() const
{ {
return f_data; return fData;
} }
void void
TDateTimeSection::SetData(uint32 data) TDateTimeSection::SetData(uint32 data)
{ {
f_data = data; fData = data;
} }
// #pragma mark -
TTimeEdit::TTimeEdit(BRect frame, const char *name, uint32 sections) TTimeEdit::TTimeEdit(BRect frame, const char *name, uint32 sections)
@@ -46,9 +47,10 @@ TTimeEdit::TTimeEdit(BRect frame, const char *name, uint32 sections)
TTimeEdit::~TTimeEdit() TTimeEdit::~TTimeEdit()
{ {
TSection *section; TSection *section;
if (f_sections->CountItems() > 0) if (f_sections->CountItems() > 0) {
for (int32 idx = 0; (section = (TSection *)f_sections->ItemAt(idx)); idx++) for (int32 idx = 0; (section = (TSection *)f_sections->ItemAt(idx)); idx++)
delete section; delete section;
}
delete f_sections; delete f_sections;
} }
@@ -62,54 +64,56 @@ TTimeEdit::InitView()
void void
TTimeEdit::DrawSection(uint32 index, bool isfocus) TTimeEdit::DrawSection(uint32 index, bool hasFocus)
{ {
if (f_sections->CountItems() == 0) if (f_sections->CountItems() == 0)
printf("No Sections!!!\n"); printf("No Sections!!!\n");
// user defined section drawing // user defined section drawing
TDateTimeSection *section; TDateTimeSection *section;
section = (TDateTimeSection *)f_sections->ItemAt(index); section = (TDateTimeSection *)f_sections->ItemAt(index);
BRect bounds = section->Frame(); BRect bounds = section->Frame();
// format value to display // format value to display
uint32 value; uint32 value;
if (isfocus) { if (hasFocus) {
SetLowColor(tint_color(ViewColor(), B_DARKEN_1_TINT)); SetLowColor(tint_color(ViewColor(), B_DARKEN_1_TINT));
value = f_holdvalue; value = f_holdvalue;
} else { } else {
SetLowColor(ViewColor()); SetLowColor(ViewColor());
value = section->Data(); value = section->Data();
} }
BString text; BString text;
// format value (new method?) // format value (new method?)
switch (index) { switch (index) {
case 0: // hour case 0: // hour
if (value> 12) { if (value > 12) {
if (value < 22) text << "0"; if (value < 22)
text << value -12; text << "0";
text << value - 12;
} else if (value == 0) { } else if (value == 0) {
text << "12"; text << "12";
} else { } else {
if (value <10) text << "0"; if (value < 10)
text << "0";
text << value; text << value;
} }
break; break;
case 1: // minute case 1: // minute
case 2: // second case 2: // second
if (value <10) text << "0"; if (value < 10)
text << "0";
text << value; text << value;
break; break;
case 3: //am/pm case 3: // am/pm
value = ((TDateTimeSection *)f_sections->ItemAt(0))->Data(); value = ((TDateTimeSection *)f_sections->ItemAt(0))->Data();
if (value >12 || value == 0) if (value >= 12)
text << "PM"; text << "PM";
else else
text << "AM"; text << "AM";
@@ -297,28 +301,28 @@ TTimeEdit::CheckRange()
{ {
int32 value = f_holdvalue; int32 value = f_holdvalue;
switch (f_focus) { switch (f_focus) {
case 0: //hour case 0: // hour
if (value> 23) if (value> 23)
value = 0; value = 0;
else if (value < 0) else if (value < 0)
value = 23; value = 23;
break; break;
case 1: //minute case 1: // minute
if (value> 59) if (value> 59)
value = 0; value = 0;
else if (value < 0) else if (value < 0)
value = 59; value = 59;
break; break;
case 2: //second case 2: // second
if (value > 59) if (value > 59)
value = 0; value = 0;
else if (value < 0) else if (value < 0)
value = 59; value = 59;
break; break;
case 3: case 3:
// modify hour value to reflect change in am/pm // modify hour value to reflect change in am/pm
value = ((TDateTimeSection *)f_sections->ItemAt(0))->Data(); value = ((TDateTimeSection *)f_sections->ItemAt(0))->Data();
@@ -326,28 +330,27 @@ TTimeEdit::CheckRange()
value += 12; value += 12;
else else
value -= 12; value -= 12;
if (value == 24) value = 0; if (value == 24)
value = 0;
((TDateTimeSection *)f_sections->ItemAt(0))->SetData(value); ((TDateTimeSection *)f_sections->ItemAt(0))->SetData(value);
break; break;
default: default:
return; return;
} }
((TDateTimeSection *)f_sections->ItemAt(f_focus))->SetData(value); ((TDateTimeSection *)f_sections->ItemAt(f_focus))->SetData(value);
f_holdvalue = value; f_holdvalue = value;
Draw(Bounds()); Invalidate(Bounds());
} }
// #pragma mark -
/* DATE TSECTIONEDIT */ const char *months[] = {
"January", "Febuary", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
const char *months[] =
{ "January", "Febuary", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
}; };
@@ -361,9 +364,10 @@ TDateEdit::TDateEdit(BRect frame, const char *name, uint32 sections)
TDateEdit::~TDateEdit() TDateEdit::~TDateEdit()
{ {
TSection *section; TSection *section;
if (f_sections->CountItems() > 0) if (f_sections->CountItems() > 0) {
for (int32 idx = 0; (section = (TSection *)f_sections->ItemAt(idx)); idx++) for (int32 idx = 0; (section = (TSection *)f_sections->ItemAt(idx)); idx++)
delete section; delete section;
}
delete f_sections; delete f_sections;
} }
@@ -377,7 +381,7 @@ TDateEdit::InitView()
void void
TDateEdit::DrawSection(uint32 index, bool isfocus) TDateEdit::DrawSection(uint32 index, bool hasFocus)
{ {
if (f_sections->CountItems() == 0) if (f_sections->CountItems() == 0)
printf("No Sections!!!\n"); printf("No Sections!!!\n");
@@ -385,55 +389,53 @@ TDateEdit::DrawSection(uint32 index, bool isfocus)
// user defined section drawing // user defined section drawing
TDateTimeSection *section; TDateTimeSection *section;
section = (TDateTimeSection *)f_sections->ItemAt(index); section = (TDateTimeSection *)f_sections->ItemAt(index);
BRect bounds = section->Frame(); BRect bounds = section->Frame();
// format value to display // format value to display
uint32 value; uint32 value;
if (isfocus) { if (hasFocus) {
SetLowColor(tint_color(ViewColor(), B_DARKEN_1_TINT)); SetLowColor(tint_color(ViewColor(), B_DARKEN_1_TINT));
value = f_holdvalue; value = f_holdvalue;
} else { } else {
SetLowColor(ViewColor()); SetLowColor(ViewColor());
value = section->Data(); value = section->Data();
} }
BString text; char text[64];
// format value (new method?) // format value (new method?)
switch (index) { switch (index) {
case 0: // month case 0: // month
text << months[value]; struct tm tm;
break; tm.tm_mon = value;
strftime(text, sizeof(text), "%B", &tm);
break;
case 1: // day case 1: // day
text << value; snprintf(text, sizeof(text), "%lu", value);
break; break;
case 2: // year case 2: // year
text << value +1900; snprintf(text, sizeof(text), "%lu", value + 1900);
break; break;
default: default:
return; return;
break;
} }
// calc and center text in section rect // calc and center text in section rect
float width = be_plain_font->StringWidth(text.String()); float width = StringWidth(text);
BPoint offset(-(bounds.Width()/2.0 -width/2.0) -1.0, (bounds.Height()/2.0 -6.0)); BPoint offset(-(bounds.Width() - width) /2.0 - 1.0, (bounds.Height() / 2.0 - 6.0));
if (index == 0) if (index == 0)
offset.x = -(bounds.Width() -width) ; offset.x = -(bounds.Width() - width) ;
BPoint drawpt(bounds.LeftBottom() -offset);
SetHighColor(0, 0, 0, 255); SetHighColor(0, 0, 0, 255);
FillRect(bounds, B_SOLID_LOW); FillRect(bounds, B_SOLID_LOW);
DrawString(text.String(), drawpt); DrawString(text, bounds.LeftBottom() - offset);
} }
@@ -444,14 +446,18 @@ TDateEdit::DrawSeperator(uint32 index)
in f_sections or the section to the seps left in f_sections or the section to the seps left
*/ */
if (index == 3) return; // no seperator for am/pm if (index == 3) {
// no seperator for am/pm
return;
}
BString text("/"); BString text("/");
uint32 sep; uint32 sep;
GetSeperatorWidth(&sep); GetSeperatorWidth(&sep);
TDateTimeSection *section = (TDateTimeSection *)f_sections->ItemAt(index); TDateTimeSection *section = (TDateTimeSection *)f_sections->ItemAt(index);
BRect bounds = section->Frame(); BRect bounds = section->Frame();
float width = be_plain_font->StringWidth(text.String()); float width = be_plain_font->StringWidth(text.String());
BPoint offset(-(sep/2.0 -width/2.0) -1.0, bounds.Height()/2.0 -6.0); BPoint offset(-(sep/2.0 -width/2.0) -1.0, bounds.Height()/2.0 -6.0);
BPoint drawpt(bounds.RightBottom() -offset); BPoint drawpt(bounds.RightBottom() -offset);
@@ -469,25 +475,25 @@ TDateEdit::SetSections(BRect area)
section = new TDateTimeSection(area); section = new TDateTimeSection(area);
f_sections->AddItem(section); f_sections->AddItem(section);
} }
BRect bounds(area); BRect bounds(area);
float width; float width;
// year // year
width = be_plain_font->StringWidth("0000") +6; width = be_plain_font->StringWidth("0000") +6;
bounds.right = area.right; bounds.right = area.right;
bounds.left = bounds.right -width; bounds.left = bounds.right -width;
((TDateTimeSection *)f_sections->ItemAt(2))->SetBounds(bounds); ((TDateTimeSection *)f_sections->ItemAt(2))->SetBounds(bounds);
uint32 sep; uint32 sep;
GetSeperatorWidth(&sep); GetSeperatorWidth(&sep);
// day // day
width = be_plain_font->StringWidth("00") +6; width = be_plain_font->StringWidth("00") +6;
bounds.right = bounds.left -sep; bounds.right = bounds.left -sep;
bounds.left = bounds.right -width; bounds.left = bounds.right -width;
((TDateTimeSection *)f_sections->ItemAt(1))->SetBounds(bounds); ((TDateTimeSection *)f_sections->ItemAt(1))->SetBounds(bounds);
// month // month
bounds.right = bounds.left -sep; bounds.right = bounds.left -sep;
bounds.left = area.left; bounds.left = area.left;
@@ -509,7 +515,7 @@ TDateEdit::SectionFocus(uint32 index)
// update hold value // update hold value
f_holdvalue = ((TDateTimeSection *)f_sections->ItemAt(f_focus))->Data(); f_holdvalue = ((TDateTimeSection *)f_sections->ItemAt(f_focus))->Data();
Draw(Bounds()); Draw(Bounds());
} }
@@ -517,30 +523,29 @@ TDateEdit::SectionFocus(uint32 index)
void void
TDateEdit::SetTo(uint32 year, uint32 month, uint32 day) TDateEdit::SetTo(uint32 year, uint32 month, uint32 day)
{ {
if (f_sections->CountItems()> 0) if (f_sections->CountItems() > 0) {
{
bool update = false; bool update = false;
TDateTimeSection *section = (TDateTimeSection *)f_sections->ItemAt(0); TDateTimeSection *section = (TDateTimeSection *)f_sections->ItemAt(0);
if (section->Data() != month) { if (section->Data() != month) {
section->SetData(month); section->SetData(month);
update = true; update = true;
} }
section = (TDateTimeSection *)f_sections->ItemAt(1); section = (TDateTimeSection *)f_sections->ItemAt(1);
if (section->Data() != day) { if (section->Data() != day) {
section->SetData(day); section->SetData(day);
update = true; update = true;
} }
section = (TDateTimeSection *)f_sections->ItemAt(2); section = (TDateTimeSection *)f_sections->ItemAt(2);
if (section->Data() != year) { if (section->Data() != year) {
section->SetData(year); section->SetData(year);
update = true; update = true;
} }
if (update) if (update)
Draw(Bounds()); Invalidate(Bounds());
} }
} }
@@ -553,9 +558,9 @@ TDateEdit::DoUpPress()
// update displayed value // update displayed value
f_holdvalue += 1; f_holdvalue += 1;
CheckRange(); CheckRange();
// send message to change Date // send message to change Date
DispatchMessage(); DispatchMessage();
} }
@@ -569,9 +574,9 @@ TDateEdit::DoDownPress()
// update display value // update display value
f_holdvalue -= 1; f_holdvalue -= 1;
CheckRange(); CheckRange();
// send message to change Date // send message to change Date
DispatchMessage(); DispatchMessage();
} }
@@ -581,17 +586,17 @@ void
TDateEdit::BuildDispatch(BMessage *message) TDateEdit::BuildDispatch(BMessage *message)
{ {
static const char *fields[3] = {"month", "day", "year"}; static const char *fields[3] = {"month", "day", "year"};
message->AddBool("time", false); message->AddBool("time", false);
for (int32 idx = 0; idx < f_sections->CountItems(); idx++) { for (int32 idx = 0; idx < f_sections->CountItems(); idx++) {
uint32 data; uint32 data;
if (f_focus == idx) if (f_focus == idx)
data = f_holdvalue; data = f_holdvalue;
else else
data = ((TDateTimeSection *)f_sections->ItemAt(idx))->Data(); data = ((TDateTimeSection *)f_sections->ItemAt(idx))->Data();
message->AddInt32(fields[idx], data); message->AddInt32(fields[idx], data);
} }
} }
@@ -611,7 +616,7 @@ TDateEdit::CheckRange()
value = 11; value = 11;
break; break;
} }
case 1: //day case 1: //day
{ {
uint32 month = ((TDateTimeSection *)f_sections->ItemAt(0))->Data(); uint32 month = ((TDateTimeSection *)f_sections->ItemAt(0))->Data();
@@ -624,7 +629,7 @@ TDateEdit::CheckRange()
value = daycnt; value = daycnt;
break; break;
} }
case 2: //year case 2: //year
{ {
if (value > YEAR_DELTA_MAX) if (value > YEAR_DELTA_MAX)
@@ -633,16 +638,14 @@ TDateEdit::CheckRange()
value = YEAR_DELTA_MAX; value = YEAR_DELTA_MAX;
break; break;
} }
default: default:
return; return;
} }
((TDateTimeSection *)f_sections->ItemAt(f_focus))->SetData(value); ((TDateTimeSection *)f_sections->ItemAt(f_focus))->SetData(value);
f_holdvalue = value; f_holdvalue = value;
Draw(Bounds()); Draw(Bounds());
} }
//---//
+11 -15
View File
@@ -4,7 +4,7 @@
#include "SectionEdit.h" #include "SectionEdit.h"
// TSection descendent to hold uint32 value // TSection descendent to hold uint32 value
class TDateTimeSection: public TSection { class TDateTimeSection : public TSection {
public: public:
TDateTimeSection(BRect frame, uint32 data = 0); TDateTimeSection(BRect frame, uint32 data = 0);
~TDateTimeSection(); ~TDateTimeSection();
@@ -12,38 +12,35 @@ class TDateTimeSection: public TSection {
uint32 Data() const; uint32 Data() const;
void SetData(uint32 data); void SetData(uint32 data);
private: private:
uint32 f_data; uint32 fData;
}; };
// TSectionEdit descendent to edit time // TSectionEdit descendent to edit time
class TTimeEdit: public TSectionEdit { class TTimeEdit : public TSectionEdit {
public: public:
TTimeEdit(BRect frame, const char *name, uint32 sections); TTimeEdit(BRect frame, const char *name, uint32 sections);
~TTimeEdit(); ~TTimeEdit();
virtual void InitView(); virtual void InitView();
virtual void DrawSection(uint32 index, bool isfocus); virtual void DrawSection(uint32 index, bool isfocus);
virtual void DrawSeperator(uint32 index); virtual void DrawSeperator(uint32 index);
virtual void SetSections(BRect area); virtual void SetSections(BRect area);
virtual void SectionFocus(uint32 index); virtual void SectionFocus(uint32 index);
virtual void GetSeperatorWidth(uint32 *width); virtual void GetSeperatorWidth(uint32 *width);
void CheckRange(); void CheckRange();
virtual void DoUpPress(); virtual void DoUpPress();
virtual void DoDownPress(); virtual void DoDownPress();
virtual void BuildDispatch(BMessage *message); virtual void BuildDispatch(BMessage *message);
void SetTo(uint32 hour, uint32 minute, uint32 second); void SetTo(uint32 hour, uint32 minute, uint32 second);
}; };
/* DATE TSECTIONEDIT */
// TSectionEdit descendent to edit Date // TSectionEdit descendent to edit Date
class TDateEdit: public TSectionEdit { class TDateEdit : public TSectionEdit {
public: public:
TDateEdit(BRect frame, const char *name, uint32 sections); TDateEdit(BRect frame, const char *name, uint32 sections);
~TDateEdit(); ~TDateEdit();
@@ -66,5 +63,4 @@ class TDateEdit: public TSectionEdit {
void SetTo(uint32 hour, uint32 minute, uint32 second); void SetTo(uint32 hour, uint32 minute, uint32 second);
}; };
#endif // DATETIME_H
#endif //DATETIME_H