Changed fpBitmap to the more Be-ish notation of fBitmap, added code to allow the user to drag the bitmap selection around like in Be's ShowImage. However, no changes can be made yet to the underlying image. Need to resolve resize flickering of selected image and allow the user to modify the underlying image.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@5392 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Matthew Wilber
2003-11-16 23:23:50 +00:00
parent 0e93ae51e9
commit 9eec20bc4a
2 changed files with 105 additions and 52 deletions
+97 -50
View File
@@ -132,7 +132,7 @@ void
ShowImageView::Pulse() ShowImageView::Pulse()
{ {
// animate marching ants // animate marching ants
if (fbHasSelection && fAnimateSelection && Window()->IsActive()) { if (HasSelection() && fAnimateSelection && Window()->IsActive()) {
RotatePatterns(); RotatePatterns();
DrawSelectionBox(fSelectionRect); DrawSelectionBox(fSelectionRect);
} }
@@ -153,7 +153,8 @@ ShowImageView::ShowImageView(BRect rect, const char *name, uint32 resizingMode,
{ {
InitPatterns(); InitPatterns();
fpBitmap = NULL; fBitmap = NULL;
fSelBitmap = NULL;
fDocumentIndex = 1; fDocumentIndex = 1;
fDocumentCount = 1; fDocumentCount = 1;
fAnimateSelection = true; fAnimateSelection = true;
@@ -238,15 +239,22 @@ void
ShowImageView::DeleteBitmap() ShowImageView::DeleteBitmap()
{ {
DeleteScaler(); DeleteScaler();
delete fpBitmap; DeleteSelBitmap();
fpBitmap = NULL; delete fBitmap;
fBitmap = NULL;
}
void ShowImageView::DeleteSelBitmap()
{
delete fSelBitmap;
fSelBitmap = NULL;
} }
void void
ShowImageView::SetImage(const entry_ref *pref) ShowImageView::SetImage(const entry_ref *pref)
{ {
DeleteBitmap(); DeleteBitmap();
fbHasSelection = false; SetHasSelection(false);
fMakesSelection = false; fMakesSelection = false;
entry_ref ref; entry_ref ref;
@@ -276,7 +284,7 @@ ShowImageView::SetImage(const entry_ref *pref)
if (proster->Translate(&file, &info, &ioExtension, &outstream, if (proster->Translate(&file, &info, &ioExtension, &outstream,
B_TRANSLATOR_BITMAP) != B_OK) B_TRANSLATOR_BITMAP) != B_OK)
return; return;
if (outstream.DetachBitmap(&fpBitmap) != B_OK) if (outstream.DetachBitmap(&fBitmap) != B_OK)
return; return;
fCurrentRef = ref; fCurrentRef = ref;
@@ -334,7 +342,7 @@ ShowImageView::SetAlignment(alignment horizontal, vertical_alignment vertical)
BBitmap * BBitmap *
ShowImageView::GetBitmap() ShowImageView::GetBitmap()
{ {
return fpBitmap; return fBitmap;
} }
void void
@@ -389,7 +397,7 @@ ShowImageView::AttachedToWindow()
BRect BRect
ShowImageView::AlignBitmap() ShowImageView::AlignBitmap()
{ {
BRect rect(fpBitmap->Bounds()); BRect rect(fBitmap->Bounds());
float width, height; float width, height;
width = Bounds().Width()-2*PEN_SIZE+1; width = Bounds().Width()-2*PEN_SIZE+1;
height = Bounds().Height()-2*PEN_SIZE+1; height = Bounds().Height()-2*PEN_SIZE+1;
@@ -452,8 +460,8 @@ ShowImageView::Setup(BRect rect)
{ {
fLeft = rect.left; fLeft = rect.left;
fTop = rect.top; fTop = rect.top;
fScaleX = (rect.Width()+1.0) / (fpBitmap->Bounds().Width()+1.0); fScaleX = (rect.Width()+1.0) / (fBitmap->Bounds().Width()+1.0);
fScaleY = (rect.Height()+1.0) / (fpBitmap->Bounds().Height()+1.0); fScaleY = (rect.Height()+1.0) / (fBitmap->Bounds().Height()+1.0);
} }
BPoint BPoint
@@ -569,7 +577,7 @@ ShowImageView::GetScaler()
if (fScaler == NULL || fScaler->Scale() != fZoom) { if (fScaler == NULL || fScaler->Scale() != fZoom) {
DeleteScaler(); DeleteScaler();
BMessenger msgr(this, Window()); BMessenger msgr(this, Window());
fScaler = new Scaler(fpBitmap, fZoom, msgr, MSG_INVALIDATE); fScaler = new Scaler(fBitmap, fZoom, msgr, MSG_INVALIDATE);
fScaler->Start(); fScaler->Start();
} }
return fScaler; return fScaler;
@@ -586,13 +594,13 @@ ShowImageView::DrawImage(BRect rect)
return; return;
} }
} }
DrawBitmap(fpBitmap, fpBitmap->Bounds(), rect); DrawBitmap(fBitmap, fBitmap->Bounds(), rect);
} }
void void
ShowImageView::Draw(BRect updateRect) ShowImageView::Draw(BRect updateRect)
{ {
if (fpBitmap) { if (fBitmap) {
if (!IsPrinting()) { if (!IsPrinting()) {
BRect rect = AlignBitmap(); BRect rect = AlignBitmap();
Setup(rect); Setup(rect);
@@ -615,11 +623,37 @@ ShowImageView::Draw(BRect updateRect)
DrawCaption(); DrawCaption();
} }
if (fbHasSelection) { if (HasSelection()) {
if (fSelBitmap) {
BRect clippedRect, bounds;
clippedRect = fSelectionRect;
ConstrainToImage(clippedRect);
clippedRect = ImageToView(clippedRect);
bounds = fSelectionRect;
if (bounds.left < 0)
bounds.left = -(bounds.left);
else
bounds.left = 0;
if (bounds.top < 0)
bounds.top = -(bounds.top);
else
bounds.top = 0;
if (bounds.right > fBitmap->Bounds().right)
bounds.right = bounds.left + clippedRect.Width();
else
bounds.right = fSelBitmap->Bounds().right;
if (bounds.bottom > fBitmap->Bounds().bottom)
bounds.bottom = bounds.top + clippedRect.Height();
else
bounds.bottom = fSelBitmap->Bounds().bottom;
DrawBitmap(fSelBitmap, bounds, clippedRect);
}
DrawSelectionBox(fSelectionRect); DrawSelectionBox(fSelectionRect);
} }
} else { } else {
DrawBitmap(fpBitmap); DrawBitmap(fBitmap);
} }
} }
} }
@@ -650,13 +684,13 @@ ShowImageView::FrameResized(float /* width */, float /* height */)
void void
ShowImageView::ConstrainToImage(BPoint &point) ShowImageView::ConstrainToImage(BPoint &point)
{ {
point.ConstrainTo(fpBitmap->Bounds()); point.ConstrainTo(fBitmap->Bounds());
} }
void void
ShowImageView::ConstrainToImage(BRect &rect) ShowImageView::ConstrainToImage(BRect &rect)
{ {
BRect bounds = fpBitmap->Bounds(); BRect bounds = fBitmap->Bounds();
BPoint leftTop, rightBottom; BPoint leftTop, rightBottom;
leftTop = rect.LeftTop(); leftTop = rect.LeftTop();
@@ -674,16 +708,16 @@ ShowImageView::CopySelection(uchar alpha)
{ {
bool hasAlpha = alpha != 255; bool hasAlpha = alpha != 255;
if (!fbHasSelection) return NULL; if (!HasSelection()) return NULL;
BRect rect(0, 0, fSelectionRect.IntegerWidth(), fSelectionRect.IntegerHeight()); BRect rect(0, 0, fCopyFromRect.IntegerWidth(), fCopyFromRect.IntegerHeight());
BView view(rect, NULL, B_FOLLOW_NONE, B_WILL_DRAW); BView view(rect, NULL, B_FOLLOW_NONE, B_WILL_DRAW);
BBitmap *bitmap = new BBitmap(rect, hasAlpha ? B_RGBA32 : fpBitmap->ColorSpace(), true); BBitmap *bitmap = new BBitmap(rect, hasAlpha ? B_RGBA32 : fBitmap->ColorSpace(), true);
if (bitmap == NULL) return NULL; if (bitmap == NULL) return NULL;
if (bitmap->Lock()) { if (bitmap->Lock()) {
bitmap->AddChild(&view); bitmap->AddChild(&view);
view.DrawBitmap(fpBitmap, fSelectionRect, rect); view.DrawBitmap(fBitmap, fCopyFromRect, rect);
if (hasAlpha) { if (hasAlpha) {
view.SetDrawingMode(B_OP_SUBTRACT); view.SetDrawingMode(B_OP_SUBTRACT);
view.SetHighColor(0, 0, 0, 255-alpha); view.SetHighColor(0, 0, 0, 255-alpha);
@@ -900,8 +934,11 @@ ShowImageView::MouseDown(BPoint position)
point = ViewToImage(position); point = ViewToImage(position);
buttons = GetMouseButtons(); buttons = GetMouseButtons();
if (fbHasSelection && fSelectionRect.Contains(point) && if (HasSelection() && fSelectionRect.Contains(point) &&
(buttons & (B_PRIMARY_MOUSE_BUTTON | B_SECONDARY_MOUSE_BUTTON))) { (buttons & (B_PRIMARY_MOUSE_BUTTON | B_SECONDARY_MOUSE_BUTTON))) {
if (!fSelBitmap) {
fSelBitmap = CopySelection();
}
BPoint sourcePoint = point; BPoint sourcePoint = point;
BeginDrag(sourcePoint); BeginDrag(sourcePoint);
@@ -909,8 +946,8 @@ ShowImageView::MouseDown(BPoint position)
// Keep reading mouse movement until // Keep reading mouse movement until
// the user lets up on all mouse buttons // the user lets up on all mouse buttons
GetMouse(&point, &buttons); GetMouse(&point, &buttons);
snooze(50000); snooze(25 * 1000);
// sleep for 50 milliseconds to minimize CPU usage during loop // sleep for 25 milliseconds to minimize CPU usage during loop
} }
if (Bounds().Contains(point)) { if (Bounds().Contains(point)) {
@@ -923,7 +960,7 @@ ShowImageView::MouseDown(BPoint position)
BRect newSelection = fSelectionRect; BRect newSelection = fSelectionRect;
newSelection.OffsetBy(diff); newSelection.OffsetBy(diff);
if (fpBitmap->Bounds().Intersects(newSelection)) { if (fBitmap->Bounds().Intersects(newSelection)) {
// Do not accept the new selection box location // Do not accept the new selection box location
// if it does not intersect with the bitmap rectangle // if it does not intersect with the bitmap rectangle
fSelectionRect = newSelection; fSelectionRect = newSelection;
@@ -935,12 +972,13 @@ ShowImageView::MouseDown(BPoint position)
} else if (buttons == B_PRIMARY_MOUSE_BUTTON) { } else if (buttons == B_PRIMARY_MOUSE_BUTTON) {
// begin new selection // begin new selection
SetHasSelection(true);
fMakesSelection = true; fMakesSelection = true;
fbHasSelection = true;
SetMouseEventMask(B_POINTER_EVENTS); SetMouseEventMask(B_POINTER_EVENTS);
ConstrainToImage(point); ConstrainToImage(point);
fFirstPoint = point; fFirstPoint = point;
fSelectionRect.Set(point.x, point.y, point.x, point.y); fCopyFromRect.Set(point.x, point.y, point.x, point.y);
fSelectionRect = fCopyFromRect;
Invalidate(); Invalidate();
} else if (buttons == B_SECONDARY_MOUSE_BUTTON) { } else if (buttons == B_SECONDARY_MOUSE_BUTTON) {
ShowPopUpMenu(ConvertToScreen(position)); ShowPopUpMenu(ConvertToScreen(position));
@@ -955,22 +993,23 @@ ShowImageView::MouseDown(BPoint position)
void void
ShowImageView::UpdateSelectionRect(BPoint point, bool final) { ShowImageView::UpdateSelectionRect(BPoint point, bool final) {
BRect oldSelection = fSelectionRect; BRect oldSelection = fCopyFromRect;
point = ViewToImage(point); point = ViewToImage(point);
ConstrainToImage(point); ConstrainToImage(point);
fSelectionRect.left = min(fFirstPoint.x, point.x); fCopyFromRect.left = min(fFirstPoint.x, point.x);
fSelectionRect.right = max(fFirstPoint.x, point.x); fCopyFromRect.right = max(fFirstPoint.x, point.x);
fSelectionRect.top = min(fFirstPoint.y, point.y); fCopyFromRect.top = min(fFirstPoint.y, point.y);
fSelectionRect.bottom = max(fFirstPoint.y, point.y); fCopyFromRect.bottom = max(fFirstPoint.y, point.y);
fSelectionRect = fCopyFromRect;
if (final) { if (final) {
// selection must contain a few pixels // selection must contain a few pixels
if (fSelectionRect.Width() * fSelectionRect.Height() <= 1) { if (fCopyFromRect.Width() * fCopyFromRect.Height() <= 1) {
fbHasSelection = false; SetHasSelection(false);
} }
} }
if (oldSelection != fSelectionRect || !fbHasSelection) { if (oldSelection != fCopyFromRect || !HasSelection()) {
BRect updateRect; BRect updateRect;
updateRect = oldSelection | fSelectionRect; updateRect = oldSelection | fCopyFromRect;
updateRect = ImageToView(updateRect); updateRect = ImageToView(updateRect);
updateRect.InsetBy(-PEN_SIZE, -PEN_SIZE); updateRect.InsetBy(-PEN_SIZE, -PEN_SIZE);
Invalidate(updateRect); Invalidate(updateRect);
@@ -1181,7 +1220,7 @@ ShowImageView::FixupScrollBars()
} }
BRect rctview = Bounds(), rctbitmap(0, 0, 0, 0); BRect rctview = Bounds(), rctbitmap(0, 0, 0, 0);
if (fpBitmap) { if (fBitmap) {
BRect rect(AlignBitmap()); BRect rect(AlignBitmap());
rctbitmap.Set(0, 0, rect.Width(), rect.Height()); rctbitmap.Set(0, 0, rect.Width(), rect.Height());
} }
@@ -1225,24 +1264,32 @@ ShowImageView::PageCount()
void void
ShowImageView::SelectAll() ShowImageView::SelectAll()
{ {
fbHasSelection = true; SetHasSelection(true);
fSelectionRect.Set(0, 0, fpBitmap->Bounds().Width(), fpBitmap->Bounds().Height()); fCopyFromRect.Set(0, 0, fBitmap->Bounds().Width(), fBitmap->Bounds().Height());
fSelectionRect = fCopyFromRect;
Invalidate(); Invalidate();
} }
void void
ShowImageView::Unselect() ShowImageView::Unselect()
{ {
if (fbHasSelection) { if (HasSelection()) {
fbHasSelection = false; SetHasSelection(false);
Invalidate(); Invalidate();
} }
} }
void
ShowImageView::SetHasSelection(bool bHasSelection)
{
DeleteSelBitmap();
fbHasSelection = bHasSelection;
}
void void
ShowImageView::CopySelectionToClipboard() ShowImageView::CopySelectionToClipboard()
{ {
if (fbHasSelection && be_clipboard->Lock()) { if (HasSelection() && be_clipboard->Lock()) {
be_clipboard->Clear(); be_clipboard->Clear();
BMessage *clip = NULL; BMessage *clip = NULL;
if ((clip = be_clipboard->Data()) != NULL) { if ((clip = be_clipboard->Data()) != NULL) {
@@ -1527,14 +1574,14 @@ ShowImageView::DoImageOperation(image_operation op)
int32 x, y, destX, destY; int32 x, y, destX, destY;
BRect rect; BRect rect;
if (fpBitmap == NULL) return; if (fBitmap == NULL) return;
cs = fpBitmap->ColorSpace(); cs = fBitmap->ColorSpace();
bpp = BytesPerPixel(cs); bpp = BytesPerPixel(cs);
if (bpp < 1) return; if (bpp < 1) return;
width = fpBitmap->Bounds().IntegerWidth(); width = fBitmap->Bounds().IntegerWidth();
height = fpBitmap->Bounds().IntegerHeight(); height = fBitmap->Bounds().IntegerHeight();
if (op == kRotateClockwise || op == kRotateAntiClockwise) { if (op == kRotateClockwise || op == kRotateAntiClockwise) {
rect.Set(0, 0, height, width); rect.Set(0, 0, height, width);
@@ -1545,9 +1592,9 @@ ShowImageView::DoImageOperation(image_operation op)
bm = new BBitmap(rect, cs); bm = new BBitmap(rect, cs);
if (bm == NULL) return; if (bm == NULL) return;
src = (uchar*)fpBitmap->Bits(); src = (uchar*)fBitmap->Bits();
dest = (uchar*)bm->Bits(); dest = (uchar*)bm->Bits();
bpr = fpBitmap->BytesPerRow(); bpr = fBitmap->BytesPerRow();
destBPR = bm->BytesPerRow(); destBPR = bm->BytesPerRow();
switch (op) { switch (op) {
@@ -1598,9 +1645,9 @@ ShowImageView::DoImageOperation(image_operation op)
// set new bitmap // set new bitmap
DeleteBitmap(); DeleteBitmap();
fpBitmap = bm; fBitmap = bm;
// remove selection // remove selection
fbHasSelection = false; SetHasSelection(false);
Notify(NULL); Notify(NULL);
} }
+8 -2
View File
@@ -107,11 +107,14 @@ private:
}; };
void InitPatterns(); void InitPatterns();
void RotatePatterns(); void RotatePatterns();
bool HasSelection() { return fbHasSelection; }
void SetHasSelection(bool bHasSelection);
void AnimateSelection(bool a); void AnimateSelection(bool a);
void Notify(const char* status); void Notify(const char* status);
void AddToRecentDocuments(); void AddToRecentDocuments();
void DeleteScaler(); void DeleteScaler();
void DeleteBitmap(); void DeleteBitmap();
void DeleteSelBitmap();
int32 BytesPerPixel(color_space cs) const; int32 BytesPerPixel(color_space cs) const;
inline void CopyPixel(uchar* dest, int32 destX, int32 destY, int32 destBPR, uchar* src, int32 x, int32 y, int32 bpr, int32 bpp); inline void CopyPixel(uchar* dest, int32 destX, int32 destY, int32 destBPR, uchar* src, int32 x, int32 y, int32 bpr, int32 bpp);
inline void InvertPixel(int32 x, int32 y, uchar* dest, int32 destBPR, uchar* src, int32 bpr, int32 bpp); inline void InvertPixel(int32 x, int32 y, uchar* dest, int32 destBPR, uchar* src, int32 bpr, int32 bpp);
@@ -155,7 +158,8 @@ private:
entry_ref fCurrentRef; entry_ref fCurrentRef;
int32 fDocumentIndex; int32 fDocumentIndex;
int32 fDocumentCount; int32 fDocumentCount;
BBitmap *fpBitmap; BBitmap *fBitmap;
BBitmap *fSelBitmap;
float fZoom; float fZoom;
bool fScaleBilinear; bool fScaleBilinear;
Scaler* fScaler; Scaler* fScaler;
@@ -171,7 +175,9 @@ private:
BPoint fFirstPoint; // first point in image space of selection BPoint fFirstPoint; // first point in image space of selection
bool fAnimateSelection; // marching ants bool fAnimateSelection; // marching ants
bool fbHasSelection; // is fSelectionRect valid bool fbHasSelection; // is fSelectionRect valid
BRect fSelectionRect; // the selection in image space BRect fSelectionRect; // the current location of the selection rectangle
BRect fCopyFromRect;
// the portion of the background bitmap the selection is made from
pattern fPatternUp, fPatternDown, fPatternLeft, fPatternRight; pattern fPatternUp, fPatternDown, fPatternLeft, fPatternRight;
bool fSlideShow; bool fSlideShow;