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