ReactOS 0.4.16-dev-91-g764881a
palette.cpp
Go to the documentation of this file.
1/*
2 * PROJECT: PAINT for ReactOS
3 * LICENSE: LGPL-2.0-or-later (https://spdx.org/licenses/LGPL-2.0-or-later)
4 * PURPOSE: Window procedure of the palette window
5 * COPYRIGHT: Copyright 2015 Benedikt Freisen <b.freisen@gmx.net>
6 * Copyright 2023 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
7 */
8
9#include "precomp.h"
10
11/* The private metrics */
12#define CXY_SELECTEDBOX 15 /* width / height of a selected color box */
13#define X_MARGIN 4 /* horizontal margin */
14#define Y_MARGIN ((rcClient.bottom / 2) - CXY_COLORBOX) /* center position minus one color box */
15#define X_COLORBOX_OFFSET (X_MARGIN + CXY_BIGBOX + X_MARGIN)
16#define COLOR_COUNT 28
17#define HALF_COLOR_COUNT (COLOR_COUNT / 2)
18
20
21/* FUNCTIONS ********************************************************/
22
24 : m_hbmCached(NULL)
25{
26}
27
29{
30 if (m_hbmCached)
32}
33
34static VOID drawColorBox(HDC hDC, LPCRECT prc, COLORREF rgbColor, UINT nBorder)
35{
36 RECT rc = *prc;
37 ::FillRect(hDC, &rc, (HBRUSH)(COLOR_3DFACE + 1));
38 ::DrawEdge(hDC, &rc, nBorder, BF_RECT | BF_ADJUST);
39
40 HBRUSH hbr = ::CreateSolidBrush(rgbColor);
41 ::FillRect(hDC, &rc, hbr);
42 ::DeleteObject(hbr);
43}
44
45static VOID getColorBoxRect(LPRECT prc, const RECT& rcClient, INT iColor)
46{
47 INT dx = (iColor % HALF_COLOR_COUNT) * CXY_COLORBOX; /* delta x */
48 INT dy = (iColor / HALF_COLOR_COUNT) * CXY_COLORBOX; /* delta y */
51 prc->top = Y_MARGIN + dy;
53}
54
56{
57 RECT rcClient;
58 GetClientRect(&rcClient);
59
60 /* delta x and y */
61 INT dx = (xPos - X_COLORBOX_OFFSET), dy = (yPos - Y_MARGIN);
62
63 /* horizontal and vertical indexes */
64 INT ix = (dx / CXY_COLORBOX), iy = (dy / CXY_COLORBOX);
65
66 /* Is it inside of a color box? */
67 if (0 <= ix && ix < HALF_COLOR_COUNT && 0 <= iy && iy < 2)
68 return ix + (iy * HALF_COLOR_COUNT); /* return the color index */
69
70 return -1; /* Not found */
71}
72
74{
75 return TRUE; /* Avoid flickering */
76}
77
79{
80 RECT rc, rcClient;
81 GetClientRect(&rcClient);
82
83 PAINTSTRUCT ps;
84 HDC hDC = BeginPaint(&ps);
85
86 /* To avoid flickering, we use a memory bitmap.
87 The left and top values are zeros in client rectangle */
89 m_hbmCached = CachedBufferDIB(m_hbmCached, rcClient.right, rcClient.bottom);
90 HGDIOBJ hbmOld = ::SelectObject(hMemDC, m_hbmCached);
91
92 /* Fill the background (since WM_ERASEBKGND handling is disabled) */
93 ::FillRect(hMemDC, &rcClient, (HBRUSH)(COLOR_3DFACE + 1));
94
95 /* Draw the big box that contains the black box and the white box */
97 ::DrawEdge(hMemDC, &rc, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
99 for (INT y = rc.top; y < rc.bottom; ++y)
100 {
101 BOOL bLight = (y & 1);
102 for (INT x = rc.left; x < rc.right; ++x)
103 {
104 if (bLight)
105 ::SetPixelV(hMemDC, x, y, rgbLight);
106 bLight = !bLight;
107 }
108 }
109
110 /* Draw the white box in the big box, at 5/8 position */
111 rc.left = X_MARGIN + (CXY_BIGBOX * 5 / 8) - (CXY_SELECTEDBOX / 2);
112 rc.top = Y_MARGIN + (CXY_BIGBOX * 5 / 8) - (CXY_SELECTEDBOX / 2);
113 rc.right = rc.left + CXY_SELECTEDBOX;
114 rc.bottom = rc.top + CXY_SELECTEDBOX;
116
117 /* Draw the black box (overlapping the white box), at 3/8 position */
118 rc.left = X_MARGIN + (CXY_BIGBOX * 3 / 8) - (CXY_SELECTEDBOX / 2);
119 rc.top = Y_MARGIN + (CXY_BIGBOX * 3 / 8) - (CXY_SELECTEDBOX / 2);
120 rc.right = rc.left + CXY_SELECTEDBOX;
121 rc.bottom = rc.top + CXY_SELECTEDBOX;
123
124 /* Draw the normal color boxes */
125 for (INT i = 0; i < COLOR_COUNT; i++)
126 {
127 getColorBoxRect(&rc, rcClient, i);
129 }
130
131 /* Transfer bits (hDC <-- hMemDC) */
132 ::BitBlt(hDC, 0, 0, rcClient.right, rcClient.bottom, hMemDC, 0, 0, SRCCOPY);
133
134 ::SelectObject(hMemDC, hbmOld);
135 ::DeleteDC(hMemDC);
136 EndPaint(&ps);
137 return 0;
138}
139
141{
143 if (iColor != -1)
145 SetCapture();
146 return 0;
147}
148
150{
152 if (iColor != -1)
154 return 0;
155}
156
158{
160 COLORREF rgbColor = paletteModel.GetFgColor();
161 if (iColor != -1 && mainWindow.ChooseColor(&rgbColor))
162 {
163 paletteModel.SetColor(iColor, rgbColor);
164 paletteModel.SetFgColor(rgbColor);
165 }
166 return 0;
167}
168
170{
172 COLORREF rgbColor = paletteModel.GetBgColor();
173 if (iColor != -1 && mainWindow.ChooseColor(&rgbColor))
174 {
175 paletteModel.SetColor(iColor, rgbColor);
176 paletteModel.SetBgColor(rgbColor);
177 }
178 return 0;
179}
180
182{
183 Invalidate(FALSE);
184 return 0;
185}
186
188{
189 if (::GetCapture() != m_hWnd)
190 return 0;
191
194
195 RECT rc;
197
198 POINT ptCenter = { (rc.left + rc.right) / 2, (rc.top + rc.bottom) / 2 };
199
200 DWORD dwExpectedBar1ID = ((pt.y < ptCenter.y) ? BAR1ID_TOP : BAR1ID_BOTTOM);
201
202 if (registrySettings.Bar1ID != dwExpectedBar1ID)
203 {
204 registrySettings.Bar1ID = dwExpectedBar1ID;
206 }
207
208 return 0;
209}
210
212{
213 if (::GetCapture() != m_hWnd)
214 return 0;
215
217 return 0;
218}
static HDC hDC
Definition: 3dtext.c:33
CMainWindow mainWindow
Definition: main.cpp:25
#define CXY_COLORBOX
Definition: palette.h:10
#define CXY_BIGBOX
Definition: palette.h:11
RegistrySettings registrySettings
Definition: registry.cpp:14
#define BAR1ID_TOP
Definition: registry.h:50
#define BAR1ID_BOTTOM
Definition: registry.h:51
BOOL GetWindowRect(LPRECT lpRect) const
Definition: atlwin.h:816
BOOL PostMessage(UINT message, WPARAM wParam=0, LPARAM lParam=0)
Definition: atlwin.h:1044
BOOL ChooseColor(IN OUT COLORREF *prgbColor)
Definition: main.cpp:282
virtual ~CPaletteWindow()
Definition: palette.cpp:28
LRESULT OnMouseMove(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: palette.cpp:187
LRESULT OnRButtonDblClk(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: palette.cpp:169
LRESULT OnPaletteModelColorChanged(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: palette.cpp:181
LRESULT OnLButtonUp(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: palette.cpp:211
LRESULT OnPaint(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: palette.cpp:78
LRESULT OnLButtonDblClk(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: palette.cpp:157
INT DoHitTest(INT xPos, INT yPos) const
Definition: palette.cpp:55
LRESULT OnEraseBkgnd(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: palette.cpp:73
HBITMAP m_hbmCached
Definition: palette.h:35
LRESULT OnRButtonDown(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: palette.cpp:149
LRESULT OnLButtonDown(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL &bHandled)
Definition: palette.cpp:140
COLORREF GetBgColor() const
void SetFgColor(COLORREF newColor)
void SetBgColor(COLORREF newColor)
void SetColor(UINT nIndex, COLORREF newColor)
COLORREF GetFgColor() const
COLORREF GetColor(UINT nIndex) const
WPARAM wParam
Definition: combotst.c:138
LPARAM lParam
Definition: combotst.c:139
HBITMAP CachedBufferDIB(HBITMAP hbm, int minimalWidth, int minimalHeight)
Definition: dib.cpp:115
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define pt(x, y)
Definition: drawing.c:79
static VOID BitBlt(_In_ ULONG Left, _In_ ULONG Top, _In_ ULONG Width, _In_ ULONG Height, _In_reads_bytes_(Delta *Height) PUCHAR Buffer, _In_ ULONG BitsPerPixel, _In_ ULONG Delta)
Definition: common.c:57
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
pKey DeleteObject()
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
GLint dy
Definition: linetemp.h:97
GLint dx
Definition: linetemp.h:97
static HDC
Definition: imagelist.c:88
unsigned int UINT
Definition: ndis.h:50
_Out_ LPRECT prc
Definition: ntgdi.h:1658
#define COLOR_COUNT
Definition: palette.cpp:16
#define CXY_SELECTEDBOX
Definition: palette.cpp:12
CPaletteWindow paletteWindow
Definition: palette.cpp:19
static VOID drawColorBox(HDC hDC, LPCRECT prc, COLORREF rgbColor, UINT nBorder)
Definition: palette.cpp:34
#define Y_MARGIN
Definition: palette.cpp:14
static VOID getColorBoxRect(LPRECT prc, const RECT &rcClient, INT iColor)
Definition: palette.cpp:45
#define HALF_COLOR_COUNT
Definition: palette.cpp:17
#define X_MARGIN
Definition: palette.cpp:13
#define X_COLORBOX_OFFSET
Definition: palette.cpp:15
PaletteModel paletteModel
long y
Definition: polytest.cpp:48
LONG right
Definition: windef.h:308
LONG bottom
Definition: windef.h:309
LONG top
Definition: windef.h:307
LONG left
Definition: windef.h:306
int iy
Definition: tritemp.h:491
int32_t INT
Definition: typedefs.h:58
LONG_PTR LPARAM
Definition: windef.h:208
LONG_PTR LRESULT
Definition: windef.h:209
UINT_PTR WPARAM
Definition: windef.h:207
DWORD COLORREF
Definition: windef.h:300
#define GET_Y_LPARAM(lp)
Definition: windowsx.h:300
#define GET_X_LPARAM(lp)
Definition: windowsx.h:299
HGDIOBJ WINAPI SelectObject(_In_ HDC, _In_ HGDIOBJ)
Definition: dc.c:1546
HDC WINAPI CreateCompatibleDC(_In_opt_ HDC hdc)
#define SRCCOPY
Definition: wingdi.h:333
BOOL WINAPI SetPixelV(_In_ HDC, _In_ int, _In_ int, _In_ COLORREF)
int WINAPI FillRect(HDC, LPCRECT, HBRUSH)
HBRUSH WINAPI CreateSolidBrush(_In_ COLORREF)
BOOL WINAPI DeleteDC(_In_ HDC)
HWND WINAPI SetCapture(_In_ HWND hWnd)
DWORD WINAPI GetSysColor(_In_ int)
#define BDR_SUNKENOUTER
Definition: winuser.h:443
#define EDGE_SUNKEN
Definition: winuser.h:451
BOOL WINAPI ReleaseCapture(void)
Definition: message.c:2890
#define WM_SIZE
Definition: winuser.h:1614
#define COLOR_3DHIGHLIGHT
Definition: winuser.h:939
#define BF_ADJUST
Definition: winuser.h:470
HWND WINAPI GetCapture(void)
Definition: message.c:2881
BOOL WINAPI ClientToScreen(_In_ HWND, _Inout_ LPPOINT)
BOOL WINAPI GetClientRect(_In_ HWND, _Out_ LPRECT)
BOOL WINAPI DrawEdge(_In_ HDC, _Inout_ LPRECT, _In_ UINT, _In_ UINT)
BOOL WINAPI EndPaint(_In_ HWND, _In_ const PAINTSTRUCT *)
#define BDR_RAISEDINNER
Definition: winuser.h:444
HDC WINAPI BeginPaint(_In_ HWND, _Out_ LPPAINTSTRUCT)
#define BF_RECT
Definition: winuser.h:462
#define COLOR_3DFACE
Definition: winuser.h:932
_In_ ULONG iColor
Definition: xlateobj.h:17