diff --git a/src/apps/showimage/ShowImageView.cpp b/src/apps/showimage/ShowImageView.cpp index e6980f1e13..cabdce2739 100644 --- a/src/apps/showimage/ShowImageView.cpp +++ b/src/apps/showimage/ShowImageView.cpp @@ -53,6 +53,7 @@ ShowImageView::ShowImageView(BRect rect, const char *name, uint32 resizingMode, fpbitmap = NULL; fdocumentIndex = 1; fdocumentCount = 1; + fbhasSelection = false; SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); SetHighColor(0, 0, 0); @@ -150,15 +151,105 @@ ShowImageView::Draw(BRect updateRect) BORDER_HEIGHT - PEN_SIZE, fpbitmap->Bounds().Width() + BORDER_WIDTH + PEN_SIZE, fpbitmap->Bounds().Height() + BORDER_HEIGHT + PEN_SIZE)); + + if (fbhasSelection) + DrawInvertBox(fselectionRect); } } +void +ShowImageView::DrawInvertBox(BRect &rect) +{ + drawing_mode oldmode = DrawingMode(); + SetDrawingMode(B_OP_INVERT); + StrokeRect(rect); + SetDrawingMode(oldmode); +} + void ShowImageView::FrameResized(float /* width */, float /* height */) { FixupScrollBars(); } +void +ShowImageView::ConstrainToImage(BPoint &point) +{ + point.ConstrainTo( + BRect( + BORDER_WIDTH, BORDER_HEIGHT, + fpbitmap->Bounds().Width() + BORDER_WIDTH, + fpbitmap->Bounds().Height() + BORDER_HEIGHT + ) + ); +} + +void +ShowImageView::MouseDown(BPoint point) +{ + // TODO: Need to handle the case where user clicks + // inside an existing selection and starts a drag operation + + if (fbhasSelection) + DrawInvertBox(fselectionRect); + + fbhasSelection = false; + + if (!fpbitmap) { + // Can't select anything if there is no image + Invalidate(); + return; + } + + BPoint firstPoint, secondPoint; + firstPoint = point; + ConstrainToImage(firstPoint); + secondPoint = firstPoint; + // just to be safe + + BRect curSel, lastSel; + + BMessage *pmsg = Window()->CurrentMessage(); + uint32 buttons = static_cast(pmsg->FindInt32("buttons")); + bool bfirst = true; + // While any combination of mouse buttons is down, + // allow the user to size their selection. When all + // mouse buttons are up, the loop ends, and the selection + // has been made. + while (buttons) { + ConstrainToImage(secondPoint); + curSel = BRect(firstPoint, secondPoint); + if (!bfirst && curSel != lastSel) + DrawInvertBox(lastSel); + if (curSel != lastSel) + DrawInvertBox(curSel); + + snooze(20 * 1000); + lastSel = curSel; + GetMouse(&secondPoint, &buttons); + bfirst = false; + } + + if (curSel.IntegerWidth() < 2 && curSel.IntegerHeight() < 2) { + // The user must select at least 2 pixels + fbhasSelection = false; + Invalidate(); + } else { + fselectionRect = curSel; + fbhasSelection = true; + } +} + +void +ShowImageView::MouseMoved(BPoint point, uint32 state, const BMessage *pmsg) +{ +} + +void +ShowImageView::MouseUp(BPoint point) +{ +} + void ShowImageView::MessageReceived(BMessage *pmsg) { diff --git a/src/apps/showimage/ShowImageView.h b/src/apps/showimage/ShowImageView.h index 2b6363bbc0..77dd2b013a 100644 --- a/src/apps/showimage/ShowImageView.h +++ b/src/apps/showimage/ShowImageView.h @@ -46,6 +46,9 @@ public: virtual void AttachedToWindow(); virtual void Draw(BRect updateRect); virtual void FrameResized(float width, float height); + virtual void MouseDown(BPoint point); + virtual void MouseMoved(BPoint point, uint32 state, const BMessage *pmsg); + virtual void MouseUp(BPoint point); virtual void MessageReceived(BMessage *pmsg); void FixupScrollBars(); @@ -59,10 +62,15 @@ public: void PrevPage(); private: + void ConstrainToImage(BPoint &point); + void DrawInvertBox(BRect &rect); + entry_ref fcurrentRef; int32 fdocumentIndex; int32 fdocumentCount; BBitmap *fpbitmap; + bool fbhasSelection; + BRect fselectionRect; }; #endif /* _ShowImageView_h */