ReactOS  0.4.12-dev-51-ge94618b
selectionmodel.cpp
Go to the documentation of this file.
1 /*
2  * PROJECT: PAINT for ReactOS
3  * LICENSE: LGPL
4  * FILE: base/applications/mspaint/selectionmodel.cpp
5  * PURPOSE: Keep track of selection parameters, notify listeners
6  * PROGRAMMERS: Benedikt Freisen
7  */
8 
9 /* INCLUDES *********************************************************/
10 
11 #include "precomp.h"
12 
13 /* FUNCTIONS ********************************************************/
14 
16 {
17  m_ptStack = NULL;
18  m_iPtSP = 0;
19 
21 }
22 
24 {
25  if (m_ptStack != NULL)
27  m_ptStack = NULL;
28  m_iPtSP = 0;
29 }
30 
32 {
33  if (m_iPtSP % 1024 == 0)
34  {
35  if (m_ptStack)
37  else
38  m_ptStack = (POINT*) HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, sizeof(POINT) * 1024);
39  }
40  m_ptStack[m_iPtSP].x = x;
41  m_ptStack[m_iPtSP].y = y;
42  m_iPtSP++;
43 }
44 
46 {
47  int i;
50  for (i = 0; i < m_iPtSP; i++)
51  {
52  if (m_ptStack[i].x < m_rcSrc.left)
54  if (m_ptStack[i].y < m_rcSrc.top)
55  m_rcSrc.top = m_ptStack[i].y;
56  if (m_ptStack[i].x > m_rcSrc.right)
58  if (m_ptStack[i].y > m_rcSrc.bottom)
60  }
61  m_rcSrc.right += 1;
62  m_rcSrc.bottom += 1;
67 
68  if (m_iPtSP > 1)
69  {
73  POINT *m_ptStackCopy = (POINT*) HeapAlloc(GetProcessHeap(), HEAP_GENERATE_EXCEPTIONS, sizeof(POINT) * m_iPtSP);
74  for (i = 0; i < m_iPtSP; i++)
75  {
76  m_ptStackCopy[i].x = m_ptStack[i].x - m_rcSrc.left;
77  m_ptStackCopy[i].y = m_ptStack[i].y - m_rcSrc.top;
78  }
79  Poly(m_hDC, m_ptStackCopy, m_iPtSP, 0x00ffffff, 0x00ffffff, 1, 2, TRUE, FALSE);
80  HeapFree(GetProcessHeap(), 0, m_ptStackCopy);
85  }
86 }
87 
89 {
93  Rect(m_hDC, 0, 0, RECT_WIDTH(m_rcSrc), RECT_HEIGHT(m_rcSrc), 0x00ffffff, 0x00ffffff, 1, 2);
97 }
98 
100 {
101  Poly(hDCImage, m_ptStack, m_iPtSP, crBg, crBg, 1, 2, TRUE, FALSE);
102 }
103 
105 {
106  Rect(hDCImage, m_rcSrc.left, m_rcSrc.top, m_rcSrc.right, m_rcSrc.bottom, crBg, crBg, 0, 1);
107 }
108 
109 extern BOOL
110 ColorKeyedMaskBlt(HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, HDC hdcSrc, int nXSrc, int nYSrc, HBITMAP hbmMask, int xMask, int yMask, DWORD dwRop, COLORREF keyColor);
111 
112 void SelectionModel::DrawSelection(HDC hDCImage, COLORREF crBg, BOOL bBgTransparent)
113 {
114  if (!bBgTransparent)
116  m_hDC, 0, 0, m_hMask, 0, 0, MAKEROP4(SRCCOPY, SRCAND));
117  else
119  m_hDC, 0, 0, m_hMask, 0, 0, MAKEROP4(SRCCOPY, SRCAND), crBg);
120 }
121 
123 {
125 }
126 
128 {
129  HDC hTempDC;
130  HBITMAP hTempBm;
131  hTempDC = CreateCompatibleDC(m_hDC);
133  SelectObject(hTempDC, hTempBm);
135  StretchBlt(hTempDC, 0, 0, RECT_WIDTH(m_rcDest), RECT_HEIGHT(m_rcDest), m_hDC, 0, 0,
138  m_hBm = hTempBm;
140  SelectObject(hTempDC, hTempBm);
142  StretchBlt(hTempDC, 0, 0, RECT_WIDTH(m_rcDest), RECT_HEIGHT(m_rcDest), m_hDC, 0, 0,
145  m_hMask = hTempBm;
147  DeleteDC(hTempDC);
148 }
149 
151 {
152  HDC hTempDC;
153  HBITMAP hTempMask;
154 
156  IMAGE_BITMAP, 0, 0,
157  LR_COPYRETURNORG)));
158 
160  m_rcDest.left = m_rcDest.top = 0;
163 
164  hTempDC = CreateCompatibleDC(m_hDC);
165  hTempMask = CreateBitmap(RECT_WIDTH(m_rcDest), RECT_HEIGHT(m_rcDest), 1, 1, NULL);
166  SelectObject(hTempDC, hTempMask);
167  Rect(hTempDC, m_rcDest.left, m_rcDest.top, m_rcDest.right, m_rcDest.bottom, 0x00ffffff, 0x00ffffff, 1, 1);
169  m_hMask = hTempMask;
170  DeleteDC(hTempDC);
171 }
172 
174 {
182 }
183 
185 {
193 }
194 
196 {
197  if (iN == 2)
198  {
205  }
207 }
208 
210 {
211  return m_hBm;
212 }
213 
215 {
216  return m_iPtSP;
217 }
218 
220 {
221  Poly(hDCImage, m_ptStack, m_iPtSP, 0, 0, 2, 0, FALSE, TRUE); /* draw the freehand selection inverted/xored */
222 }
223 
225 {
226  m_rcDest.left = m_rcSrc.left = min(ptFrom.x, ptTo.x);
227  m_rcDest.top = m_rcSrc.top = min(ptFrom.y, ptTo.y);
228  m_rcDest.right = m_rcSrc.right = max(ptFrom.x, ptTo.x);
229  m_rcDest.bottom = m_rcSrc.bottom = max(ptFrom.y, ptTo.y);
230 }
231 
233 {
236 }
237 
239 {
240  return (RECT_WIDTH(m_rcSrc) != 0) && (RECT_HEIGHT(m_rcSrc) != 0);
241 }
242 
243 void SelectionModel::ModifyDestRect(POINT& ptDelta, int iAction)
244 {
245  POINT ptDeltaUsed;
246 
247  switch (iAction)
248  {
249  case ACTION_MOVE: /* move selection */
250  ptDeltaUsed.x = ptDelta.x;
251  ptDeltaUsed.y = ptDelta.y;
252  OffsetRect(&m_rcDest, ptDeltaUsed.x, ptDeltaUsed.y);
253  break;
254  case ACTION_RESIZE_TOP_LEFT: /* resize at upper left corner */
255  ptDeltaUsed.x = min(ptDelta.x, RECT_WIDTH(m_rcDest) - 1);
256  ptDeltaUsed.y = min(ptDelta.y, RECT_HEIGHT(m_rcDest) - 1);
257  m_rcDest.left += ptDeltaUsed.x;
258  m_rcDest.top += ptDeltaUsed.y;
259  break;
260  case ACTION_RESIZE_TOP: /* resize at top edge */
261  ptDeltaUsed.x = ptDelta.x;
262  ptDeltaUsed.y = min(ptDelta.y, RECT_HEIGHT(m_rcDest) - 1);
263  m_rcDest.top += ptDeltaUsed.y;
264  break;
265  case ACTION_RESIZE_TOP_RIGHT: /* resize at upper right corner */
266  ptDeltaUsed.x = max(ptDelta.x, -(RECT_WIDTH(m_rcDest) - 1));
267  ptDeltaUsed.y = min(ptDelta.y, RECT_HEIGHT(m_rcDest) - 1);
268  m_rcDest.top += ptDeltaUsed.y;
269  m_rcDest.right += ptDeltaUsed.x;
270  break;
271  case ACTION_RESIZE_LEFT: /* resize at left edge */
272  ptDeltaUsed.x = min(ptDelta.x, RECT_WIDTH(m_rcDest) - 1);
273  ptDeltaUsed.y = ptDelta.y;
274  m_rcDest.left += ptDeltaUsed.x;
275  break;
276  case ACTION_RESIZE_RIGHT: /* resize at right edge */
277  ptDeltaUsed.x = max(ptDelta.x, -(RECT_WIDTH(m_rcDest) - 1));
278  ptDeltaUsed.y = ptDelta.y;
279  m_rcDest.right += ptDeltaUsed.x;
280  break;
281  case ACTION_RESIZE_BOTTOM_LEFT: /* resize at lower left corner */
282  ptDeltaUsed.x = min(ptDelta.x, RECT_WIDTH(m_rcDest) - 1);
283  ptDeltaUsed.y = max(ptDelta.y, -(RECT_HEIGHT(m_rcDest) - 1));
284  m_rcDest.left += ptDeltaUsed.x;
285  m_rcDest.bottom += ptDeltaUsed.y;
286  break;
287  case ACTION_RESIZE_BOTTOM: /* resize at bottom edge */
288  ptDeltaUsed.x = ptDelta.x;
289  ptDeltaUsed.y = max(ptDelta.y, -(RECT_HEIGHT(m_rcDest) - 1));
290  m_rcDest.bottom += ptDeltaUsed.y;
291  break;
292  case ACTION_RESIZE_BOTTOM_RIGHT: /* resize at lower right corner */
293  ptDeltaUsed.x = max(ptDelta.x, -(RECT_WIDTH(m_rcDest) - 1));
294  ptDeltaUsed.y = max(ptDelta.y, -(RECT_HEIGHT(m_rcDest) - 1));
295  m_rcDest.right += ptDeltaUsed.x;
296  m_rcDest.bottom += ptDeltaUsed.y;
297  break;
298  }
299  ptDelta.x -= ptDeltaUsed.x;
300  ptDelta.y -= ptDeltaUsed.y;
301 }
302 
304 {
305  return m_rcDest.right - m_rcDest.left;
306 }
307 
309 {
310  return m_rcDest.bottom - m_rcDest.top;
311 }
312 
314 {
315  return m_rcDest.left;
316 }
317 
319 {
320  return m_rcDest.top;
321 }
322 
323 void SelectionModel::DrawTextToolText(HDC hDCImage, COLORREF crFg, COLORREF crBg, BOOL bBgTransparent)
324 {
325  Text(hDCImage, m_rcDest.left, m_rcDest.top, m_rcDest.right, m_rcDest.bottom, crFg, crBg, textToolText, hfontTextFont, bBgTransparent);
326 }
327 
329 {
331 }
void DrawSelectionStretched(HDC hDCImage)
#define HDC
Definition: msvc.h:22
#define max(a, b)
Definition: svc.c:63
#define TRUE
Definition: types.h:120
void RotateNTimes90Degrees(int iN)
void DrawFramePoly(HDC hDCImage)
#define HBITMAP
Definition: msvc.h:28
long y
Definition: polytest.cpp:48
BOOL WINAPI OffsetRect(_Inout_ LPRECT, _In_ int, _In_ int)
#define MAXLONG
Definition: umtypes.h:116
long x
Definition: polytest.cpp:48
#define ACTION_RESIZE_TOP_LEFT
void PushToPtStack(LONG x, LONG y)
#define RECT_WIDTH(a)
Definition: definitions.h:19
LONG top
Definition: windef.h:297
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1497
BOOL WINAPI DeleteObject(_In_ HGDIOBJ)
#define ACTION_RESIZE_RIGHT
HDC WINAPI CreateCompatibleDC(_In_opt_ HDC hdc)
void CalculateContents(HDC hDCImage)
BOOL ColorKeyedMaskBlt(HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, HDC hdcSrc, int nXSrc, int nYSrc, HBITMAP hbmMask, int xMask, int yMask, DWORD dwRop, COLORREF keyColor)
Definition: selection.cpp:25
int GetDIBHeight(HBITMAP hBitmap)
Definition: dib.cpp:38
LONG left
Definition: windef.h:296
static HDC hdcSrc
Definition: xlate.c:32
LONG right
Definition: windef.h:298
static VOID NTAPI BitBlt(IN ULONG Left, IN ULONG Top, IN ULONG Width, IN ULONG Height, IN PUCHAR Buffer, IN ULONG BitsPerPixel, IN ULONG Delta)
Definition: vga.c:416
BOOL WINAPI StretchBlt(_In_ HDC, _In_ int, _In_ int, _In_ int, _In_ int, _In_opt_ HDC, _In_ int, _In_ int, _In_ int, _In_ int, _In_ DWORD)
INT INT y
Definition: msvc.h:62
int GetDIBWidth(HBITMAP hBitmap)
Definition: dib.cpp:30
#define LR_COPYRETURNORG
Definition: winuser.h:1080
#define ACTION_RESIZE_BOTTOM_LEFT
GLenum GLclampf GLint i
Definition: glfuncs.h:14
long LONG
Definition: pedump.c:60
HANDLE WINAPI CopyImage(_In_ HANDLE, _In_ UINT, _In_ int, _In_ int, _In_ UINT)
Definition: cursoricon.c:1956
smooth NULL
Definition: ftsmooth.c:416
HBITMAP CreateDIBWithProperties(int width, int height)
Definition: dib.cpp:16
#define MAKEROP4(f, b)
Definition: wingdi.h:2923
#define ACTION_RESIZE_TOP_RIGHT
void DrawBackgroundPoly(HDC hDCImage, COLORREF crBg)
void Poly(HDC hdc, POINT *lpPoints, int nCount, COLORREF fg, COLORREF bg, int thickness, int style, BOOL closed, BOOL inverted)
Definition: drawing.cpp:70
#define POINT
Definition: precomp.h:30
void ModifyDestRect(POINT &ptDelta, int iAction)
void DrawBackgroundRect(HDC hDCImage, COLORREF crBg)
unsigned int BOOL
Definition: ntddk_ex.h:94
#define ACTION_MOVE
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
HFONT hfontTextFont
Definition: main.cpp:23
unsigned long DWORD
Definition: ntddk_ex.h:95
#define ACTION_RESIZE_TOP
#define ACTION_RESIZE_LEFT
LPTSTR textToolText
Definition: main.cpp:25
ImageModel imageModel
Definition: main.cpp:32
BOOL WINAPI SetRectEmpty(_Out_ LPRECT)
#define WHITENESS
Definition: wingdi.h:335
#define RECT_HEIGHT(a)
Definition: definitions.h:22
void SetSrcRectSizeToZero()
BOOL WINAPI DeleteDC(_In_ HDC)
void InsertFromHBITMAP(HBITMAP hBm)
INT x
Definition: msvc.h:62
#define HeapReAlloc
Definition: compat.h:393
BOOL IsSrcRectSizeNonzero()
#define min(a, b)
Definition: monoChain.cc:55
#define SRCAND
Definition: wingdi.h:328
#define IMAGE_BITMAP
Definition: winuser.h:211
void DrawSelection(HDC hDCImage, COLORREF crBg=0, BOOL bBgTransparent=FALSE)
void CalculateBoundingBoxAndContents(HDC hDCImage)
LONG bottom
Definition: windef.h:299
#define ACTION_RESIZE_BOTTOM
BOOL WINAPI MaskBlt(_In_ HDC, _In_ int, _In_ int, _In_ int, _In_ int, _In_ HDC, _In_ int, _In_ int, _In_ HBITMAP, _In_ int, _In_ int, _In_ DWORD)
void NotifyRefreshNeeded()
void DrawTextToolText(HDC hDCImage, COLORREF crFg, COLORREF crBg, BOOL bBgTransparent=FALSE)
#define ACTION_RESIZE_BOTTOM_RIGHT
DWORD COLORREF
Definition: windef.h:290
#define HEAP_GENERATE_EXCEPTIONS
Definition: nt_native.h:1694
#define WM_SELECTIONMODELREFRESHNEEDED
Definition: definitions.h:248
void ResetToPrevious(void)
Definition: history.cpp:93
#define HeapFree(x, y, z)
Definition: compat.h:394
#define SRCCOPY
Definition: wingdi.h:331
struct Rect Rect
CSelectionWindow selectionWindow
Definition: main.cpp:70
char * Text
Definition: combotst.c:136
void SetSrcAndDestRectFromPoints(POINT &ptFrom, POINT &ptTo)
HBITMAP WINAPI CreateBitmap(_In_ INT cx, _In_ INT cy, _In_ UINT cPlanes, _In_ UINT cBitsPerPel, _In_opt_ const VOID *pvBits)