Files
haiku-beta6/src/kits/interface/ColorControl.cpp
T

784 lines
15 KiB
C++
Raw Normal View History

/*
* Copyright 2001-2005, Haiku Inc.
* Distributed under the terms of the MIT License.
*
* Authors:
* Marc Flerackers ([email protected])
* Axel Dörfler, [email protected]
*/
/** BColorControl displays a palette of selectable colors. */
2003-06-16 08:17:19 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <ColorControl.h>
#include <Bitmap.h>
#include <TextControl.h>
#include <Screen.h>
#include <Window.h>
#include <Errors.h>
#include <Region.h>
2003-06-16 08:17:19 +00:00
const uint32 U_COLOR_CONTROL_RED_CHANGED_MSG = 'CCRC';
const uint32 U_COLOR_CONTROL_GREEN_CHANGED_MSG = 'CCGC';
const uint32 U_COLOR_CONTROL_BLUE_CHANGED_MSG = 'CCBC';
2003-06-16 08:17:19 +00:00
BColorControl::BColorControl(BPoint leftTop, color_control_layout matrix,
float cellSize, const char *name, BMessage *message,
bool bufferedDrawing)
: BControl(BRect(leftTop, leftTop), name, NULL, message,
2003-06-16 08:17:19 +00:00
B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW | B_NAVIGABLE)
{
InitData(matrix, cellSize, bufferedDrawing, NULL);
}
2003-06-16 08:17:19 +00:00
BColorControl::BColorControl(BMessage *archive)
: BControl(archive)
2003-06-16 08:17:19 +00:00
{
int32 layout;
bool use_offscreen;
archive->FindInt32("_layout", &layout);
SetLayout((color_control_layout)layout);
archive->FindFloat("_csize", &fCellSize);
archive->FindBool("_use_off", &use_offscreen);
}
2003-06-16 08:17:19 +00:00
BColorControl::~BColorControl()
{
}
BArchivable *
BColorControl::Instantiate(BMessage *archive)
2003-06-16 08:17:19 +00:00
{
if ( validate_instantiation(archive, "BColorControl"))
return new BColorControl(archive);
return NULL;
2003-06-16 08:17:19 +00:00
}
status_t
BColorControl::Archive(BMessage *archive, bool deep) const
2003-06-16 08:17:19 +00:00
{
BControl::Archive(archive, deep);
archive->AddInt32("_layout", Layout());
archive->AddFloat("_csize", fCellSize);
archive->AddBool("_use_off", fOffscreenView != NULL);
return B_OK;
}
void
BColorControl::SetValue(int32 color)
2003-06-16 08:17:19 +00:00
{
if (!fRetainCache)
fCachedIndex = -1;
fRetainCache = false;
if (fBitmap) {
if (fBitmap->Lock()) {
2003-06-16 08:17:19 +00:00
if (!fOffscreenView)
UpdateOffscreen();
fBitmap->Unlock();
}
}
rgb_color c1 = ValueAsColor();
rgb_color c2;
c2.red = (color & 0xFF000000) >> 24;
c2.green = (color & 0x00FF0000) >> 16;
c2.blue = (color & 0x0000FF00) >> 8;
char string[4];
if (c1.red != c2.red) {
2003-06-16 08:17:19 +00:00
sprintf(string, "%d", c2.red);
fRedText->SetText(string);
}
if (c1.green != c2.green) {
2003-06-16 08:17:19 +00:00
sprintf(string, "%d", c2.green);
fGreenText->SetText(string);
}
if (c1.blue != c2.blue) {
2003-06-16 08:17:19 +00:00
sprintf(string, "%d", c2.blue);
fBlueText->SetText(string);
}
if (LockLooper()) {
2003-06-16 08:17:19 +00:00
Window()->UpdateIfNeeded();
UnlockLooper();
}
2003-06-16 08:17:19 +00:00
BControl::SetValue(color);
}
rgb_color
BColorControl::ValueAsColor()
2003-06-16 08:17:19 +00:00
{
int32 value = Value();
rgb_color color;
color.red = (value & 0xFF000000) >> 24;
color.green = (value & 0x00FF0000) >> 16;
color.blue = (value & 0x0000FF00) >> 8;
color.alpha = 255;
return color;
}
void
BColorControl::SetEnabled(bool enabled)
2003-06-16 08:17:19 +00:00
{
BControl::SetEnabled(enabled);
fRedText->SetEnabled(enabled);
fGreenText->SetEnabled(enabled);
fBlueText->SetEnabled(enabled);
}
void
BColorControl::AttachedToWindow()
2003-06-16 08:17:19 +00:00
{
if (Parent())
SetViewColor(Parent()->ViewColor());
else
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
2003-06-16 08:17:19 +00:00
BControl::AttachedToWindow();
fRedText->SetTarget(this);
fGreenText->SetTarget(this);
fBlueText->SetTarget(this);
}
void
BColorControl::MessageReceived(BMessage *message)
2003-06-16 08:17:19 +00:00
{
switch (message->what) {
2003-06-16 08:17:19 +00:00
case 'ccol':
{
rgb_color color;
2003-06-16 08:17:19 +00:00
color.red = strtol(fRedText->Text(), NULL, 10);
color.green = strtol(fGreenText->Text(), NULL, 10);
color.blue = strtol(fBlueText->Text(), NULL, 10);
SetValue(color);
Invoke();
break;
}
default:
BControl::MessageReceived(message);
}
}
void
BColorControl::Draw(BRect updateRect)
2003-06-16 08:17:19 +00:00
{
if (fTState != NULL)
return;
if (fBitmap) {
2003-06-16 08:17:19 +00:00
if (!fBitmap->Lock())
return;
if (fOffscreenView->Bounds().Intersects(updateRect)) {
2003-06-16 08:17:19 +00:00
UpdateOffscreen(updateRect);
DrawBitmap(fBitmap, updateRect.LeftTop());
}
fBitmap->Unlock();
} else if (Bounds().Intersects(updateRect))
2003-06-16 08:17:19 +00:00
DrawColorArea(this, updateRect);
}
void
BColorControl::MouseDown(BPoint point)
2003-06-16 08:17:19 +00:00
{
rgb_color color = ValueAsColor();
BRect rect(0.0f, 0.0f, fColumns * fCellSize, Bounds().bottom);
float rampsize = rect.bottom / 4;
2003-06-16 08:17:19 +00:00
uint8 shade = (unsigned char)max_c(0,
min_c((point.x - 2) * 255 / (rect.Width() - 4.0f), 255));
2003-06-16 08:17:19 +00:00
if (point.y - 2 < rampsize) {
2003-06-16 08:17:19 +00:00
color.red = color.green = color.blue = shade;
fFocusedComponent = 1;
} else if (point.y - 2 < (rampsize * 2)) {
2003-06-16 08:17:19 +00:00
color.red = shade;
fFocusedComponent = 2;
} else if (point.y - 2 < (rampsize * 3)) {
2003-06-16 08:17:19 +00:00
color.green = shade;
fFocusedComponent = 3;
} else {
2003-06-16 08:17:19 +00:00
color.blue = shade;
fFocusedComponent = 4;
}
SetValue(color);
Invoke();
MakeFocus();
SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS);
}
void
BColorControl::KeyDown(const char *bytes, int32 numBytes)
2003-06-16 08:17:19 +00:00
{
}
void
BColorControl::SetCellSize(float cellSide)
2003-06-16 08:17:19 +00:00
{
fCellSize = cellSide;
}
float
BColorControl::CellSize() const
2003-06-16 08:17:19 +00:00
{
return fCellSize;
}
void
BColorControl::SetLayout(color_control_layout layout)
2003-06-16 08:17:19 +00:00
{
switch (layout) {
2003-06-16 08:17:19 +00:00
case B_CELLS_4x64:
fColumns = 4;
fRows = 64;
break;
case B_CELLS_8x32:
fColumns = 8;
fRows = 32;
break;
case B_CELLS_16x16:
fColumns = 16;
fRows = 16;
break;
case B_CELLS_32x8:
fColumns = 32;
fRows = 8;
break;
case B_CELLS_64x4:
fColumns = 64;
fRows = 4;
break;
}
}
color_control_layout
BColorControl::Layout() const
2003-06-16 08:17:19 +00:00
{
if (fColumns == 4 &&fRows == 64)
return B_CELLS_4x64;
if (fColumns == 8 &&fRows == 32)
return B_CELLS_8x32;
if (fColumns == 16 &&fRows == 16)
return B_CELLS_16x16;
if (fColumns == 32 &&fRows == 8)
return B_CELLS_32x8;
if (fColumns == 64 &&fRows == 4)
return B_CELLS_64x4;
return B_CELLS_32x8;
}
void
BColorControl::WindowActivated(bool state)
2003-06-16 08:17:19 +00:00
{
BControl::WindowActivated(state);
}
void
BColorControl::MouseUp(BPoint point)
2003-06-16 08:17:19 +00:00
{
fFocusedComponent = 0;
}
void
BColorControl::MouseMoved(BPoint point, uint32 transit,
const BMessage *message)
2003-06-16 08:17:19 +00:00
{
if (fFocusedComponent == 0)
return;
2003-06-16 08:17:19 +00:00
rgb_color color = ValueAsColor();
2003-06-16 08:17:19 +00:00
BRect rect(0.0f, 0.0f, fColumns * fCellSize, Bounds().bottom);
2003-06-16 08:17:19 +00:00
uint8 shade = (unsigned char)max_c(0,
min_c((point.x - 2) * 255 / (rect.Width() - 4.0f), 255));
2003-06-16 08:17:19 +00:00
switch (fFocusedComponent) {
case 1:
color.red = color.green = color.blue = shade;
break;
case 2:
color.red = shade;
break;
case 3:
color.green = shade;
break;
case 4:
color.blue = shade;
break;
2003-06-16 08:17:19 +00:00
}
SetValue(color);
Invoke();
2003-06-16 08:17:19 +00:00
}
void
BColorControl::DetachedFromWindow()
2003-06-16 08:17:19 +00:00
{
BControl::DetachedFromWindow();
}
void
BColorControl::GetPreferredSize(float *_width, float *_height)
2003-06-16 08:17:19 +00:00
{
if (_width)
*_width = fColumns * fCellSize + 4.0f + fRedText->Bounds().Width();
if (_height)
*_height = fBlueText->Frame().bottom;
2003-06-16 08:17:19 +00:00
}
void
BColorControl::ResizeToPreferred()
2003-06-16 08:17:19 +00:00
{
BControl::ResizeToPreferred();
}
status_t
BColorControl::Invoke(BMessage *msg)
2003-06-16 08:17:19 +00:00
{
return BControl::Invoke(msg);
}
void
BColorControl::FrameMoved(BPoint new_position)
2003-06-16 08:17:19 +00:00
{
BControl::FrameMoved(new_position);
}
void
BColorControl::FrameResized(float new_width, float new_height)
2003-06-16 08:17:19 +00:00
{
BControl::FrameResized(new_width, new_height);
}
BHandler *
BColorControl::ResolveSpecifier(BMessage *msg, int32 index,
BMessage *specifier, int32 form, const char *property)
2003-06-16 08:17:19 +00:00
{
return BControl::ResolveSpecifier(msg, index, specifier, form, property);
}
status_t
BColorControl::GetSupportedSuites(BMessage *data)
2003-06-16 08:17:19 +00:00
{
return BControl::GetSupportedSuites(data);
}
void
BColorControl::MakeFocus(bool state)
2003-06-16 08:17:19 +00:00
{
BControl::MakeFocus(state);
}
void
BColorControl::AllAttached()
2003-06-16 08:17:19 +00:00
{
BControl::AllAttached();
}
void
BColorControl::AllDetached()
2003-06-16 08:17:19 +00:00
{
BControl::AllDetached();
}
status_t
BColorControl::Perform(perform_code d, void *arg)
2003-06-16 08:17:19 +00:00
{
return B_ERROR;
}
2003-06-16 08:17:19 +00:00
void BColorControl::_ReservedColorControl1() {}
void BColorControl::_ReservedColorControl2() {}
void BColorControl::_ReservedColorControl3() {}
void BColorControl::_ReservedColorControl4() {}
BColorControl &
BColorControl::operator=(const BColorControl &)
2003-06-16 08:17:19 +00:00
{
return *this;
}
void
BColorControl::LayoutView(bool calcFrame)
2003-06-16 08:17:19 +00:00
{
BRect rect(0.0f, 0.0f, ceil(fColumns * fCellSize), ceil(fRows * fCellSize + 2));
if (rect.Height() < fBlueText->Frame().bottom) {
// adjust the height to fit
rect.bottom = fBlueText->Frame().bottom;
}
2003-06-16 08:17:19 +00:00
ResizeTo(rect.Width() + fRedText->Bounds().Width(), rect.Height());
2003-06-16 08:17:19 +00:00
float offset = floor(rect.bottom / 4);
float y = offset;
if (offset < fRedText->Bounds().Height() + 2) {
offset = fRedText->Bounds().Height() + 2;
y = 0;
}
fRedText->MoveTo(rect.right + 1, y);
2003-06-16 08:17:19 +00:00
y += offset;
fGreenText->MoveTo(rect.right + 1, y);
y += offset;
fBlueText->MoveTo(rect.right + 1, y);
2003-06-16 08:17:19 +00:00
}
void
BColorControl::UpdateOffscreen()
2003-06-16 08:17:19 +00:00
{
}
void
BColorControl::UpdateOffscreen(BRect update)
2003-06-16 08:17:19 +00:00
{
if (fBitmap->Lock()) {
2003-06-16 08:17:19 +00:00
update = update & fOffscreenView->Bounds();
fOffscreenView->FillRect(update);
DrawColorArea(fOffscreenView, update);
fOffscreenView->Sync();
fBitmap->Unlock();
}
}
void
BColorControl::DrawColorArea(BView *target, BRect update)
2003-06-16 08:17:19 +00:00
{
BRegion region(update);
target->ConstrainClippingRegion(&region);
BRect rect(0.0f, 0.0f, ceil(fColumns * fCellSize), Bounds().bottom);
rgb_color noTint = ui_color(B_PANEL_BACKGROUND_COLOR),
lightenmax = tint_color(noTint, B_LIGHTEN_MAX_TINT),
darken1 = tint_color(noTint, B_DARKEN_1_TINT),
darken4 = tint_color(noTint, B_DARKEN_4_TINT);
2003-06-16 08:17:19 +00:00
// First bevel
target->SetHighColor(darken1);
target->StrokeLine(rect.LeftBottom(), rect.LeftTop());
target->StrokeLine(rect.LeftTop(), rect.RightTop());
target->SetHighColor(lightenmax);
target->StrokeLine(BPoint(rect.left + 1.0f, rect.bottom), rect.RightBottom());
target->StrokeLine(rect.RightBottom(), BPoint(rect.right, rect.top + 1.0f));
rect.InsetBy(1.0f, 1.0f);
// Second bevel
target->SetHighColor(darken4);
target->StrokeLine(rect.LeftBottom(), rect.LeftTop());
target->StrokeLine(rect.LeftTop(), rect.RightTop());
target->SetHighColor(noTint);
2003-06-16 08:17:19 +00:00
target->StrokeLine(BPoint(rect.left + 1.0f, rect.bottom), rect.RightBottom());
target->StrokeLine(rect.RightBottom(), BPoint(rect.right, rect.top + 1.0f));
// Ramps
rgb_color white = {255, 255, 255, 255};
rgb_color red = {255, 0, 0, 255};
rgb_color green = {0, 255, 0, 255};
rgb_color blue = {0, 0, 255, 255};
rect.InsetBy(1.0f, 1.0f);
BRect ramprect(rect);
float rampsize = ramprect.Height() / 4.0;
ramprect.bottom = ramprect.top + rampsize;
ColorRamp(BRect(), ramprect, target, white, 0, false);
ramprect.OffsetBy(0,rampsize);
ColorRamp(BRect(), ramprect, target, red, 0, false);
2003-06-16 08:17:19 +00:00
ramprect.OffsetBy(0,rampsize);
ColorRamp(BRect(), ramprect, target, green, 0, false);
2003-06-16 08:17:19 +00:00
ramprect.OffsetBy(0,rampsize);
ColorRamp(BRect(), ramprect, target, blue, 0, false);
2003-06-16 08:17:19 +00:00
// Selectors
rgb_color color = ValueAsColor();
float x, y = rampsize * 1.5;
2003-06-16 08:17:19 +00:00
target->SetDrawingMode(B_OP_OVER);
2003-06-16 08:17:19 +00:00
target->SetPenSize(2.0f);
x = rect.left + color.red * (rect.Width() - 7) / 255;
target->SetHighColor(255, 255, 255);
target->StrokeEllipse(BRect(x, y, x + 4.0f, y + 4.0f));
y += rampsize;
2003-06-16 08:17:19 +00:00
x = rect.left + color.green * (rect.Width() - 7) / 255;
target->SetHighColor(255, 255, 255);
target->StrokeEllipse(BRect(x, y, x + 4.0f, y + 4.0f));
y += rampsize;
2003-06-16 08:17:19 +00:00
x = rect.left + color.blue * (rect.Width() - 7) / 255;
target->SetHighColor(255, 255, 255);
target->StrokeEllipse(BRect ( x, y, x + 4.0f, y + 4.0f));
target->SetPenSize(1.0f);
target->SetDrawingMode(B_OP_COPY);
target->ConstrainClippingRegion(NULL);
2003-06-16 08:17:19 +00:00
}
void
BColorControl::ColorRamp(BRect r, BRect where, BView *target, rgb_color c,
int16 flag, bool focused)
2003-06-16 08:17:19 +00:00
{
rgb_color color = {255, 255, 255, 255};
float width = where.Width()+1;
2003-06-16 08:17:19 +00:00
target->BeginLineArray(width);
for (float i = 0; i <= width; i++) {
2003-06-16 08:17:19 +00:00
color.red = (uint8)(i * c.red / width);
color.green = (uint8)(i * c.green / width);
color.blue = (uint8)(i * c.blue / width);
target->AddLine(BPoint(where.left + i, where.top),
BPoint(where.left + i, where.bottom),
color);
}
target->EndLineArray();
}
void
BColorControl::KbAdjustColor(uint32 key)
2003-06-16 08:17:19 +00:00
{
}
bool
BColorControl::key_down32(uint32 key)
2003-06-16 08:17:19 +00:00
{
return false;
}
bool
BColorControl::key_down8(uint32 key)
2003-06-16 08:17:19 +00:00
{
return false;
}
BRect
BColorControl::CalcFrame(BPoint start, color_control_layout layout,
int32 size)
2003-06-16 08:17:19 +00:00
{
BRect rect;
switch (layout) {
2003-06-16 08:17:19 +00:00
case B_CELLS_4x64:
rect.Set(0.0f, 0.0f, 4 * size + 4.0f,
64 * size + 4.0f);
break;
case B_CELLS_8x32:
rect.Set(0.0f, 0.0f, 8 * size + 4.0f,
32 * size + 4.0f);
break;
case B_CELLS_16x16:
rect.Set(0.0f, 0.0f, 16 * size + 4.0f,
16 * size + 4.0f);
break;
case B_CELLS_32x8:
rect.Set(0.0f, 0.0f, 32 * size + 4.0f,
8 * size + 4.0f);
break;
case B_CELLS_64x4:
rect.Set(0.0f, 0.0f, 64 * size + 4.0f,
4 * size + 4.0f);
break;
}
return rect;
}
void
BColorControl::InitData(color_control_layout layout, float size,
bool useOffscreen, BMessage *data)
2003-06-16 08:17:19 +00:00
{
BRect bounds(Bounds());
fColumns = layout;
fRows = 256 / fColumns;
fTState = NULL;
fCellSize = size;
fRound = 1.0f;
fFastSet = false;
BScreen screen(Window());
fLastMode = screen.ColorSpace();
if (useOffscreen) {
2003-06-16 08:17:19 +00:00
fBitmap = new BBitmap(bounds, B_RGB32, true, false);
fOffscreenView = new BView(bounds, "off_view", 0, 0);
fBitmap->Lock();
fBitmap->AddChild(fOffscreenView);
fBitmap->Unlock();
} else {
2003-06-16 08:17:19 +00:00
fBitmap = NULL;
fOffscreenView = NULL;
}
fFocused = false;
fCachedIndex = -1;
fRetainCache = false;
if (data) {
2003-06-16 08:17:19 +00:00
int32 _val = 0;
data->FindInt32("_val", &_val);
SetValue(_val);
fRedText = (BTextControl*)FindView("_red");
fGreenText = (BTextControl*)FindView("_green");
fBlueText = (BTextControl*)FindView("_blue");
} else {
BRect rect(0.0f, 0.0f, 70.0f, 15.0f);
float labelWidth = StringWidth("Green:") + 5;
rect.right = labelWidth + StringWidth("999") + 20;
// red
2003-06-16 08:17:19 +00:00
fRedText = new BTextControl(rect, "_red", "Red:", "0",
new BMessage('ccol'), B_FOLLOW_LEFT | B_FOLLOW_TOP,
B_WILL_DRAW | B_NAVIGABLE);
fRedText->SetDivider(labelWidth);
float offset = fRedText->Bounds().Height() + 2;
for (int32 i = 0; i < 256; i++)
2003-06-16 08:17:19 +00:00
fRedText->TextView()->DisallowChar(i);
for (int32 i = '0'; i <= '9'; i++)
2003-06-16 08:17:19 +00:00
fRedText->TextView()->AllowChar(i);
fRedText->TextView()->SetMaxBytes(3);
// green
2003-06-16 08:17:19 +00:00
rect.OffsetBy(0.0f, offset);
2003-06-16 08:17:19 +00:00
fGreenText = new BTextControl(rect, "_green", "Green:", "0",
new BMessage('ccol'), B_FOLLOW_LEFT | B_FOLLOW_TOP,
B_WILL_DRAW | B_NAVIGABLE);
fGreenText->SetDivider(labelWidth);
for (int32 i = 0; i < 256; i++)
2003-06-16 08:17:19 +00:00
fGreenText->TextView()->DisallowChar(i);
for (int32 i = '0'; i <= '9'; i++)
2003-06-16 08:17:19 +00:00
fGreenText->TextView()->AllowChar(i);
fGreenText->TextView()->SetMaxBytes(3);
// blue
2003-06-16 08:17:19 +00:00
rect.OffsetBy(0.0f, offset);
2003-06-16 08:17:19 +00:00
fBlueText = new BTextControl(rect, "_blue", "Blue:", "0",
new BMessage('ccol'), B_FOLLOW_LEFT | B_FOLLOW_TOP,
B_WILL_DRAW | B_NAVIGABLE);
fBlueText->SetDivider(labelWidth);
for (int32 i = 0; i < 256; i++)
2003-06-16 08:17:19 +00:00
fBlueText->TextView()->DisallowChar(i);
for (int32 i = '0'; i <= '9'; i++)
2003-06-16 08:17:19 +00:00
fBlueText->TextView()->AllowChar(i);
fBlueText->TextView()->SetMaxBytes(3);
LayoutView(false);
AddChild(fRedText);
AddChild(fGreenText);
AddChild(fBlueText);
}
}
void
BColorControl::DoMouseMoved(BPoint pt)
2003-06-16 08:17:19 +00:00
{
}
void
BColorControl::DoMouseUp(BPoint pt)
2003-06-16 08:17:19 +00:00
{
}