* 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 "ValControlSegment.h"
#include "ValControl.h"
#include <Debug.h>
#include <cstdio>
__USE_CORTEX_NAMESPACE
ValControlSegment::ValControlSegment(underline_style underlineStyle)
:BView(BRect(), "ValControlSegment", B_FOLLOW_LEFT|B_FOLLOW_TOP,
B_WILL_DRAW|B_FRAME_EVENTS),
fDragScaleFactor(fDefDragScaleFactor),
fLastClickTime(0LL),
fUnderlineStyle(underlineStyle),
fXUnderlineLeft(0.0),
fXUnderlineRight(0.0)
{
_Init();
}
ValControlSegment::~ValControlSegment()
{
}
void
ValControlSegment::_Init()
{
}
const double ValControlSegment::fDefDragScaleFactor = 4;
ValControl*
ValControlSegment::parent() const
{
return dynamic_cast<ValControl*>(Parent());
}
void
ValControlSegment::trackMouse(BPoint point, track_flags flags)
{
fTrackFlags = flags;
fPrevPoint = point;
setTracking(true);
SetMouseEventMask(B_POINTER_EVENTS,
B_LOCK_WINDOW_FOCUS | B_NO_POINTER_HISTORY);
}
void
ValControlSegment::setTracking(bool tracking)
{
fTracking = tracking;
}
bool
ValControlSegment::isTracking() const
{
return fTracking;
}
double
ValControlSegment::dragScaleFactor() const
{
return fDragScaleFactor;
}
void
ValControlSegment::sizeUnderline(float* outLeft, float* outRight)
{
*outLeft = 0.0;
*outRight = Bounds().right;
}
void
ValControlSegment::AttachedToWindow()
{
SetViewColor(parent()->ViewColor());
}
void
ValControlSegment::Draw(BRect updateRect)
{
if (fUnderlineStyle == NO_UNDERLINE)
return;
float fY = parent()->baselineOffset() + 1;
rgb_color white = {255,255,255,255};
rgb_color blue = {0,0,255,255};
rgb_color gray = {140,140,140,255};
rgb_color old = HighColor();
SetLowColor(white);
if(parent()->IsEnabled() && parent()->IsFocus())
SetHighColor(blue);
else
SetHighColor(gray);
if (fXUnderlineRight <= fXUnderlineLeft)
sizeUnderline(&fXUnderlineLeft, &fXUnderlineRight);
StrokeLine(BPoint(fXUnderlineLeft, fY),
BPoint(fXUnderlineRight, fY),
(fUnderlineStyle == DOTTED_UNDERLINE) ? B_MIXED_COLORS :
B_SOLID_HIGH);
SetHighColor(old);
}
void
ValControlSegment::FrameResized(float width, float height)
{
_Inherited::FrameResized(width, height);
sizeUnderline(&fXUnderlineLeft, &fXUnderlineRight);
}
void
ValControlSegment::MouseDown(BPoint point)
{
if (!parent()->IsEnabled())
return;
parent()->MakeFocus();
uint32 nButton;
GetMouse(&point, &nButton);
if (!(nButton & B_PRIMARY_MOUSE_BUTTON))
return;
bigtime_t doubleClickInterval;
if (get_click_speed(&doubleClickInterval) < B_OK) {
PRINT(("* ValControlSegment::MouseDown():\n"
" get_click_speed() failed."));
return;
}
bigtime_t now = system_time();
if (now - fLastClickTime < doubleClickInterval) {
if(isTracking()) {
setTracking(false);
mouseReleased();
}
parent()->showEditField();
fLastClickTime = 0LL;
return;
}
else
fLastClickTime = now;
trackMouse(point, TRACK_VERTICAL);
}
void
ValControlSegment::MouseMoved(BPoint point, uint32 transit,
const BMessage* dragMessage)
{
if (!parent()->IsEnabled())
return;
if (!isTracking() || fPrevPoint == point)
return;
float fYDelta = fTrackFlags & TRACK_VERTICAL ?
point.y - fPrevPoint.y : 0.0;
float fRemaining = handleDragUpdate(fYDelta);
fPrevPoint = point;
fPrevPoint.y -= fRemaining;
}
void
ValControlSegment::MouseUp(BPoint point)
{
if (isTracking()) {
setTracking(false);
mouseReleased();
}
}
void
ValControlSegment::MessageReceived(BMessage* message)
{
switch(message->what) {
default:
_Inherited::MessageReceived(message);
}
}
ValControlSegment::ValControlSegment(BMessage* archive)
: BView(archive)
{
_Init();
archive->FindInt32("ulStyle", (int32*)&fUnderlineStyle);
}
status_t
ValControlSegment::Archive(BMessage* archive, bool deep) const
{
_Inherited::Archive(archive, deep);
archive->AddInt32("ulStyle", fUnderlineStyle);
return B_OK;
}