* Made TSectionEdit use be_control_look to draw its frame. Also, it no longer

uses those ugly bitmaps for the up/down buttons - while the updated drawing is
  far from nice either, it at least fits our UI style a lot better.
* Fixed typo "seperator" -> "separator".
* Coding style cleanup (still some stuff left).


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@31374 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2009-07-02 15:32:19 +00:00
parent d3bde5de44
commit 02b73236ae
4 changed files with 183 additions and 234 deletions

View File

@ -24,9 +24,10 @@
using BPrivate::B_LOCAL_TIME;
TTimeEdit::TTimeEdit(BRect frame, const char *name, uint32 sections)
: TSectionEdit(frame, name, sections),
fLastKeyDownTime(0)
TTimeEdit::TTimeEdit(BRect frame, const char* name, uint32 sections)
:
TSectionEdit(frame, name, sections),
fLastKeyDownTime(0)
{
InitView();
fTime = BTime::CurrentTime(B_LOCAL_TIME);
@ -51,7 +52,7 @@ TTimeEdit::KeyDown(const char* bytes, int32 numBytes)
int32 section = FocusIndex();
if (section < 0 || section > 2)
return;
bigtime_t currentTime = system_time();
if (currentTime - fLastKeyDownTime < 1000000) {
int32 doubleDigi = number + fLastKeyDownInt * 10;
@ -63,12 +64,12 @@ TTimeEdit::KeyDown(const char* bytes, int32 numBytes)
fLastKeyDownTime = currentTime;
fLastKeyDownInt = number;
}
// update display value
fHoldValue = number;
_CheckRange();
// send message to change time
DispatchMessage();
}
@ -105,8 +106,8 @@ TTimeEdit::DrawSection(uint32 index, bool hasFocus)
BString text;
switch (index) {
case 0:
{ // hour
case 0:
// hour
if (value > 12) {
if (value < 22)
text << "0";
@ -117,67 +118,65 @@ TTimeEdit::DrawSection(uint32 index, bool hasFocus)
if (value < 10)
text << "0";
text << value;
}
} break;
}
break;
case 1:
case 2:
{ // minute
// minute
// second
if (value < 10)
text << "0";
text << value;
} break;
break;
case 3:
{ // am/pm
// am/pm
value = fTime.Hour();
if (value >= 12)
text << "PM";
else
else
text << "AM";
} break;
break;
default:
return;
break;
}
// calc and center text in section rect
float width = be_plain_font->StringWidth(text.String());
BPoint offset(-((bounds.Width()- 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);
SetHighColor(0, 0, 0, 255);
FillRect(bounds, B_SOLID_LOW);
DrawString(text.String(), bounds.LeftBottom() - offset);
DrawString(text.String(), bounds.LeftBottom() - offset);
}
void
TTimeEdit::DrawSeperator(uint32 index)
TTimeEdit::DrawSeparator(uint32 index)
{
if (index == 3)
return;
return;
TSection *section = NULL;
section = static_cast<TSection*> (fSectionList->ItemAt(index));
if (!section)
return;
BRect bounds = section->Frame();
float sepWidth = SeperatorWidth();
float sepWidth = SeparatorWidth();
char* sep = ":";
if (index == 2)
sep = "-";
float width = be_plain_font->StringWidth(sep);
BPoint offset(-((sepWidth - width) / 2.0) -1.0
, bounds.Height() / 2.0 -6.0);
DrawString(sep, bounds.RightBottom() - offset);
BPoint offset(-((sepWidth - width) / 2.0) -1.0, bounds.Height() / 2.0 -6.0);
DrawString(sep, bounds.RightBottom() - offset);
}
@ -186,16 +185,16 @@ TTimeEdit::SetSections(BRect area)
{
// by default divide up the sections evenly
BRect bounds(area);
float sepWidth = SeperatorWidth();
float sepWidth = SeparatorWidth();
float sep_2 = ceil(sepWidth / fSectionCount +1);
float width = bounds.Width() / fSectionCount -sep_2;
bounds.right = bounds.left + (width -sepWidth / fSectionCount);
for (uint32 idx = 0; idx < fSectionCount; idx++) {
fSectionList->AddItem(new TSection(bounds));
bounds.left = bounds.right + sepWidth;
if (idx == fSectionCount -2)
bounds.right = area.right -1;
@ -206,7 +205,7 @@ TTimeEdit::SetSections(BRect area)
float
TTimeEdit::SeperatorWidth() const
TTimeEdit::SeparatorWidth() const
{
return 10.0f;
}
@ -225,7 +224,7 @@ TTimeEdit::SectionFocus(uint32 index)
void
TTimeEdit::SetTime(int32 hour, int32 minute, int32 second)
{
if (fTime.Hour() == hour && fTime.Minute() == minute
if (fTime.Hour() == hour && fTime.Minute() == minute
&& fTime.Second() == second)
return;
@ -239,17 +238,17 @@ TTimeEdit::DoUpPress()
{
if (fFocus == -1)
SectionFocus(0);
// update displayed value
fHoldValue += 1;
_CheckRange();
// send message to change time
DispatchMessage();
}
void
TTimeEdit::DoDownPress()
{
@ -258,27 +257,27 @@ TTimeEdit::DoDownPress()
// update display value
fHoldValue -= 1;
_CheckRange();
// send message to change time
DispatchMessage();
}
void
TTimeEdit::BuildDispatch(BMessage *message)
{
const char *fields[3] = { "hour", "minute", "second" };
message->AddBool("time", true);
for (int32 index = 0; index < fSectionList->CountItems() -1; ++index) {
uint32 data = _SectionValue(index);
if (fFocus == index)
data = fHoldValue;
message->AddInt32(fields[index], data);
}
}
@ -290,37 +289,37 @@ TTimeEdit::_CheckRange()
int32 value = fHoldValue;
switch (fFocus) {
case 0:
{ // hour
if (value > 23)
// hour
if (value > 23)
value = 0;
else if (value < 0)
else if (value < 0)
value = 23;
fTime.SetTime(value, fTime.Minute(), fTime.Second());
} break;
break;
case 1:
{ // minute
// minute
if (value> 59)
value = 0;
else if (value < 0)
value = 59;
fTime.SetTime(fTime.Hour(), value, fTime.Second());
} break;
break;
case 2:
{ // second
// second
if (value > 59)
value = 0;
else if (value < 0)
value = 59;
fTime.SetTime(fTime.Hour(), fTime.Minute(), value);
} break;
break;
case 3:
{
// AM/PM
value = fTime.Hour();
if (value < 13)
value += 12;
@ -331,7 +330,7 @@ TTimeEdit::_CheckRange()
// modify hour value to reflect change in am/ pm
fTime.SetTime(value, fTime.Minute(), fTime.Second());
} break;
break;
default:
return;
@ -348,22 +347,22 @@ TTimeEdit::_IsValidDoubleDigi(int32 value)
bool isInRange = false;
switch (fFocus) {
case 0:
{ // hour
if (value <= 23)
// hour
if (value <= 23)
isInRange = true;
} break;
break;
case 1:
{ // minute
// minute
if (value <= 59)
isInRange = true;
} break;
break;
case 2:
{ // second
// second
if (value <= 59)
isInRange = true;
} break;
break;
default:
return isInRange;
@ -381,7 +380,7 @@ TTimeEdit::_SectionValue(int32 index) const
case 0:
value = fTime.Hour();
break;
case 1:
value = fTime.Minute();
break;
@ -428,7 +427,7 @@ TDateEdit::KeyDown(const char* bytes, int32 numBytes)
int32 section = FocusIndex();
if (section < 1 || section > 2)
return;
bigtime_t currentTime = system_time();
if (currentTime - fLastKeyDownTime < 1000000) {
int32 doubleDigi = number + fLastKeyDownInt * 10;
@ -440,7 +439,7 @@ TDateEdit::KeyDown(const char* bytes, int32 numBytes)
fLastKeyDownTime = currentTime;
fLastKeyDownInt = number;
}
// if year add 2000
if (section == 2) {
int32 oldCentury = int32(fHoldValue / 100) * 100;
@ -450,9 +449,9 @@ TDateEdit::KeyDown(const char* bytes, int32 numBytes)
}
// update display value
fHoldValue = number;
_CheckRange();
// send message to change time
DispatchMessage();
}
@ -496,17 +495,17 @@ TDateEdit::DrawSection(uint32 index, bool hasFocus)
// calc and center text in section rect
float width = StringWidth(text.String());
BPoint offset(-(bounds.Width() - 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));
SetHighColor(0, 0, 0, 255);
FillRect(bounds, B_SOLID_LOW);
DrawString(text.String(), bounds.LeftBottom() - offset);
DrawString(text.String(), bounds.LeftBottom() - offset);
}
void
TDateEdit::DrawSeperator(uint32 index)
TDateEdit::DrawSeparator(uint32 index)
{
if (index == 3)
return;
@ -515,14 +514,14 @@ TDateEdit::DrawSeperator(uint32 index)
section = static_cast<TSection*> (fSectionList->ItemAt(index));
BRect bounds = section->Frame();
float sepWidth = SeperatorWidth();
float sepWidth = SeparatorWidth();
float width = be_plain_font->StringWidth("/");
BPoint offset(-(sepWidth / 2.0 - width / 2.0) -1.0
, bounds.Height() / 2.0 -6.0);
BPoint offset(-(sepWidth / 2.0 - width / 2.0) -1.0,
bounds.Height() / 2.0 -6.0);
SetHighColor(0, 0, 0, 255);
DrawString("/", bounds.RightBottom() - offset);
DrawString("/", bounds.RightBottom() - offset);
}
@ -534,7 +533,7 @@ TDateEdit::SetSections(BRect area)
fSectionList->AddItem(new TSection(area));
BRect bounds(area);
float sepWidth = SeperatorWidth();
float sepWidth = SeparatorWidth();
// year
TSection *section = NULL;
@ -560,7 +559,7 @@ TDateEdit::SetSections(BRect area)
float
TDateEdit::SeperatorWidth() const
TDateEdit::SeparatorWidth() const
{
return 10.0f;
}
@ -602,7 +601,7 @@ TDateEdit::DoUpPress()
DispatchMessage();
}
void
TDateEdit::DoDownPress()
{
@ -617,8 +616,8 @@ TDateEdit::DoDownPress()
// send message to change Date
DispatchMessage();
}
void
TDateEdit::BuildDispatch(BMessage *message)
{
@ -642,22 +641,21 @@ void
TDateEdit::_CheckRange()
{
int32 value = fHoldValue;
switch (fFocus) {
case 0:
{
// month
// month
if (value > 12)
value = 1;
else if (value < 1)
value = 12;
fDate.SetDate(fDate.Year(), value, fDate.Day());
} break;
break;
case 1:
{
//day
// day
int32 days = fDate.DaysInMonth();
if (value > days)
value = 1;
@ -665,18 +663,18 @@ TDateEdit::_CheckRange()
value = days;
fDate.SetDate(fDate.Year(), fDate.Month(), value);
} break;
break;
}
case 2:
{
//year
// year
if (value > 2037)
value = 2037;
else if (value < 1970)
value = 1970;
fDate.SetDate(value, fDate.Month(), fDate.Day());
} break;
break;
default:
return;
@ -694,19 +692,22 @@ TDateEdit::_IsValidDoubleDigi(int32 value)
int32 year = 0;
switch (fFocus) {
case 1:
{ // day
//day
{
// day
int32 days = fDate.DaysInMonth();
if (value <= days)
isInRange = true;
} break;
break;
}
case 2:
{ //year
{
// year
year = int32(fHoldValue / 100) * 100 + value;
if (year <= 2037 && year >= 1970)
isInRange = true;
} break;
break;
}
default:
return isInRange;

View File

@ -30,11 +30,11 @@ class TTimeEdit : public TSectionEdit {
virtual void InitView();
virtual void DrawSection(uint32 index, bool isfocus);
virtual void DrawSeperator(uint32 index);
virtual void DrawSeparator(uint32 index);
virtual void SetSections(BRect area);
virtual void SectionFocus(uint32 index);
virtual float SeperatorWidth() const;
virtual float SeparatorWidth() const;
virtual void DoUpPress();
virtual void DoDownPress();
@ -63,11 +63,11 @@ class TDateEdit : public TSectionEdit {
virtual void InitView();
virtual void DrawSection(uint32 index, bool isfocus);
virtual void DrawSeperator(uint32 index);
virtual void DrawSeparator(uint32 index);
virtual void SetSections(BRect area);
virtual void SectionFocus(uint32 index);
virtual float SeperatorWidth() const;
virtual float SeparatorWidth() const;
virtual void DoUpPress();
virtual void DoDownPress();

View File

@ -9,25 +9,24 @@
*/
#include "SectionEdit.h"
#include "Bitmaps.h"
#include "TimeMessages.h"
#include <Bitmap.h>
#include <ControlLook.h>
#include <List.h>
#include <Window.h>
#include "TimeMessages.h"
const uint32 kArrowAreaWidth = 16;
TSectionEdit::TSectionEdit(BRect frame, const char *name, uint32 sections)
: BControl(frame, name, NULL, NULL, B_FOLLOW_NONE, B_NAVIGABLE | B_WILL_DRAW),
fUpArrow(NULL),
fDownArrow(NULL),
fSectionList(NULL),
fFocus(-1),
fSectionCount(sections)
:
BControl(frame, name, NULL, NULL, B_FOLLOW_NONE, B_NAVIGABLE | B_WILL_DRAW),
fSectionList(NULL),
fFocus(-1),
fSectionCount(sections)
{
InitView();
}
@ -35,9 +34,6 @@ TSectionEdit::TSectionEdit(BRect frame, const char *name, uint32 sections)
TSectionEdit::~TSectionEdit()
{
delete fUpArrow;
delete fDownArrow;
int32 count = fSectionList->CountItems();
if (count > 0) {
for (int32 index = 0; index < count; index++)
@ -50,22 +46,20 @@ TSectionEdit::~TSectionEdit()
void
TSectionEdit::AttachedToWindow()
{
if (Parent()) {
if (Parent())
SetViewColor(Parent()->ViewColor());
ReplaceTransparentColor(fUpArrow, ViewColor());
ReplaceTransparentColor(fDownArrow, ViewColor());
}
}
void
TSectionEdit::Draw(BRect updateRect)
{
DrawBorder();
DrawBorder(updateRect);
for (uint32 idx = 0; idx < fSectionCount; idx++) {
DrawSection(idx, ((uint32)fFocus == idx) && IsFocus());
if (idx < fSectionCount -1)
DrawSeperator(idx);
DrawSeparator(idx);
}
}
@ -74,7 +68,7 @@ void
TSectionEdit::MouseDown(BPoint where)
{
MakeFocus(true);
if (fUpRect.Contains(where))
DoUpPress();
else if (fDownRect.Contains(where))
@ -87,7 +81,7 @@ TSectionEdit::MouseDown(BPoint where)
SectionFocus(idx);
return;
}
}
}
}
}
@ -99,7 +93,7 @@ TSectionEdit::MakeFocus(bool focused)
return;
BControl::MakeFocus(focused);
if (fFocus == -1)
SectionFocus(0);
else
@ -112,33 +106,33 @@ TSectionEdit::KeyDown(const char *bytes, int32 numbytes)
{
if (fFocus == -1)
SectionFocus(0);
switch (bytes[0]) {
case B_LEFT_ARROW:
fFocus -= 1;
if (fFocus < 0)
fFocus = fSectionCount -1;
SectionFocus(fFocus);
break;
break;
case B_RIGHT_ARROW:
fFocus += 1;
if ((uint32)fFocus >= fSectionCount)
fFocus = 0;
SectionFocus(fFocus);
break;
break;
case B_UP_ARROW:
DoUpPress();
break;
break;
case B_DOWN_ARROW:
DoDownPress();
break;
break;
default:
BControl::KeyDown(bytes, numbytes);
break;
break;
}
Draw(Bounds());
}
@ -170,103 +164,60 @@ TSectionEdit::FocusIndex() const
void
TSectionEdit::InitView()
{
// create arrow bitmaps
BRect rect(0, 0, kUpArrowWidth -1, kUpArrowHeight -1);
fUpArrow = new BBitmap(rect, kUpArrowColorSpace);
fUpArrow->SetBits(kUpArrowBits, (kUpArrowWidth) *(kUpArrowHeight+1), 0
, kUpArrowColorSpace);
rect = BRect(0, 0, kDownArrowWidth -1, kDownArrowHeight -2);
fDownArrow = new BBitmap(rect, kDownArrowColorSpace);
fDownArrow->SetBits(kDownArrowBits, (kDownArrowWidth) *(kDownArrowHeight)
, 0, kDownArrowColorSpace);
// setup sections
fSectionList = new BList(fSectionCount);
fSectionArea = Bounds().InsetByCopy(2, 2);
fSectionArea.right -= kArrowAreaWidth;
fSectionArea.right -= kArrowAreaWidth;
}
void
TSectionEdit::Draw3DFrame(BRect frame, bool inset)
TSectionEdit::DrawBorder(const BRect& updateRect)
{
rgb_color color1 = LowColor();
rgb_color color2 = HighColor();
if (inset) {
color1 = HighColor();
color2 = LowColor();
}
BeginLineArray(4);
// left side
AddLine(frame.LeftBottom(), frame.LeftTop(), color2);
// right side
AddLine(frame.RightTop(), frame.RightBottom(), color1);
// bottom side
AddLine(frame.RightBottom(), frame.LeftBottom(), color1);
// top side
AddLine(frame.LeftTop(), frame.RightTop(), color2);
EndLineArray();
}
void
TSectionEdit::DrawBorder()
{
rgb_color bgcolor = ViewColor();
rgb_color light = tint_color(bgcolor, B_LIGHTEN_MAX_TINT);
rgb_color dark = tint_color(bgcolor, B_DARKEN_1_TINT);
rgb_color darker = tint_color(bgcolor, B_DARKEN_3_TINT);
SetHighColor(light);
SetLowColor(dark);
BRect bounds(Bounds());
Draw3DFrame(bounds, true);
StrokeLine(bounds.LeftBottom(), bounds.LeftBottom(), B_SOLID_LOW);
bounds.InsetBy(1, 1);
bounds.right -= kArrowAreaWidth;
fShowFocus = (IsFocus() && Window() && Window()->IsActive());
if (fShowFocus) {
rgb_color navcolor = keyboard_navigation_color();
SetHighColor(navcolor);
StrokeRect(bounds);
} else {
// draw border thickening (erase focus)
SetHighColor(darker);
SetLowColor(bgcolor);
Draw3DFrame(bounds, false);
}
be_control_look->DrawBorder(this, bounds, updateRect, ViewColor(),
B_FANCY_BORDER, fShowFocus ? BControlLook::B_FOCUSED : 0);
// draw up/down control
SetHighColor(light);
bounds.left = bounds.right +1;
bounds.right = Bounds().right -1;
fUpRect.Set(bounds.left +3, bounds.top +2, bounds.right, bounds.bottom /2.0);
fDownRect = fUpRect.OffsetByCopy(0, fUpRect.Height()+2);
if (fUpArrow)
DrawBitmap(fUpArrow, fUpRect.LeftTop());
if (fDownArrow)
DrawBitmap(fDownArrow, fDownRect.LeftTop());
Draw3DFrame(bounds, false);
SetHighColor(dark);
StrokeLine(bounds.LeftBottom(), bounds.RightBottom());
StrokeLine(bounds.RightBottom(), bounds.RightTop());
SetHighColor(light);
StrokeLine(bounds.RightTop(), bounds.RightTop());
bounds.left = bounds.right - kArrowAreaWidth;
bounds.right = Bounds().right - 2;
fUpRect.Set(bounds.left + 3, bounds.top + 2, bounds.right,
bounds.bottom / 2.0);
fDownRect = fUpRect.OffsetByCopy(0, fUpRect.Height() + 2);
BPoint middle(floorf(fUpRect.left + fUpRect.Width() / 2), fUpRect.top + 1);
BPoint left(fUpRect.left + 3, fUpRect.bottom - 1);
BPoint right(left.x + 2 * (middle.x - left.x), fUpRect.bottom - 1);
SetPenSize(2);
if (updateRect.Intersects(fUpRect)) {
FillRect(fUpRect, B_SOLID_LOW);
BeginLineArray(2);
AddLine(left, middle, HighColor());
AddLine(middle, right, HighColor());
EndLineArray();
}
if (updateRect.Intersects(fDownRect)) {
middle.y = fDownRect.bottom - 1;
left.y = right.y = fDownRect.top + 1;
FillRect(fDownRect, B_SOLID_LOW);
BeginLineArray(2);
AddLine(left, middle, HighColor());
AddLine(middle, right, HighColor());
EndLineArray();
}
SetPenSize(1);
}
float
TSectionEdit::SeperatorWidth() const
TSectionEdit::SeparatorWidth() const
{
return 0.0f;
}

View File

@ -22,8 +22,8 @@ class TSection {
public:
TSection(BRect frame)
: fFrame(frame) {}
BRect Bounds() const
BRect Bounds() const
{
BRect frame(fFrame);
return frame.OffsetByCopy(B_ORIGIN);
@ -31,7 +31,7 @@ class TSection {
void SetFrame(BRect frame)
{ fFrame = frame; }
BRect Frame() const
{ return fFrame; }
@ -44,50 +44,47 @@ class TSectionEdit: public BControl {
public:
TSectionEdit(BRect frame, const char *name, uint32 sections);
virtual ~TSectionEdit();
virtual void AttachedToWindow();
virtual void Draw(BRect updateRect);
virtual void MouseDown(BPoint point);
virtual void MakeFocus(bool focused = true);
virtual void KeyDown(const char *bytes, int32 numBytes);
uint32 CountSections() const;
int32 FocusIndex() const;
BRect SectionArea() const;
protected:
virtual void InitView();
//hooks
virtual void DrawBorder();
// hooks
virtual void DrawBorder(const BRect& updateRect);
virtual void DrawSection(uint32 index, bool isFocus) {}
virtual void DrawSeperator(uint32 index) {}
virtual void Draw3DFrame(BRect frame, bool inset);
virtual void DrawSeparator(uint32 index) {}
virtual void SectionFocus(uint32 index) {}
virtual void SectionChange(uint32 index, uint32 value) {}
virtual void SetSections(BRect area) {}
virtual float SeperatorWidth() const;
virtual float SeparatorWidth() const;
virtual void DoUpPress() {}
virtual void DoDownPress() {}
virtual void DispatchMessage();
virtual void BuildDispatch(BMessage *message) = 0;
protected:
BBitmap *fUpArrow;
BBitmap *fDownArrow;
BList *fSectionList;
BRect fUpRect;
BRect fDownRect;
BRect fSectionArea;
int32 fFocus;
uint32 fSectionCount;
uint32 fHoldValue;
bool fShowFocus;
};