Cleanup & small fixes

* Style cleanup
 * Don't delete message in case they happen to be the passed in pointers.
This commit is contained in:
Stephan Aßmus 2012-05-12 13:48:54 +02:00
parent 4b140bad0d
commit f50e7b8dc5
2 changed files with 88 additions and 118 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2006, Haiku.
* Copyright 2006-2012, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
@ -8,6 +8,7 @@
#include "SwatchView.h"
#include <new>
#include <stdio.h>
#include <Bitmap.h>
@ -22,62 +23,35 @@
#include "support.h"
#include "support_ui.h"
#define DRAG_INIT_DIST 10.0
// constructor
SwatchView::SwatchView(const char* name, BMessage* message,
BHandler* target, rgb_color color,
float width, float height)
: BView(BRect(0.0, 0.0, width, height), name,
B_FOLLOW_NONE, B_WILL_DRAW),
fColor(color),
fTrackingStart(-1.0, -1.0),
fActive(false),
fDropInvokes(false),
fClickMessage(message),
fDroppedMessage(NULL),
fTarget(target),
fWidth(width),
fHeight(height)
SwatchView::SwatchView(const char* name, BMessage* message, BHandler* target,
rgb_color color, float width, float height)
:
BView(BRect(0.0, 0.0, width, height), name, B_FOLLOW_NONE, B_WILL_DRAW),
fColor(color),
fTrackingStart(-1.0, -1.0),
fActive(false),
fDropInvokes(false),
fClickMessage(message),
fDroppedMessage(NULL),
fTarget(target),
fWidth(width),
fHeight(height)
{
SetViewColor(B_TRANSPARENT_32_BIT);
SetHighColor(fColor);
}
// destructor
SwatchView::~SwatchView()
{
delete fClickMessage;
delete fDroppedMessage;
}
#if LIB_LAYOUT
// layoutprefs
minimax
SwatchView::layoutprefs()
{
if (fWidth > 6.0 && fHeight > 6.0) {
mpm.mini.x = mpm.maxi.x = fWidth;
mpm.mini.y = mpm.maxi.y = fHeight;
} else {
mpm.mini.x = 6.0;
mpm.maxi.x = 10000.0;
mpm.mini.y = 6.0;
mpm.maxi.y = 10000.0;
}
mpm.weight = 1.0;
return mpm;
}
// layout
BRect
SwatchView::layout(BRect frame)
{
MoveTo(frame.LeftTop());
ResizeTo(frame.Width(), frame.Height());
return Frame();
}
#endif // LIB_LAYOUT
inline void
blend_color(rgb_color& a, const rgb_color& b, float alpha)
@ -88,7 +62,7 @@ blend_color(rgb_color& a, const rgb_color& b, float alpha)
a.blue = (uint8)(b.blue * alphaInv + a.blue * alpha);
}
// Draw
void
SwatchView::Draw(BRect updateRect)
{
@ -148,27 +122,28 @@ SwatchView::Draw(BRect updateRect)
}
}
// MessageReceived
void
SwatchView::MessageReceived(BMessage* message)
{
switch (message->what) {
case B_PASTE: {
case B_PASTE:
{
rgb_color color;
if (restore_color_from_message(message,
color) >= B_OK) {
if (restore_color_from_message(message, color) == B_OK) {
SetColor(color);
_Invoke(fDroppedMessage);
}
break;
}
default:
BView::MessageReceived(message);
break;
}
}
// MouseDown
void
SwatchView::MouseDown(BPoint where)
{
@ -176,29 +151,30 @@ SwatchView::MouseDown(BPoint where)
fTrackingStart = where;
}
// MouseUp
void
SwatchView::MouseUp(BPoint where)
{
if (Bounds().Contains(where)
&& Bounds().Contains(fTrackingStart))
if (Bounds().Contains(where) && Bounds().Contains(fTrackingStart))
_Invoke(fClickMessage);
fTrackingStart.x = -1.0;
fTrackingStart.y = -1.0;
}
// MouseMoved
void
SwatchView::MouseMoved(BPoint where, uint32 transit,
const BMessage* dragMessage)
const BMessage* dragMessage)
{
if (transit == B_ENTERED_VIEW) {
BCursor cursor(kDropperCursor);
SetViewCursor(&cursor, true);
}
if (Bounds().Contains(fTrackingStart)) {
if (point_point_distance(where, fTrackingStart)
> DRAG_INIT_DIST || transit == B_EXITED_VIEW) {
if (point_point_distance(where, fTrackingStart) > DRAG_INIT_DIST
|| transit == B_EXITED_VIEW) {
_DragColor();
fTrackingStart.x = -1.0;
fTrackingStart.y = -1.0;
@ -206,7 +182,7 @@ SwatchView::MouseMoved(BPoint where, uint32 transit,
}
}
// SetColor
void
SwatchView::SetColor(rgb_color color)
{
@ -215,63 +191,73 @@ SwatchView::SetColor(rgb_color color)
Invalidate();
}
// SetClickedMessage
void
SwatchView::SetClickedMessage(BMessage* message)
{
delete fClickMessage;
if (message != fClickMessage)
delete fClickMessage;
fClickMessage = message;
}
// SetDroppedMessage
void
SwatchView::SetDroppedMessage(BMessage* message)
{
delete fDroppedMessage;
if (message != fDroppedMessage)
delete fDroppedMessage;
fDroppedMessage = message;
}
// _Invoke
void
SwatchView::_Invoke(const BMessage* _message)
{
if (_message) {
BHandler* target = fTarget ? fTarget
: dynamic_cast<BHandler*>(Window());
BLooper* looper;
if (target && (looper = target->Looper())) {
BMessage message(*_message);
message.AddPointer("be:source", (void*)this);
message.AddInt64("be:when", system_time());
message.AddBool("begin", true);
store_color_in_message(&message, fColor);
looper->PostMessage(&message, target);
}
}
if (_message == NULL)
return;
BHandler* target = fTarget;
if (target == NULL)
target = Window();
if (target == NULL)
return;
BLooper* looper = target->Looper();
if (looper == NULL)
return;
BMessage message(*_message);
message.AddPointer("be:source", (void*)this);
message.AddInt64("be:when", system_time());
message.AddBool("begin", true);
store_color_in_message(&message, fColor);
looper->PostMessage(&message, target);
}
// _StrokeRect
void
SwatchView::_StrokeRect(BRect r, rgb_color leftTop,
rgb_color rightBottom)
SwatchView::_StrokeRect(BRect r, rgb_color leftTop, rgb_color rightBottom)
{
BeginLineArray(4);
AddLine(BPoint(r.left, r.bottom - 1),
BPoint(r.left, r.top), leftTop);
AddLine(BPoint(r.left + 1, r.top),
BPoint(r.right, r.top), leftTop);
AddLine(BPoint(r.right, r.top + 1),
BPoint(r.right, r.bottom), rightBottom);
AddLine(BPoint(r.right - 1, r.bottom),
BPoint(r.left, r.bottom), rightBottom);
AddLine(BPoint(r.left, r.bottom - 1), BPoint(r.left, r.top), leftTop);
AddLine(BPoint(r.left + 1, r.top), BPoint(r.right, r.top), leftTop);
AddLine(BPoint(r.right, r.top + 1), BPoint(r.right, r.bottom),
rightBottom);
AddLine(BPoint(r.right - 1, r.bottom), BPoint(r.left, r.bottom),
rightBottom);
EndLineArray();
}
// _DragColor
void
SwatchView::_DragColor()
{
BBitmap *bitmap = new BBitmap(BRect(0.0, 0.0, 15.0, 15.0), B_RGB32);
BBitmap* bitmap = new(std::nothrow) BBitmap(BRect(0.0, 0.0, 15.0, 15.0),
B_RGB32);
BMessage message = make_color_drop_message(fColor, bitmap);
DragMessage(&message, bitmap, B_OP_ALPHA, BPoint(9.0, 9.0));

View File

@ -1,63 +1,47 @@
/*
* Copyright 2006, Haiku.
* Copyright 2006-2012, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Stephan Aßmus <superstippi@gmx.de>
*/
#ifndef SWATCH_VIEW_H
#define SWATCH_VIEW_H
#include <View.h>
#if LIB_LAYOUT
#include <layout.h>
#endif
class SwatchView :
#if LIB_LAYOUT
public MView,
#endif
public BView {
public:
SwatchView(const char* name,
BMessage* message,
BHandler* target,
rgb_color color,
float width = 24.0,
float height = 24.0);
class SwatchView : public BView {
public:
SwatchView(const char* name, BMessage* message,
BHandler* target, rgb_color color,
float width = 24.0, float height = 24.0);
virtual ~SwatchView();
#if LIB_LAYOUT
// MView
virtual minimax layoutprefs();
virtual BRect layout(BRect frame);
#endif
// BView
// BView
virtual void Draw(BRect updateRect);
virtual void MessageReceived(BMessage* message);
virtual void MouseDown(BPoint where);
virtual void MouseUp(BPoint where);
virtual void MouseMoved(BPoint where, uint32 transit,
const BMessage* dragMessage);
const BMessage* dragMessage);
// SwatchView
// SwatchView
void SetColor(rgb_color color);
rgb_color Color() const
inline rgb_color Color() const
{ return fColor; }
void SetClickedMessage(BMessage* message);
void SetDroppedMessage(BMessage* message);
private:
private:
void _Invoke(const BMessage* message);
void _StrokeRect(BRect frame, rgb_color leftTop,
rgb_color rightBottom);
rgb_color rightBottom);
void _DragColor();
private:
rgb_color fColor;
BPoint fTrackingStart;
bool fActive;