* Copyright (c) 1999-2000, Eric Moon.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions, and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions, and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ValControlDigitSegment.h"
#include "ValControl.h"
#include "NumericValControl.h"
#include <Debug.h>
#include <math.h>
#include <stdlib.h>
#include <cstdio>
__USE_CORTEX_NAMESPACE
const float ValControlDigitSegment::s_widthTrim = -2;
const BFont* ValControlDigitSegment::s_cachedFont = 0;
float ValControlDigitSegment::s_cachedDigitWidth = 0.0;
ValControlDigitSegment::ValControlDigitSegment(
uint16 digitCount,
int16 scaleFactor,
bool negativeVisible,
display_flags flags) :
ValControlSegment(SOLID_UNDERLINE),
m_digitCount(digitCount),
m_value(0),
m_negative(false),
m_scaleFactor(scaleFactor),
m_font(0),
m_yOffset(0.0),
m_minusSignWidth(0.0),
m_digitPadding(0.0),
m_flags(flags),
m_negativeVisible(negativeVisible) {}
ValControlDigitSegment::~ValControlDigitSegment() {}
uint16 ValControlDigitSegment::digitCount() const {
return m_digitCount;
}
int16 ValControlDigitSegment::scaleFactor() const {
return m_scaleFactor;
}
int64 ValControlDigitSegment::value() const {
return m_value;
}
void ValControlDigitSegment::setValue(
int64 value,
bool negative) {
if(
value == m_value &&
m_negative == negative)
return;
m_value = value;
m_negative = negative;
Invalidate();
}
ValCtrlLayoutEntry ValControlDigitSegment::makeLayoutEntry() {
return ValCtrlLayoutEntry(this, ValCtrlLayoutEntry::SEGMENT_ENTRY);
}
float ValControlDigitSegment::handleDragUpdate(
float distance) {
int64 units = (int64)(distance / dragScaleFactor());
float remaining = distance;
if(units) {
remaining = fmod(distance, dragScaleFactor());
NumericValControl* numericParent = dynamic_cast<NumericValControl*>(parent());
ASSERT(numericParent);
numericParent->offsetSegmentValue(this, units);
}
return remaining;
}
void ValControlDigitSegment::mouseReleased() {
}
void ValControlDigitSegment::Draw(BRect updateRect) {
ASSERT(m_font);
BBitmap* pBufferBitmap = parent()->backBuffer();
BView* pView = pBufferBitmap ? parent()->backBufferView() : this;
if(pBufferBitmap)
pBufferBitmap->Lock();
rgb_color black = {0,0,0,255};
rgb_color disabled = tint_color(black, B_LIGHTEN_2_TINT);
rgb_color viewColor = ViewColor();
BRect b = Bounds();
float digitWidth = MaxDigitWidth(m_font);
BPoint p;
p.x = b.right - digitWidth;
p.y = m_yOffset;
pView->SetFont(m_font);
if(parent()->IsEnabled()) {
pView->SetHighColor(black);
} else {
pView->SetHighColor(disabled);
}
pView->SetLowColor(viewColor);
int16 digit;
int64 cur = abs(m_value);
for(digit = 0;
digit < m_digitCount;
digit++, cur /= 10, p.x -= (digitWidth+m_digitPadding)) {
uint8 digitValue = (uint8)(cur % 10);
if(digit && !(m_flags & ZERO_FILL) && !cur)
break;
pView->DrawChar('0' + digitValue, p);
}
if(m_negative) {
p.x += (digitWidth-m_minusSignWidth);
pView->DrawChar('-', p);
}
if(pBufferBitmap) {
pView->Sync();
DrawBitmap(parent()->backBuffer(), b, b);
pBufferBitmap->Unlock();
}
_inherited::Draw(updateRect);
}
void ValControlDigitSegment::GetPreferredSize(float* pWidth, float* pHeight) {
*pWidth = prefWidth();
*pHeight = prefHeight();
}
float ValControlDigitSegment::prefWidth() const {
ASSERT(m_font);
float width = (m_digitCount*MaxDigitWidth(m_font)) +
((m_digitCount - 1)*m_digitPadding);
if(m_negativeVisible)
width += (m_minusSignWidth + m_digitPadding);
return width;
}
float ValControlDigitSegment::prefHeight() const {
ASSERT(m_font);
return m_fontHeight.ascent + m_fontHeight.descent + m_fontHeight.leading;
}
void ValControlDigitSegment::fontChanged(
const BFont* font) {
m_font = font;
m_font->GetHeight(&m_fontHeight);
ASSERT(parent());
m_yOffset = parent()->baselineOffset();
char c = '-';
m_minusSignWidth = m_font->StringWidth(&c, 1) + s_widthTrim;
m_digitPadding = parent()->segmentPadding();
}
void ValControlDigitSegment::MessageReceived(BMessage* pMsg) {
double fVal;
switch(pMsg->what) {
case ValControl::M_SET_VALUE:
pMsg->FindDouble("value", &fVal);
setValue((int64)fVal, fVal < 0);
break;
case ValControl::M_GET_VALUE: {
BMessage reply(ValControl::M_VALUE);
reply.AddDouble("value", value());
pMsg->SendReply(&reply);
break;
}
}
}
ValControlDigitSegment::ValControlDigitSegment(BMessage* pArchive) :
ValControlSegment(pArchive),
m_font(0),
m_digitPadding(0.0) {
status_t err __attribute__((unused)) = pArchive->FindInt16("digits", (int16*)&m_digitCount);
ASSERT(err == B_OK);
err = pArchive->FindInt64("value", &m_value);
ASSERT(err == B_OK);
err = pArchive->FindInt16("scaleFactor", &m_scaleFactor);
ASSERT(err == B_OK);
}
status_t ValControlDigitSegment::Archive(BMessage* pArchive, bool bDeep) const{
_inherited::Archive(pArchive, bDeep);
pArchive->AddInt16("digits", m_digitCount);
pArchive->AddInt64("value", m_value);
pArchive->AddInt16("scaleFactor", m_scaleFactor);
return B_OK;
}
BArchivable* ValControlDigitSegment::Instantiate(BMessage* pArchive) {
if(validate_instantiation(pArchive, "ValControlDigitSegment"))
return new ValControlDigitSegment(pArchive);
else
return 0;
}
float ValControlDigitSegment::MaxDigitWidth(const BFont* pFont) {
ASSERT(pFont);
if(s_cachedFont == pFont)
return s_cachedDigitWidth;
s_cachedFont = pFont;
float fMax = 0.0;
for(char c = '0'; c <= '9'; c++) {
float fWidth = pFont->StringWidth(&c, 1);
if(fWidth > fMax)
fMax = fWidth;
}
s_cachedDigitWidth = ceil(fMax + s_widthTrim);
return s_cachedDigitWidth;
}