//--------------------------------------------------------------------------- #include #pragma hdrstop #include "TPTextileImage.h" #pragma link "TPImage" #pragma package(smart_init) //--------------------------------------------------------------------------- #define PARENTIMAGE ((TPTextileImage *)FParentImage) //--------------------------------------------------------------------------- // hotfix of undefined of min/max #define min(X,Y) (XY?X:Y) //--------------------------------------------------------------------------- // ValidCtrCheck is used to assure that the components created do not have // any pure virtual functions. // static inline void ValidCtrCheck(TPTextileImage *) { new TPTextileImage(NULL); } //--------------------------------------------------------------------------- __fastcall TPTextileImage::TPTextileImage(TComponent* Owner) : TPImage(Owner) { FOnPaintRange = NULL; FBitmap = new TTexpiaBitmap; FBitmap->OnPropertyChange = PropertyChange; } //--------------------------------------------------------------------------- __fastcall TPTextileImage::~TPTextileImage() { if (FBitmap) delete FBitmap; } //--------------------------------------------------------------------------- // Private Function //--------------------------------------------------------------------------- void __fastcall TPTextileImage::SetBorderColor(TColor Value) { if (FBorderColor!=Value) { FBorderColor = Value; Paint(); Invalidate(); } } //--------------------------------------------------------------------------- void __fastcall TPTextileImage::SetRepeat(bool Value) { if (Value!=FRepeat) { FRepeat = Value; Paint(); Invalidate(); } } //--------------------------------------------------------------------------- void __fastcall TPTextileImage::SetStretch(bool Value) { if (Value!=FStretch) { FStretch = Value; Paint(); Invalidate(); } } //--------------------------------------------------------------------------- bool __fastcall TPTextileImage::PositionChange() { return true; } //--------------------------------------------------------------------------- void __fastcall TPTextileImage::PaintStretch(BITMAPHANDLE *bh) { HPALETTE hpaintpal = NULL, holdpal = NULL; HDC dc; RECT Src, Dst; dc = TGraphicControl::Canvas->Handle; if (FBitmap->BitsPerPixel==8) { hpaintpal = L_CreatePaintPalette(dc, bh); if (hpaintpal) holdpal = SelectPalette(dc, hpaintpal, false); } Src.left = 0; Src.top = 0; Src.right = bh->Width; Src.bottom = bh->Height; Dst.left = 0; Dst.top = 0; Dst.right = Width; Dst.bottom = Height; L_PaintDC(dc, bh, &Src, NULL, &Dst, NULL, SRCCOPY); if (hpaintpal) { if (holdpal) SelectPalette(dc, holdpal, false); DeleteObject(hpaintpal); } if (FOnPaint) FOnPaint(this); } //--------------------------------------------------------------------------- void __fastcall TPTextileImage::PaintStretchMask(BITMAPHANDLE *bh) { BITMAPHANDLE *mh; HPALETTE hpaintpal = NULL, holdpal = NULL; HDC dc; RECT Src, Dst; dc = TGraphicControl::Canvas->Handle; if (FBitmap->BitsPerPixel==8) { hpaintpal = L_CreatePaintPalette(dc, bh); if (hpaintpal) holdpal = SelectPalette(dc, hpaintpal, false); } mh = FMask->Handle; if (mh) { Src.left = 0; Src.top = 0; Src.right = bh->Width; Src.bottom = bh->Height; Dst.left = 0; Dst.top = 0; Dst.right = Width; Dst.bottom = Height; L_PaintDC(dc, bh, &Src, NULL, &Dst, NULL, SRCCOPY); L_PaintDC(dc, mh, &Src, NULL, &Dst, NULL, SRCINVERT); } if (hpaintpal) { if (holdpal) SelectPalette(dc, holdpal, false); DeleteObject(hpaintpal); } if (FOnPaint) FOnPaint(this); } //--------------------------------------------------------------------------- #define PAINT_GRIDLINE \ SetROP2(dcMemV, R2_COPYPEN); \ if ((hPen = CreatePen(psSolid, 1, FGridColor)) == NULL) goto fail; \ hOldPen = SelectObject(dcMemV, hPen); \ p = sx-Floor(sx/gx)*gx; \ p = p>0 ? (gx-p)*FZoomIn/FZoomOut : 0; \ while (p0 ? (gy-p)*FZoomIn/FZoomOut : 0; \ while (p0) { \ gx = FGridGapX*FGridMark; \ gy = FGridGapY*FGridMark; \ gzx = gx*FZoomIn/FZoomOut; \ gzy = gy*FZoomIn/FZoomOut; \ if ((hPen = CreatePen(psSolid, 1, FGridMarkColor)) == NULL) goto fail; \ hOldPen = SelectObject(dcMemV, hPen); \ p = sx-Floor(sx/gx)*gx; \ p = p>0 ? (gx-p)*FZoomIn/FZoomOut : 0; \ while (p0 ? (gy-p)*FZoomIn/FZoomOut : 0; \ while (pHandle; \ if (FBitmap->BitsPerPixel==8) { \ hpaintpal = L_CreatePaintPalette(dcDst, bh); \ if (hpaintpal) holdpal = SelectPalette(dcDst, hpaintpal, false); \ } \ if (FParentImage) { \ FGrid = FParentImage->Grid; \ FZoomIn = FParentImage->ZoomIn; \ FZoomOut = FParentImage->ZoomOut; \ } \ pw = Width*FZoomOut/FZoomIn; \ ph = Height*FZoomOut/FZoomIn; \ if (pw>FBitmap->Width-sx) pw = FBitmap->Width-sx; \ if (ph>FBitmap->Height-sy) ph = FBitmap->Height-sy; \ if (FZoomOut>1) { \ pw -= pw%FZoomOut; \ ph -= ph%FZoomOut; \ } \ vw = pw*FZoomIn/FZoomOut; \ vh = ph*FZoomIn/FZoomOut; \ TGraphicControl::Canvas->Brush->Color = FColor; \ if (vwFillRect(Rect(vw, 0, Width, vh)); \ if (vhFillRect(Rect(0, vh, Width, Height)); //--------------------------------------------------------------------------- void __fastcall TPTextileImage::PaintTop(BITMAPHANDLE *bh) { HPALETTE hpaintpal = NULL, holdpal = NULL; int pw, ph, vw, vh, n; double sx, sy, gx, gy, gzx, gzy, p; HDC dcSrc = NULL, dcDst, dcMemV = NULL; HBITMAP bmMemV = NULL; HPEN hPen = NULL, hOldPen; PAINT_INIT; if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; if ((bmMemV = CreateCompatibleBitmap(dcSrc, vw, vh)) == NULL) goto fail; if ((dcMemV = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemV, bmMemV); if (hpaintpal) SelectPalette(dcMemV, hpaintpal, false); SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, 0, 0, vw, vh, dcSrc, sx, sy, pw, ph, SRCCOPY); if (FGrid) { gx = FGridGapX; gy = FGridGapY; gzx = gx*FZoomIn/FZoomOut; gzy = gy*FZoomIn/FZoomOut; if (gzx>=5 && gzy>=5) { if (FParentImage) { FGridColor = FParentImage->GridColor; FGridGapX = FParentImage->GridGapX; FGridGapY = FParentImage->GridGapY; FGridMark = FParentImage->GridMark; FGridMarkColor = FParentImage->GridMarkColor; sx = FParentImage->PositionX+(Left-FParentImage->Left)*FZoomOut/FZoomIn; sy = FParentImage->PositionY+(Top-FParentImage->Top)*FZoomOut/FZoomIn; } PAINT_GRIDLINE; } } if (FOnPaintZoom) FOnPaintZoom(this, dcMemV); if (FOnPaintRange) FOnPaintRange(this, dcMemV, false, 0, 0); BitBlt(dcDst, 0, 0, vw, vh, dcMemV, 0, 0, SRCCOPY); DeleteDC(dcMemV); DeleteObject(bmMemV); FBitmap->DeleteDC(dcSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } if (FOnPaint) FOnPaint(this); return; fail: if (dcSrc) { if (bmMemV) { if (dcMemV) { if (hPen) DeleteObject(hPen); DeleteDC(dcMemV); } DeleteObject(bmMemV); } FBitmap->DeleteDC(dcSrc); } if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //--------------------------------------------------------------------------- void __fastcall TPTextileImage::PaintTopMask(BITMAPHANDLE *bh) { BITMAPHANDLE *mh; HPALETTE hpaintpal = NULL, holdpal = NULL; int pw, ph, vw, vh; double sx, sy, gx, gy, gzx, gzy, p; HDC dcSrc = NULL, dcDst, dcMemV = NULL, dcMSrc = NULL; HBITMAP bmMemV = NULL; HPEN hPen = NULL, hOldPen; mh = FMask->Handle; if (mh) { PAINT_INIT; if ((dcMSrc = FMask->CreateDC()) == NULL) goto fail; if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; if ((bmMemV = CreateCompatibleBitmap(dcSrc, vw, vh)) == NULL) goto fail; if ((dcMemV = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemV, bmMemV); if (hpaintpal) SelectPalette(dcMemV, hpaintpal, false); SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, 0, 0, vw, vh, dcSrc, sx, sy, pw, ph, SRCCOPY); StretchBlt(dcMemV, 0, 0, vw, vh, dcMSrc, sx, sy, pw, ph, SRCINVERT); if (FGrid) { gx = FGridGapX; gy = FGridGapY; gzx = gx*FZoomIn/FZoomOut; gzy = gy*FZoomIn/FZoomOut; if (gzx>=5 && gzy>=5) { if (FParentImage) { FGridColor = FParentImage->GridColor; FGridGapX = FParentImage->GridGapX; FGridGapY = FParentImage->GridGapY; FGridMark = FParentImage->GridMark; FGridMarkColor = FParentImage->GridMarkColor; sx = FParentImage->PositionX+(Left-FParentImage->Left)*FZoomOut/FZoomIn; sy = FParentImage->PositionY+(Top-FParentImage->Top)*FZoomOut/FZoomIn; } PAINT_GRIDLINE; } } if (FOnPaintZoom) FOnPaintZoom(this, dcMemV); if (FOnPaintRange) FOnPaintRange(this, dcMemV, false, 0, 0); BitBlt(dcDst, 0, 0, vw, vh, dcMemV, 0, 0, SRCCOPY); DeleteDC(dcMemV); DeleteObject(bmMemV); FBitmap->DeleteDC(dcSrc); FMask->DeleteDC(dcMSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } if (FOnPaint) FOnPaint(this); } return; fail: if (dcMSrc) { if (dcSrc) { if (bmMemV) { if (dcMemV) { if (hPen) DeleteObject(hPen); DeleteDC(dcMemV); } DeleteObject(bmMemV); } FBitmap->DeleteDC(dcSrc); } FMask->DeleteDC(dcMSrc); } if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //--------------------------------------------------------------------------- void __fastcall TPTextileImage::RepeatBitBlt(HDC dcDst, int nXDst, int nYDst, int nWDst, int nHDst, HDC dcSrc, int nWSrc, int nHSrc, int nXSrc, int nYSrc) { HBITMAP bmMem = NULL; HDC dcMem = NULL; int sx, sy, dx, dy, w, h; if ((bmMem = CreateCompatibleBitmap(dcDst, nWDst, nHDst)) == NULL) goto fail; if ((dcMem = CreateCompatibleDC(dcDst)) == NULL) goto fail; SelectObject(dcMem, bmMem); dy = 0; sy = nYSrc; while (dynHSrc-sy) h = nHSrc-sy; dx = 0; sx = nXSrc; while (dxnWSrc-sx) w = nWSrc-sx; BitBlt(dcMem, dx, dy, w, h, dcSrc, sx, sy, SRCCOPY); dx += w; sx = 0; } dy += h; sy = 0; } BitBlt(dcDst, nXDst, nYDst, nWDst, nHDst, dcMem, 0, 0, SRCCOPY); DeleteDC(dcMem); DeleteObject(bmMem); return; fail: if (bmMem) DeleteObject(bmMem); } //--------------------------------------------------------------------------- #define PAINT_REPEATINIT \ sx = FPositionX; \ sy = FPositionY; \ dcDst = TGraphicControl::Canvas->Handle; \ if (FBitmap->BitsPerPixel==8) { \ hpaintpal = L_CreatePaintPalette(dcDst, bh); \ if (hpaintpal) holdpal = SelectPalette(dcDst, hpaintpal, false); \ } \ if (FParentImage) { \ FGrid = FParentImage->Grid; \ FZoomIn = FParentImage->ZoomIn; \ FZoomOut = FParentImage->ZoomOut; \ } \ rpw = Width*FZoomOut/FZoomIn; \ rph = Height*FZoomOut/FZoomIn; \ pw = rpw>FBitmap->Width-sx ? FBitmap->Width-sx : rpw; \ ph = rph>FBitmap->Height-sy ? FBitmap->Height-sy : rph; \ if (FZoomOut>1) { \ rpw -= rpw%FZoomOut; \ rph -= rph%FZoomOut; \ pw -= pw%FZoomOut; \ ph -= ph%FZoomOut; \ } \ rvw = rpw*FZoomIn/FZoomOut; \ rvh = rph*FZoomIn/FZoomOut; \ vw = pw*FZoomIn/FZoomOut; \ vh = ph*FZoomIn/FZoomOut; \ TGraphicControl::Canvas->Brush->Color = FColor; \ if (rvwFillRect(Rect(rvw, 0, Width, rvh)); \ if (rvhFillRect(Rect(0, rvh, Width, Height)); //--------------------------------------------------------------------------- #define PAINT_GRIDLINE_REPEAT \ SetROP2(dcMemV, R2_COPYPEN); \ if ((hPen = CreatePen(psSolid, 1, FGridColor)) == NULL) goto fail; \ if ((hBorderPen = CreatePen(psSolid, 1, FBorderColor)) == NULL) goto fail; \ p = sx-Floor(sx/gx)*gx; \ if (p>0) { \ hOldPen = SelectObject(dcMemV, hPen); \ p = (gx-p)*FZoomIn/FZoomOut; \ } else { \ if (sx==0) { \ hOldPen = SelectObject(dcMemV, hBorderPen); \ MoveToEx(dcMemV, 0, 0, NULL); \ LineTo(dcMemV, 0, vh); \ SelectObject(dcMemV, hPen); \ } else { \ hOldPen = SelectObject(dcMemV, hPen); \ MoveToEx(dcMemV, 0, 0, NULL); \ LineTo(dcMemV, 0, vh); \ } \ p = gzx; \ } \ while (p0) { \ hOldPen = SelectObject(dcMemV, hPen); \ p = (gy-p)*FZoomIn/FZoomOut; \ } else { \ if (sy==0) { \ hOldPen = SelectObject(dcMemV, hBorderPen); \ MoveToEx(dcMemV, 0, 0, NULL); \ LineTo(dcMemV, vw, 0); \ SelectObject(dcMemV, hPen); \ } else { \ hOldPen = SelectObject(dcMemV, hPen); \ MoveToEx(dcMemV, 0, 0, NULL); \ LineTo(dcMemV, vw, 0); \ } \ p = gzy; \ } \ while (p0) { \ gx = FGridGapX*FGridMark; \ gy = FGridGapY*FGridMark; \ gzx = gx*FZoomIn/FZoomOut; \ gzy = gy*FZoomIn/FZoomOut; \ if ((hPen = CreatePen(psSolid, 1, FGridMarkColor)) == NULL) goto fail; \ hOldPen = SelectObject(dcMemV, hPen); \ p = sx-Floor(sx/gx)*gx; \ p = p>0 ? (gx-p)*FZoomIn/FZoomOut : gzx; \ while (p0 ? (gy-p)*FZoomIn/FZoomOut : gzy; \ while (pCreateDC()) == NULL) goto fail; if ((bmMemV = CreateCompatibleBitmap(dcSrc, vw, vh)) == NULL) goto fail; if ((dcMemV = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemV, bmMemV); if (hpaintpal) SelectPalette(dcMemV, hpaintpal, false); SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, 0, 0, vw, vh, dcSrc, sx, sy, pw, ph, SRCCOPY); if (FGrid) { gx = FGridGapX; gy = FGridGapY; gzx = gx*FZoomIn/FZoomOut; gzy = gy*FZoomIn/FZoomOut; if (gzx>=5 && gzy>=5) { if (FParentImage) { FGridColor = FParentImage->GridColor; FGridGapX = FParentImage->GridGapX; FGridGapY = FParentImage->GridGapY; FGridMark = FParentImage->GridMark; FGridMarkColor = FParentImage->GridMarkColor; sx = FParentImage->PositionX+(Left-FParentImage->Left)*FZoomOut/FZoomIn; sy = FParentImage->PositionY+(Top-FParentImage->Top)*FZoomOut/FZoomIn; } PAINT_GRIDLINE_REPEAT; } } if (FOnPaintZoom) FOnPaintZoom(this, dcMemV); if (FOnPaintRange) FOnPaintRange(this, dcMemV, false, 0, 0); RepeatBitBlt(dcDst, 0, 0, rvw, rvh, dcMemV, vw, vh, 0, 0); DeleteDC(dcMemV); DeleteObject(bmMemV); FBitmap->DeleteDC(dcSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } if (FOnPaint) FOnPaint(this); return; fail: if (dcSrc) { if (bmMemV) { if (dcMemV) { if (hPen) { if (hBorderPen) DeleteObject(hBorderPen); DeleteObject(hPen); } DeleteDC(dcMemV); } DeleteObject(bmMemV); } FBitmap->DeleteDC(dcSrc); } if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //--------------------------------------------------------------------------- void __fastcall TPTextileImage::PaintRepeatTopMask(BITMAPHANDLE *bh) { BITMAPHANDLE *mh; HPALETTE hpaintpal = NULL, holdpal = NULL; int pw, ph, vw, vh, rpw, rph, rvw, rvh; double sx, sy, gx, gy, gzx, gzy, p; HDC dcSrc = NULL, dcDst, dcMemV = NULL, dcMSrc = NULL; HBITMAP bmMemV = NULL; HPEN hPen = NULL, hOldPen, hBorderPen = NULL; mh = FMask->Handle; if (mh) { PAINT_REPEATINIT; if ((dcMSrc = FMask->CreateDC()) == NULL) goto fail; if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; if ((bmMemV = CreateCompatibleBitmap(dcSrc, vw, vh)) == NULL) goto fail; if ((dcMemV = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemV, bmMemV); if (hpaintpal) SelectPalette(dcMemV, hpaintpal, false); SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, 0, 0, vw, vh, dcSrc, sx, sy, pw, ph, SRCCOPY); StretchBlt(dcMemV, 0, 0, vw, vh, dcMSrc, sx, sy, pw, ph, SRCINVERT); if (FGrid) { gx = FGridGapX; gy = FGridGapY; gzx = gx*FZoomIn/FZoomOut; gzy = gy*FZoomIn/FZoomOut; if (gzx>=5 && gzy>=5) { if (FParentImage) { FGridColor = FParentImage->GridColor; FGridGapX = FParentImage->GridGapX; FGridGapY = FParentImage->GridGapY; FGridMark = FParentImage->GridMark; FGridMarkColor = FParentImage->GridMarkColor; sx = FParentImage->PositionX+(Left-FParentImage->Left)*FZoomOut/FZoomIn; sy = FParentImage->PositionY+(Top-FParentImage->Top)*FZoomOut/FZoomIn; } PAINT_GRIDLINE_REPEAT; } } if (FOnPaintZoom) FOnPaintZoom(this, dcMemV); if (FOnPaintRange) FOnPaintRange(this, dcMemV, false, 0, 0); RepeatBitBlt(dcDst, 0, 0, rvw, rvh, dcMemV, vw, vh, 0, 0); DeleteDC(dcMemV); DeleteObject(bmMemV); FBitmap->DeleteDC(dcSrc); FMask->DeleteDC(dcMSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } if (FOnPaint) FOnPaint(this); } return; fail: if (dcMSrc) { if (dcSrc) { if (bmMemV) { if (dcMemV) { if (hPen) { if (hBorderPen) DeleteObject(hBorderPen); DeleteObject(hPen); } DeleteDC(dcMemV); } DeleteObject(bmMemV); } FBitmap->DeleteDC(dcSrc); } FMask->DeleteDC(dcMSrc); } if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //--------------------------------------------------------------------------- void __fastcall TPTextileImage::PropertyChange() { Invalidate(); } //--------------------------------------------------------------------------- // Protected Function //--------------------------------------------------------------------------- void __fastcall TPTextileImage::Paint() { BITMAPHANDLE *bh; if (ComponentState.Contains(csDesigning)) { TGraphicControl::Canvas->Pen->Style = psDash; TGraphicControl::Canvas->Brush->Style = bsClear; TGraphicControl::Canvas->Rectangle(0, 0, Width, Height); } else { bh = FBitmap->Handle; if (bh) { if (Width<=0 || Height<=0) return; if (FMaskEnabled) { if (FStretch) { PaintStretchMask(bh); } else { if (FRepeat) { PaintRepeatTopMask(bh); } else { PaintTopMask(bh); } } } else { if (FStretch) { PaintStretch(bh); } else { if (FRepeat) { PaintRepeatTop(bh); } else { PaintTop(bh); } } } } } } //--------------------------------------------------------------------- void __fastcall TPTextileImage::MouseDown(TMouseButton Button, TShiftState Shift, int X, int Y) { int px, py; if (FCoordinationTransfer) { if (FBitmap->Handle) { if (FRepeat) { px = (FPositionX+X*FZoomOut/FZoomIn)%FBitmap->Width; py = (FPositionY+Y*FZoomOut/FZoomIn)%FBitmap->Height; TControl::MouseDown(Button, Shift, px, py); } else { px = FPositionX+X*FZoomOut/FZoomIn; py = FPositionY+Y*FZoomOut/FZoomIn; TControl::MouseDown(Button, Shift, px, py); } } else { TControl::MouseDown(Button, Shift, X, Y); } } else { TControl::MouseDown(Button, Shift, X, Y); } } //--------------------------------------------------------------------- void __fastcall TPTextileImage::MouseMove(TShiftState Shift, int X, int Y) { int px, py; if (FCoordinationTransfer) { if (FBitmap->Handle) { if (FRepeat) { px = (FPositionX+X*FZoomOut/FZoomIn)%FBitmap->Width; py = (FPositionY+Y*FZoomOut/FZoomIn)%FBitmap->Height; TControl::MouseMove(Shift, px, py); } else { px = FPositionX+X*FZoomOut/FZoomIn; py = FPositionY+Y*FZoomOut/FZoomIn; TControl::MouseMove(Shift, px, py); } } else { TControl::MouseMove(Shift, X, Y); } } else { TControl::MouseMove(Shift, X, Y); } } //--------------------------------------------------------------------- void __fastcall TPTextileImage::MouseUp(TMouseButton Button, TShiftState Shift, int X, int Y) { int px, py; if (FCoordinationTransfer) { if (FBitmap->Handle) { if (FRepeat) { px = (FPositionX+X*FZoomOut/FZoomIn)%FBitmap->Width; py = (FPositionY+Y*FZoomOut/FZoomIn)%FBitmap->Height; TControl::MouseUp(Button, Shift, px, py); } else { px = FPositionX+X*FZoomOut/FZoomIn; py = FPositionY+Y*FZoomOut/FZoomIn; TControl::MouseUp(Button, Shift, px, py); } } else { TControl::MouseUp(Button, Shift, X, Y); } } else { TControl::MouseUp(Button, Shift, X, Y); } } //--------------------------------------------------------------------- // Public Function //--------------------------------------------------------------------------- void __fastcall TPTextileImage::OutlineBitmapRgn(bool bComplex) { pBITMAPHANDLE bh; RGNXFORM XForm; HRGN rgn; HBRUSH hBrush; if (FBitmap->IsUse) return; bh = FBitmap->Handle; if (bh) { if (L_BitmapHasRgn(bh)) { if (FParentImage) { FZoomIn = FParentImage->ZoomIn; FZoomOut = FParentImage->ZoomOut; } nFrameRgn = (nFrameRgn+1)&7; XForm.uViewPerspective = TOP_LEFT; XForm.nXScalarNum = FZoomIn; XForm.nXScalarDen = FZoomOut; XForm.nYScalarNum = FZoomIn; XForm.nYScalarDen = FZoomOut; XForm.nXOffset = -FPositionX*FZoomIn/FZoomOut; XForm.nYOffset = -FPositionY*FZoomIn/FZoomOut; if (bComplex) { L_FrameBitmapRgn(TGraphicControl::Canvas->Handle, bh, &XForm, nFrameRgn); } else { L_GetBitmapRgnHandle(bh, &XForm, &rgn); if ((hBrush = CreatePatternBrush(bmFrameRgn[nFrameRgn])) != NULL) { FrameRgn(TGraphicControl::Canvas->Handle, rgn, hBrush, 1, 1); DeleteObject(hBrush); } DeleteObject(rgn); } } } } //--------------------------------------------------------------------------- #define RECTPAINT_INIT \ sx = FPositionX; \ sy = FPositionY; \ dcDst = TGraphicControl::Canvas->Handle; \ if (FBitmap->BitsPerPixel==8) { \ hpaintpal = L_CreatePaintPalette(dcDst, bh); \ if (hpaintpal) holdpal = SelectPalette(dcDst, hpaintpal, false); \ } \ if (FParentImage) { \ FGrid = FParentImage->Grid; \ FZoomIn = FParentImage->ZoomIn; \ FZoomOut = FParentImage->ZoomOut; \ } \ Src.left -= sx; \ Src.top -= sy; \ Src.right -= sx; \ Src.bottom -= sy; \ pw = min(Width*FZoomOut/FZoomIn, int(FBitmap->Width-sx)); \ ph = min(Height*FZoomOut/FZoomIn, int(FBitmap->Height-sy)); \ if (Src.left<0) Src.left = 0; \ if (Src.top<0) Src.top = 0; \ if (Src.right>pw) Src.right = pw; \ if (Src.bottom>ph) Src.bottom = ph; \ if (FZoomOut>1) { \ Src.left -= Src.left%FZoomOut; \ Src.top -= Src.top%FZoomOut; \ Src.right = Src.right-Src.right%FZoomOut; \ Src.bottom = Src.bottom-Src.bottom%FZoomOut; \ } \ pw = Src.right-Src.left; \ ph = Src.bottom-Src.top; \ vw = pw*FZoomIn/FZoomOut; \ vh = ph*FZoomIn/FZoomOut; \ px = Src.left*FZoomIn/FZoomOut; \ py = Src.top*FZoomIn/FZoomOut; \ sx += Src.left; \ sy += Src.top; //--------------------------------------------------------------------------- void __fastcall TPTextileImage::RectPaint(RECT Src) { BITMAPHANDLE *bh, *mh; HPALETTE hpaintpal = NULL, holdpal = NULL; int px, py, pw, ph, vw, vh; double sx, sy, gx, gy, gzx, gzy, p; HDC dcSrc = NULL, dcDst, dcMemV = NULL, dcMSrc = NULL; HBITMAP bmMemV = NULL; HPEN hPen = NULL, hOldPen; RECT Temp = Src; bh = FBitmap->Handle; if (FMaskEnabled) { mh = FMask->Handle; if (mh && bh) { if (Src.leftWidth && Src.right>=FPositionX && Src.topHeight && Src.bottom>=FPositionY) { RECTPAINT_INIT; if ((dcMSrc = FMask->CreateDC()) == NULL) goto fail; if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; if ((bmMemV = CreateCompatibleBitmap(dcSrc, vw, vh)) == NULL) goto fail; if ((dcMemV = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemV, bmMemV); if (hpaintpal) SelectPalette(dcMemV, hpaintpal, false); SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, 0, 0, vw, vh, dcSrc, sx, sy, pw, ph, SRCCOPY); StretchBlt(dcMemV, 0, 0, vw, vh, dcMSrc, sx, sy, pw, ph, SRCINVERT); if (FGrid) { gx = FGridGapX; gy = FGridGapY; gzx = gx*FZoomIn/FZoomOut; gzy = gy*FZoomIn/FZoomOut; if (gzx>=5 && gzy>=5) { if (FParentImage) { FGridColor = FParentImage->GridColor; FGridGapX = FParentImage->GridGapX; FGridGapY = FParentImage->GridGapY; FGridMark = FParentImage->GridMark; FGridMarkColor = FParentImage->GridMarkColor; sx = FParentImage->PositionX+(Left-FParentImage->Left)*FZoomOut/FZoomIn+Src.left; sy = FParentImage->PositionY+(Top-FParentImage->Top)*FZoomOut/FZoomIn+Src.top; } PAINT_GRIDLINE; } } if (FOnPaintRange) FOnPaintRange(this, dcMemV, true, Temp.left, Temp.top); BitBlt(dcDst, px, py, vw, vh, dcMemV, 0, 0, SRCCOPY); DeleteDC(dcMemV); DeleteObject(bmMemV); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } FBitmap->DeleteDC(dcSrc); FMask->DeleteDC(dcMSrc); } } } else { if (bh) { if (Src.leftWidth && Src.right>=FPositionX && Src.topHeight && Src.bottom>=FPositionY) { RECTPAINT_INIT; if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; if ((bmMemV = CreateCompatibleBitmap(dcSrc, vw, vh)) == NULL) goto fail; if ((dcMemV = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemV, bmMemV); if (hpaintpal) SelectPalette(dcMemV, hpaintpal, false); SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, 0, 0, vw, vh, dcSrc, sx, sy, pw, ph, SRCCOPY); if (FGrid) { gx = FGridGapX; gy = FGridGapY; gzx = gx*FZoomIn/FZoomOut; gzy = gy*FZoomIn/FZoomOut; if (gzx>=5 && gzy>=5) { if (FParentImage) { FGridColor = FParentImage->GridColor; FGridGapX = FParentImage->GridGapX; FGridGapY = FParentImage->GridGapY; FGridMark = FParentImage->GridMark; FGridMarkColor = FParentImage->GridMarkColor; sx = FParentImage->PositionX+(Left-FParentImage->Left)*FZoomOut/FZoomIn+Src.left; sy = FParentImage->PositionY+(Top-FParentImage->Top)*FZoomOut/FZoomIn+Src.top; } PAINT_GRIDLINE; } } if (FOnPaintRange) FOnPaintRange(this, dcMemV, true, Temp.left, Temp.top); BitBlt(dcDst, px, py, vw, vh, dcMemV, 0, 0, SRCCOPY); DeleteDC(dcMemV); DeleteObject(bmMemV); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } FBitmap->DeleteDC(dcSrc); } } } return; fail: if (dcSrc) { if (bmMemV) { if (dcMemV) { if (hPen) DeleteObject(hPen); DeleteDC(dcMemV); } DeleteObject(bmMemV); } FBitmap->DeleteDC(dcSrc); } if (dcMSrc) FMask->DeleteDC(dcMSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //---------------------------------------------------------------------------- void __fastcall TPTextileImage::RectEditPaint(RECT Src, TRectEditPaint func, void *pData) { BITMAPHANDLE *bh, *mh; HPALETTE hpaintpal = NULL, holdpal = NULL; int px, py, pw, ph, vw, vh; double sx, sy, gx, gy, gzx, gzy, p; HDC dcSrc = NULL, dcDst, dcMemP = NULL, dcMemV = NULL, dcMSrc = NULL; HBITMAP bmMemP = NULL, bmMemV = NULL; HPEN hPen = NULL, hOldPen; RECT Temp = Src; bh = FBitmap->Handle; if (FMaskEnabled) { mh = FMask->Handle; if (mh && bh) { if (Src.leftWidth && Src.right>=FPositionX && Src.topHeight && Src.bottom>=FPositionY) { RECTPAINT_INIT; if ((dcMSrc = FMask->CreateDC()) == NULL) goto fail; if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; if ((bmMemP = CreateCompatibleBitmap(dcSrc, pw, ph)) == NULL) goto fail; if ((dcMemP = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemP, bmMemP); if (hpaintpal) SelectPalette(dcMemP, hpaintpal, false); BitBlt(dcMemP, 0, 0, pw, ph, dcSrc, sx, sy, SRCCOPY); BitBlt(dcMemP, 0, 0, pw, ph, dcMSrc, sx, sy, SRCINVERT); if (func) func(dcMemP, sx, sy, pData); if ((bmMemV = CreateCompatibleBitmap(dcSrc, vw, vh)) == NULL) goto fail; if ((dcMemV = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemV, bmMemV); if (hpaintpal) SelectPalette(dcMemV, hpaintpal, false); SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, 0, 0, vw, vh, dcMemP, 0, 0, pw, ph, SRCCOPY); if (FGrid) { gx = FGridGapX; gy = FGridGapY; gzx = gx*FZoomIn/FZoomOut; gzy = gy*FZoomIn/FZoomOut; if (gzx>=5 && gzy>=5) { if (FParentImage) { FGridColor = FParentImage->GridColor; FGridGapX = FParentImage->GridGapX; FGridGapY = FParentImage->GridGapY; FGridMark = FParentImage->GridMark; FGridMarkColor = FParentImage->GridMarkColor; sx = FParentImage->PositionX+(Left-FParentImage->Left)*FZoomOut/FZoomIn+Src.left; sy = FParentImage->PositionY+(Top-FParentImage->Top)*FZoomOut/FZoomIn+Src.top; } PAINT_GRIDLINE; } } if (FOnPaintRange) FOnPaintRange(this, dcMemV, true, Temp.left, Temp.top); BitBlt(dcDst, px, py, vw, vh, dcMemV, 0, 0, SRCCOPY); DeleteDC(dcMemV); DeleteObject(bmMemV); DeleteDC(dcMemP); DeleteObject(bmMemP); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } FBitmap->DeleteDC(dcSrc); FMask->DeleteDC(dcMSrc); } } } else { if (bh) { if (Src.leftWidth && Src.right>=FPositionX && Src.topHeight && Src.bottom>=FPositionY) { RECTPAINT_INIT; if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; if ((bmMemP = CreateCompatibleBitmap(dcSrc, pw, ph)) == NULL) goto fail; if ((dcMemP = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemP, bmMemP); if (hpaintpal) SelectPalette(dcMemP, hpaintpal, false); BitBlt(dcMemP, 0, 0, pw, ph, dcSrc, sx, sy, SRCCOPY); if (func) func(dcMemP, sx, sy, pData); if ((bmMemV = CreateCompatibleBitmap(dcSrc, vw, vh)) == NULL) goto fail; if ((dcMemV = CreateCompatibleDC(dcSrc)) == NULL) goto fail; SelectObject(dcMemV, bmMemV); if (hpaintpal) SelectPalette(dcMemV, hpaintpal, false); SetStretchBltMode(dcMemV, COLORONCOLOR); StretchBlt(dcMemV, 0, 0, vw, vh, dcMemP, 0, 0, pw, ph, SRCCOPY); if (FGrid) { gx = FGridGapX; gy = FGridGapY; gzx = gx*FZoomIn/FZoomOut; gzy = gy*FZoomIn/FZoomOut; if (gzx>=5 && gzy>=5) { if (FParentImage) { FGridColor = FParentImage->GridColor; FGridGapX = FParentImage->GridGapX; FGridGapY = FParentImage->GridGapY; FGridMark = FParentImage->GridMark; FGridMarkColor = FParentImage->GridMarkColor; sx = FParentImage->PositionX+(Left-FParentImage->Left)*FZoomOut/FZoomIn+Src.left; sy = FParentImage->PositionY+(Top-FParentImage->Top)*FZoomOut/FZoomIn+Src.top; } PAINT_GRIDLINE; } } if (FOnPaintRange) FOnPaintRange(this, dcMemV, true, Temp.left, Temp.top); BitBlt(dcDst, px, py, vw, vh, dcMemV, 0, 0, SRCCOPY); DeleteDC(dcMemV); DeleteObject(bmMemV); DeleteDC(dcMemP); DeleteObject(bmMemP); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } FBitmap->DeleteDC(dcSrc); } } } return; fail: if (dcSrc) { if (bmMemP) { if (dcMemP) { if (bmMemV) { if (dcMemV) { if (hPen) DeleteObject(hPen); DeleteDC(dcMemV); } DeleteObject(bmMemV); } DeleteDC(dcMemP); } DeleteObject(bmMemP); } FBitmap->DeleteDC(dcSrc); } if (dcMSrc) FMask->DeleteDC(dcMSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //--------------------------------------------------------------------------- void __fastcall TPTextileImage::PaintImageEdit(TEditPaintEvent func) { BITMAPHANDLE *bh; HPALETTE hpaintpal = NULL, holdpal = NULL; int pw, ph, vw, vh; HDC dcDst, dcMem = NULL; HBITMAP bmMem = NULL; bh = FBitmap->Handle; if (bh) { dcDst = TGraphicControl::Canvas->Handle; if (FBitmap->BitsPerPixel==8) { hpaintpal = L_CreatePaintPalette(dcDst, bh); if (hpaintpal) holdpal = SelectPalette(dcDst, hpaintpal, false); } pw = Width*FZoomOut/FZoomIn; ph = Height*FZoomOut/FZoomIn; if (pw>FBitmap->Width-FPositionX) pw = FBitmap->Width-FPositionX; if (ph>FBitmap->Height-FPositionY) ph = FBitmap->Height-FPositionY; if (FZoomOut>1) { pw -= pw%FZoomOut; ph -= ph%FZoomOut; } vw = pw*FZoomIn/FZoomOut; vh = ph*FZoomIn/FZoomOut; if ((bmMem = CreateCompatibleBitmap(dcDst, vw, vh)) == NULL) goto fail; if ((dcMem = CreateCompatibleDC(dcDst)) == NULL) goto fail; SelectObject(dcMem, bmMem); if (hpaintpal) SelectPalette(dcMem, hpaintpal, false); BitBlt(dcMem, 0, 0, vw, vh, dcDst, 0, 0, SRCCOPY); if (func) func(this, dcMem); BitBlt(dcDst, 0, 0, vw, vh, dcMem, 0, 0, SRCCOPY); DeleteDC(dcMem); DeleteObject(bmMem); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } return; fail: if (bmMem) { if (dcMem) DeleteDC(dcMem); DeleteObject(bmMem); } if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //--------------------------------------------------------------------------- void __fastcall TPTextileImage::FullViewPaint(HDC dcDst, int zi, int zo) { HPALETTE hpaintpal = NULL, holdpal = NULL; int pw, ph, vw, vh; HDC dcSrc = NULL; vw = FBitmap->Width *zi / zo; vh = FBitmap->Height *zi / zo; pw = vw * zo / zi; ph = vh * zo / zi; if (FBitmap->BitsPerPixel==8) { hpaintpal = L_CreatePaintPalette(dcDst, FBitmap->Handle); if (hpaintpal) holdpal = SelectPalette(dcDst, hpaintpal, false); } if ((dcSrc = FBitmap->CreateDC()) == NULL) goto fail; SetStretchBltMode(dcDst, COLORONCOLOR); StretchBlt(dcDst, 0, 0, vw, vh, dcSrc, 0, 0, pw, ph, SRCCOPY); FBitmap->DeleteDC(dcSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } return; fail: if (dcSrc) FBitmap->DeleteDC(dcSrc); if (hpaintpal) { if (holdpal) SelectPalette(dcDst, holdpal, false); DeleteObject(hpaintpal); } } //--------------------------------------------------------------------------- int __fastcall TPTextileImage::BitmapToCanvasX(int X) { return (X-FPositionX)*ZoomIn/ZoomOut; } //--------------------------------------------------------------------------- int __fastcall TPTextileImage::BitmapToCanvasY(int Y) { return (Y-FPositionY)*ZoomIn/ZoomOut; } //--------------------------------------------------------------------------- int __fastcall TPTextileImage::RectBitmapToRectCanvasX(int X, int left) { if (left > FPositionX) { return (X - left)*ZoomIn/ZoomOut; } return (X - FPositionX)*ZoomIn/ZoomOut; } //--------------------------------------------------------------------------- int __fastcall TPTextileImage::RectBitmapToRectCanvasY(int Y, int top) { if (top > FPositionY) { return (Y - top)*ZoomIn/ZoomOut; } return (Y - FPositionY)*ZoomIn/ZoomOut; } //--------------------------------------------------------------------------- int __fastcall TPTextileImage::CanvasToBitmapX(int X) { if (FRepeat) { return (FPositionX+X*FZoomOut/FZoomIn)%FBitmap->Width; } else { return FPositionX+X*FZoomOut/FZoomIn; } } //--------------------------------------------------------------------------- int __fastcall TPTextileImage::CanvasToBitmapY(int Y) { if (FRepeat) { return (FPositionY+Y*FZoomOut/FZoomIn)%FBitmap->Height; } else { return FPositionY+Y*FZoomOut/FZoomIn; } } //--------------------------------------------------------------------------- namespace Tptextileimage { void __fastcall PACKAGE Register() { TComponentClass classes[1] = {__classid(TPTextileImage)}; RegisterComponents("Texpia", classes, 0); } } //---------------------------------------------------------------------------