diff --git a/headers/os/interface/ColorControl.h b/headers/os/interface/ColorControl.h index c5b0cc5af8..6d93ed7d43 100644 --- a/headers/os/interface/ColorControl.h +++ b/headers/os/interface/ColorControl.h @@ -1,169 +1,117 @@ -/******************************************************************************* -/ -/ File: ColorControl.h -/ -/ Description: BColorControl displays a palette of selectable colors. -/ -/ Copyright 1996-98, Be Incorporated, All Rights Reserved -/ -*******************************************************************************/ - - +/* + * Copyright 2005, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ #ifndef _COLOR_CONTROL_H #define _COLOR_CONTROL_H -#include + #include class BBitmap; -/*------------------------------------------------------------*/ -/*----- layout options for the color control -----------------*/ enum color_control_layout { - B_CELLS_4x64 = 4, - B_CELLS_8x32 = 8, - B_CELLS_16x16 = 16, - B_CELLS_32x8 = 32, - B_CELLS_64x4 = 64 + B_CELLS_4x64 = 4, + B_CELLS_8x32 = 8, + B_CELLS_16x16 = 16, + B_CELLS_32x8 = 32, + B_CELLS_64x4 = 64, }; class BTextControl; -/*----------------------------------------------------------------*/ -/*----- BColorControl class --------------------------------------*/ class BColorControl : public BControl { -public: - BColorControl( BPoint start, - color_control_layout layout, - float cell_size, - const char *name, - BMessage *message = NULL, - bool use_offscreen = false); -virtual ~BColorControl(); + public: + BColorControl(BPoint start, color_control_layout layout, + float cellSize, const char* name, + BMessage* message = NULL, bool useOffscreen = false); + BColorControl(BMessage* archive); + virtual ~BColorControl(); - BColorControl(BMessage *data); -static BArchivable *Instantiate(BMessage *data); -virtual status_t Archive(BMessage *data, bool deep = true) const; + static BArchivable* Instantiate(BMessage* archive); + virtual status_t Archive(BMessage* archive, bool deep = true) const; -virtual void SetValue(int32 color_value); - void SetValue(rgb_color color); - rgb_color ValueAsColor(); + virtual void SetValue(int32 color_value); + void SetValue(rgb_color color); + rgb_color ValueAsColor(); -virtual void SetEnabled(bool state); + virtual void SetEnabled(bool state); -virtual void AttachedToWindow(); -virtual void MessageReceived(BMessage *msg); -virtual void Draw(BRect updateRect); -virtual void MouseDown(BPoint where); -virtual void KeyDown(const char *bytes, int32 numBytes); + virtual void AttachedToWindow(); + virtual void MessageReceived(BMessage* message); + virtual void Draw(BRect updateRect); + virtual void MouseDown(BPoint where); + virtual void KeyDown(const char* bytes, int32 numBytes); -virtual void SetCellSize(float size); - float CellSize() const; -virtual void SetLayout(color_control_layout layout); + virtual void SetCellSize(float size); + float CellSize() const; + virtual void SetLayout(color_control_layout layout); color_control_layout Layout() const; -virtual void WindowActivated(bool state); -virtual void MouseUp(BPoint pt); -virtual void MouseMoved(BPoint pt, uint32 code, const BMessage *msg); -virtual void DetachedFromWindow(); -virtual void GetPreferredSize(float *width, float *height); -virtual void ResizeToPreferred(); -virtual status_t Invoke(BMessage *msg = NULL); -virtual void FrameMoved(BPoint new_position); -virtual void FrameResized(float new_width, float new_height); + virtual void WindowActivated(bool state); + virtual void MouseUp(BPoint point); + virtual void MouseMoved(BPoint point, uint32 code, + const BMessage* dragMessage); + virtual void DetachedFromWindow(); + virtual void GetPreferredSize(float* _width, float* _height); + virtual void ResizeToPreferred(); + virtual status_t Invoke(BMessage* message = NULL); + virtual void FrameMoved(BPoint newPosition); + virtual void FrameResized(float newWidth, float newHeight); -virtual BHandler *ResolveSpecifier(BMessage *msg, - int32 index, - BMessage *specifier, - int32 form, - const char *property); -virtual status_t GetSupportedSuites(BMessage *data); + virtual BHandler* ResolveSpecifier(BMessage* message, int32 index, + BMessage* specifier, int32 what, + const char* property); + virtual status_t GetSupportedSuites(BMessage* data); -virtual void MakeFocus(bool state = true); -virtual void AllAttached(); -virtual void AllDetached(); + virtual void MakeFocus(bool state = true); + virtual void AllAttached(); + virtual void AllDetached(); + private: + virtual status_t Perform(perform_code d, void *arg); + // this can be made public again if needed -/*----- Private or reserved -----------------------------------------*/ -virtual status_t Perform(perform_code d, void *arg); + virtual void _ReservedColorControl1(); + virtual void _ReservedColorControl2(); + virtual void _ReservedColorControl3(); + virtual void _ReservedColorControl4(); -private: + BColorControl& operator=(const BColorControl &other); -virtual void _ReservedColorControl1(); -virtual void _ReservedColorControl2(); -virtual void _ReservedColorControl3(); -virtual void _ReservedColorControl4(); + void _InitData(color_control_layout layout, + float size, bool useOffscreen, + BMessage* archive = NULL); + void _LayoutView(); + void _UpdateOffscreen(BRect update); + void _DrawColorArea(BView* target, BRect update); + void _ColorRamp(BRect rect, BView* target, + rgb_color baseColor, int16 flag, + bool focused); - BColorControl &operator=(const BColorControl &); + private: + float fCellSize; + int32 fRows; + int32 fColumns; - void LayoutView(bool calc_frame); - void UpdateOffscreen(); - void UpdateOffscreen(BRect update); - void DrawColorArea(BView *target, BRect update); - void ColorRamp( BRect r, - BRect where, - BView *target, - rgb_color c, - int16 flag, - bool focused); - void KbAdjustColor(uint32 key); - bool key_down32(uint32 key); - bool key_down8(uint32 key); -static BRect CalcFrame( BPoint start, - color_control_layout layout, - int32 size); - void InitData( color_control_layout layout, - float size, - bool use_offscreen, - BMessage *data = NULL); - void DoMouseMoved(BPoint pt); - void DoMouseUp(BPoint pt); + BTextControl* fRedText; + BTextControl* fGreenText; + BTextControl* fBlueText; - float fCellSize; - int32 fRows; - int32 fColumns; + BBitmap* fBitmap; + BView* fOffscreenView; - struct track_state { - int32 orig_color; - int32 cur_color; - int32 prev_color; - int32 bar_index; - BRect active_area; - BRect r; - rgb_color rgb; - color_space cspace; - }; - - BTextControl *fRedText; - BTextControl *fGreenText; - BTextControl *fBlueText; - BBitmap *fBitmap; - BView *fOffscreenView; - color_space fLastMode; - float fRound; - int32 fFocusedComponent; - int32 fCachedIndex; - uint32 _reserved[3]; - track_state *fTState; - bool fUnused; // fTracking; - bool fFocused; - bool fRetainCache; - bool fFastSet; + int32 fFocusedComponent; + uint32 _reserved[8]; }; -/*------------------------------------------------------------*/ -/*----- inline functions -------------------------------------*/ - -inline void BColorControl::SetValue(rgb_color color) +inline void +BColorControl::SetValue(rgb_color color) { - /* OK, no private parts */ int32 c = (color.red << 24) + (color.green << 16) + (color.blue << 8); SetValue(c); } -/*-------------------------------------------------------------*/ -/*-------------------------------------------------------------*/ - #endif /* _COLOR_CONTROL_H */ diff --git a/src/kits/interface/ColorControl.cpp b/src/kits/interface/ColorControl.cpp index 0952a44ee6..6929230e7c 100644 --- a/src/kits/interface/ColorControl.cpp +++ b/src/kits/interface/ColorControl.cpp @@ -27,28 +27,28 @@ const uint32 U_COLOR_CONTROL_GREEN_CHANGED_MSG = 'CCGC'; const uint32 U_COLOR_CONTROL_BLUE_CHANGED_MSG = 'CCBC'; -BColorControl::BColorControl(BPoint leftTop, color_control_layout matrix, +BColorControl::BColorControl(BPoint leftTop, color_control_layout layout, float cellSize, const char *name, BMessage *message, bool bufferedDrawing) : BControl(BRect(leftTop, leftTop), name, NULL, message, B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW | B_NAVIGABLE) { - InitData(matrix, cellSize, bufferedDrawing, NULL); + _InitData(layout, cellSize, bufferedDrawing, NULL); } -BColorControl::BColorControl(BMessage *archive) +BColorControl::BColorControl(BMessage* archive) : BControl(archive) { int32 layout; - bool use_offscreen; + float cellSize; + bool useOffscreen; archive->FindInt32("_layout", &layout); - SetLayout((color_control_layout)layout); + archive->FindFloat("_csize", &cellSize); + archive->FindBool("_use_off", &useOffscreen); - archive->FindFloat("_csize", &fCellSize); - - archive->FindBool("_use_off", &use_offscreen); + _InitData((color_control_layout)layout, cellSize, useOffscreen, archive); } @@ -57,6 +57,125 @@ BColorControl::~BColorControl() } +void +BColorControl::_InitData(color_control_layout layout, float size, + bool useOffscreen, BMessage* archive) +{ + BRect bounds(Bounds()); + + fColumns = layout; + fRows = 256 / fColumns; + fCellSize = size; + + BScreen screen(Window()); + + if (useOffscreen) { + fBitmap = new BBitmap(bounds, B_RGB32, true, false); + fOffscreenView = new BView(bounds, "off_view", 0, 0); + + fBitmap->Lock(); + fBitmap->AddChild(fOffscreenView); + fBitmap->Unlock(); + } else { + fBitmap = NULL; + fOffscreenView = NULL; + } + + if (archive) { + int32 value = 0; + archive->FindInt32("_val", &value); + + SetValue(value); + + 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 + + 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++) + fRedText->TextView()->DisallowChar(i); + for (int32 i = '0'; i <= '9'; i++) + fRedText->TextView()->AllowChar(i); + fRedText->TextView()->SetMaxBytes(3); + + // green + + rect.OffsetBy(0.0f, offset); + 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++) + fGreenText->TextView()->DisallowChar(i); + for (int32 i = '0'; i <= '9'; i++) + fGreenText->TextView()->AllowChar(i); + fGreenText->TextView()->SetMaxBytes(3); + + // blue + + rect.OffsetBy(0.0f, offset); + 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++) + fBlueText->TextView()->DisallowChar(i); + for (int32 i = '0'; i <= '9'; i++) + fBlueText->TextView()->AllowChar(i); + fBlueText->TextView()->SetMaxBytes(3); + + _LayoutView(); + + AddChild(fRedText); + AddChild(fGreenText); + AddChild(fBlueText); + } +} + + +void +BColorControl::_LayoutView() +{ + 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; + } + + ResizeTo(rect.Width() + fRedText->Bounds().Width(), rect.Height()); + + 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); + + y += offset; + fGreenText->MoveTo(rect.right + 1, y); + + y += offset; + fBlueText->MoveTo(rect.right + 1, y); +} + + BArchivable * BColorControl::Instantiate(BMessage *archive) { @@ -83,15 +202,10 @@ BColorControl::Archive(BMessage *archive, bool deep) const void BColorControl::SetValue(int32 color) { - if (!fRetainCache) - fCachedIndex = -1; - - fRetainCache = false; - if (fBitmap) { if (fBitmap->Lock()) { if (!fOffscreenView) - UpdateOffscreen(); + _UpdateOffscreen(Bounds()); fBitmap->Unlock(); } } @@ -194,60 +308,138 @@ BColorControl::MessageReceived(BMessage *message) void BColorControl::Draw(BRect updateRect) { - if (fTState != NULL) - return; - if (fBitmap) { if (!fBitmap->Lock()) return; if (fOffscreenView->Bounds().Intersects(updateRect)) { - UpdateOffscreen(updateRect); + _UpdateOffscreen(updateRect); DrawBitmap(fBitmap, updateRect.LeftTop()); } fBitmap->Unlock(); - } else if (Bounds().Intersects(updateRect)) - DrawColorArea(this, updateRect); + } else + _DrawColorArea(this, updateRect); } void -BColorControl::MouseDown(BPoint point) +BColorControl::_DrawColorArea(BView* target, BRect update) { + BRegion region(update); + target->ConstrainClippingRegion(®ion); + + 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); + + // 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); + 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(rampRect, target, white, 0, false); + + rampRect.OffsetBy(0, rampSize); + _ColorRamp(rampRect, target, red, 0, false); + + rampRect.OffsetBy(0,rampSize); + _ColorRamp(rampRect, target, green, 0, false); + + rampRect.OffsetBy(0, rampSize); + _ColorRamp(rampRect, target, blue, 0, false); + + // Selectors rgb_color color = ValueAsColor(); + float x, y = rampSize * 1.5; - BRect rect(0.0f, 0.0f, fColumns * fCellSize, Bounds().bottom); - float rampsize = rect.bottom / 4; + target->SetDrawingMode(B_OP_OVER); + target->SetPenSize(2.0f); - uint8 shade = (unsigned char)max_c(0, - min_c((point.x - 2) * 255 / (rect.Width() - 4.0f), 255)); + 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)); - if (point.y - 2 < rampsize) { - color.red = color.green = color.blue = shade; - fFocusedComponent = 1; - } else if (point.y - 2 < (rampsize * 2)) { - color.red = shade; - fFocusedComponent = 2; - } else if (point.y - 2 < (rampsize * 3)) { - color.green = shade; - fFocusedComponent = 3; - } else { - color.blue = shade; - fFocusedComponent = 4; + y += rampSize; + + 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; + + 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); +} + + +void +BColorControl::_ColorRamp(BRect rect, BView* target, + rgb_color baseColor, int16 flag, bool focused) +{ + float width = rect.Width()+1; + rgb_color color; + + target->BeginLineArray(width); + + for (float i = 0; i <= width; i++) { + color.red = (uint8)(i * baseColor.red / width); + color.green = (uint8)(i * baseColor.green / width); + color.blue = (uint8)(i * baseColor.blue / width); + + target->AddLine(BPoint(rect.left + i, rect.top), + BPoint(rect.left + i, rect.bottom), color); } - SetValue(color); - Invoke(); - - MakeFocus(); - SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS); + target->EndLineArray(); } void -BColorControl::KeyDown(const char *bytes, int32 numBytes) +BColorControl::_UpdateOffscreen(BRect update) { + if (fBitmap->Lock()) { + update = update & fOffscreenView->Bounds(); + fOffscreenView->FillRect(update); + _DrawColorArea(fOffscreenView, update); + fOffscreenView->Sync(); + fBitmap->Unlock(); + } } @@ -255,6 +447,8 @@ void BColorControl::SetCellSize(float cellSide) { fCellSize = cellSide; + + ResizeToPreferred(); } @@ -290,21 +484,24 @@ BColorControl::SetLayout(color_control_layout layout) fRows = 4; break; } + + ResizeToPreferred(); + Invalidate(); } color_control_layout BColorControl::Layout() const { - if (fColumns == 4 &&fRows == 64) + if (fColumns == 4 && fRows == 64) return B_CELLS_4x64; - if (fColumns == 8 &&fRows == 32) + if (fColumns == 8 && fRows == 32) return B_CELLS_8x32; - if (fColumns == 16 &&fRows == 16) + if (fColumns == 16 && fRows == 16) return B_CELLS_16x16; - if (fColumns == 32 &&fRows == 8) + if (fColumns == 32 && fRows == 8) return B_CELLS_32x8; - if (fColumns == 64 &&fRows == 4) + if (fColumns == 64 && fRows == 4) return B_CELLS_64x4; return B_CELLS_32x8; @@ -318,6 +515,14 @@ BColorControl::WindowActivated(bool state) } +void +BColorControl::KeyDown(const char* bytes, int32 numBytes) +{ + // TODO: make this keyboard navigable! + BControl::KeyDown(bytes, numBytes); +} + + void BColorControl::MouseUp(BPoint point) { @@ -325,6 +530,39 @@ BColorControl::MouseUp(BPoint point) } +void +BColorControl::MouseDown(BPoint point) +{ + rgb_color color = ValueAsColor(); + + BRect rect(0.0f, 0.0f, fColumns * fCellSize, Bounds().bottom); + float rampsize = rect.bottom / 4; + + uint8 shade = (unsigned char)max_c(0, + min_c((point.x - 2) * 255 / (rect.Width() - 4.0f), 255)); + + if (point.y - 2 < rampsize) { + color.red = color.green = color.blue = shade; + fFocusedComponent = 1; + } else if (point.y - 2 < (rampsize * 2)) { + color.red = shade; + fFocusedComponent = 2; + } else if (point.y - 2 < (rampsize * 3)) { + color.green = shade; + fFocusedComponent = 3; + } else { + color.blue = shade; + fFocusedComponent = 4; + } + + SetValue(color); + Invoke(); + + MakeFocus(); + SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS); +} + + void BColorControl::MouseMoved(BPoint point, uint32 transit, const BMessage *message) @@ -381,6 +619,8 @@ void BColorControl::ResizeToPreferred() { BControl::ResizeToPreferred(); + + _LayoutView(); } @@ -459,325 +699,3 @@ BColorControl::operator=(const BColorControl &) { return *this; } - - -void -BColorControl::LayoutView(bool calcFrame) -{ - 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; - } - - ResizeTo(rect.Width() + fRedText->Bounds().Width(), rect.Height()); - - 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); - - y += offset; - fGreenText->MoveTo(rect.right + 1, y); - - y += offset; - fBlueText->MoveTo(rect.right + 1, y); -} - - -void -BColorControl::UpdateOffscreen() -{ -} - - -void -BColorControl::UpdateOffscreen(BRect update) -{ - if (fBitmap->Lock()) { - update = update & fOffscreenView->Bounds(); - fOffscreenView->FillRect(update); - DrawColorArea(fOffscreenView, update); - fOffscreenView->Sync(); - fBitmap->Unlock(); - } -} - - -void -BColorControl::DrawColorArea(BView *target, BRect update) -{ - BRegion region(update); - target->ConstrainClippingRegion(®ion); - - 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); - - // 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); - 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); - - ramprect.OffsetBy(0,rampsize); - ColorRamp(BRect(), ramprect, target, green, 0, false); - - ramprect.OffsetBy(0,rampsize); - ColorRamp(BRect(), ramprect, target, blue, 0, false); - - // Selectors - rgb_color color = ValueAsColor(); - float x, y = rampsize * 1.5; - - target->SetDrawingMode(B_OP_OVER); - 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; - - 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; - - 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); -} - - -void -BColorControl::ColorRamp(BRect r, BRect where, BView *target, rgb_color c, - int16 flag, bool focused) -{ - rgb_color color = {255, 255, 255, 255}; - float width = where.Width()+1; - - target->BeginLineArray(width); - - for (float i = 0; i <= width; i++) { - 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) -{ -} - - -bool -BColorControl::key_down32(uint32 key) -{ - return false; -} - - -bool -BColorControl::key_down8(uint32 key) -{ - return false; -} - - -BRect -BColorControl::CalcFrame(BPoint start, color_control_layout layout, - int32 size) -{ - BRect rect; - - switch (layout) { - 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) -{ - 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) { - fBitmap = new BBitmap(bounds, B_RGB32, true, false); - fOffscreenView = new BView(bounds, "off_view", 0, 0); - - fBitmap->Lock(); - fBitmap->AddChild(fOffscreenView); - fBitmap->Unlock(); - } else { - fBitmap = NULL; - fOffscreenView = NULL; - } - - fFocused = false; - fCachedIndex = -1; - fRetainCache = false; - - if (data) { - 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 - - 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++) - fRedText->TextView()->DisallowChar(i); - for (int32 i = '0'; i <= '9'; i++) - fRedText->TextView()->AllowChar(i); - fRedText->TextView()->SetMaxBytes(3); - - // green - - rect.OffsetBy(0.0f, offset); - 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++) - fGreenText->TextView()->DisallowChar(i); - for (int32 i = '0'; i <= '9'; i++) - fGreenText->TextView()->AllowChar(i); - fGreenText->TextView()->SetMaxBytes(3); - - // blue - - rect.OffsetBy(0.0f, offset); - 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++) - fBlueText->TextView()->DisallowChar(i); - for (int32 i = '0'; i <= '9'; i++) - fBlueText->TextView()->AllowChar(i); - fBlueText->TextView()->SetMaxBytes(3); - - LayoutView(false); - AddChild(fRedText); - AddChild(fGreenText); - AddChild(fBlueText); - } -} - - -void -BColorControl::DoMouseMoved(BPoint pt) -{ -} - - -void -BColorControl::DoMouseUp(BPoint pt) -{ -} -