1 /*
2  * PROJECT:     PAINT for ReactOS
3  * LICENSE:     LGPL
4  * FILE:        base/applications/mspaint/toolsettings.cpp
5  * PURPOSE:     Window procedure of the tool settings window
6  * PROGRAMMERS: Benedikt Freisen
7  *              Stanislav Motylkov
8  */
9 
10 /* INCLUDES *********************************************************/
11 
12 #include "precomp.h"
13 
14 /* FUNCTIONS ********************************************************/
15 
16 LRESULT CToolSettingsWindow::OnCreate(UINT nMsg, WPARAM wParam, LPARAM lParam, WINBOOL& bHandled)
17 {
18     RECT trackbarZoomPos = {1, 1, 1 + 40, 1 + 64};
19     trackbarZoom.Create(TRACKBAR_CLASS, m_hWnd, trackbarZoomPos, NULL, WS_CHILD | TBS_VERT | TBS_AUTOTICKS);
20     trackbarZoom.SendMessage(TBM_SETRANGE, (WPARAM) TRUE, MAKELPARAM(0, 6));
21     trackbarZoom.SendMessage(TBM_SETPOS, (WPARAM) TRUE, (LPARAM) 3);
22     return 0;
23 }
24 
25 LRESULT CToolSettingsWindow::OnVScroll(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
26 {
27     if (!zoomTo(125 << trackbarZoom.SendMessage(TBM_GETPOS, 0, 0), 0, 0))
28     {
29         OnToolsModelZoomChanged(nMsg, wParam, lParam, bHandled);
30     }
31     return 0;
32 }
33 
34 LRESULT CToolSettingsWindow::OnPaint(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
35 {
36     PAINTSTRUCT ps;
37     RECT rect1 = { 0, 0, 42, 66 };
38     RECT rect2 = { 0, 70, 42, 136 };
39 
40     HDC hdc = BeginPaint(&ps);
41     DrawEdge(hdc, &rect1, BDR_SUNKENOUTER, (toolsModel.GetActiveTool() == TOOL_ZOOM) ? BF_RECT : BF_RECT | BF_MIDDLE);
42     DrawEdge(hdc, &rect2, (toolsModel.GetActiveTool() >= TOOL_RECT) ? BDR_SUNKENOUTER : 0, BF_RECT | BF_MIDDLE);
43     switch (toolsModel.GetActiveTool())
44     {
45         case TOOL_FREESEL:
46         case TOOL_RECTSEL:
47         case TOOL_TEXT:
48         {
49             HPEN oldPen = (HPEN) SelectObject(hdc, CreatePen(PS_NULL, 0, 0));
50             SelectObject(hdc, GetSysColorBrush(COLOR_HIGHLIGHT));
51             Rectangle(hdc, 2, toolsModel.IsBackgroundTransparent() * 31 + 2, 41, toolsModel.IsBackgroundTransparent() * 31 + 33);
52             DeleteObject(SelectObject(hdc, oldPen));
53             DrawIconEx(hdc, 1, 2, hNontranspIcon, 40, 30, 0, NULL, DI_NORMAL);
54             DrawIconEx(hdc, 1, 33, hTranspIcon, 40, 30, 0, NULL, DI_NORMAL);
55             break;
56         }
57         case TOOL_RUBBER:
58         {
59             int i;
60             HPEN oldPen = (HPEN) SelectObject(hdc, CreatePen(PS_NULL, 0, 0));
61             for(i = 0; i < 4; i++)
62             {
63                 if (toolsModel.GetRubberRadius() == i + 2)
64                 {
65                     SelectObject(hdc, GetSysColorBrush(COLOR_HIGHLIGHT));
66                     Rectangle(hdc, 14, i * 15 + 2, 29, i * 15 + 17);
67                     SelectObject(hdc, GetSysColorBrush(COLOR_HIGHLIGHTTEXT));
68                 }
69                 else
70                     SelectObject(hdc, GetSysColorBrush(COLOR_WINDOWTEXT));
71                 Rectangle(hdc, 19 - i, i * 14 + 7, 24 + i, i * 16 + 12);
72             }
73             DeleteObject(SelectObject(hdc, oldPen));
74             break;
75         }
76         case TOOL_BRUSH:
77         {
78             int i;
79             HPEN oldPen = (HPEN) SelectObject(hdc, CreatePen(PS_NULL, 0, 0));
80             SelectObject(hdc, GetSysColorBrush(COLOR_HIGHLIGHT));
81             Rectangle(hdc, toolsModel.GetBrushStyle() % 3 * 13 + 2, toolsModel.GetBrushStyle() / 3 * 15 + 2, toolsModel.GetBrushStyle() % 3 * 13 + 15,
82                       toolsModel.GetBrushStyle() / 3 * 15 + 17);
83             DeleteObject(SelectObject(hdc, oldPen));
84             for(i = 0; i < 12; i++)
85                 Brush(hdc, i % 3 * 13 + 7, i / 3 * 15 + 8, i % 3 * 13 + 7, i / 3 * 15 + 8,
86                       GetSysColor((i == toolsModel.GetBrushStyle()) ? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT), i);
87             break;
88         }
89         case TOOL_AIRBRUSH:
90         {
91             HPEN oldPen = (HPEN) SelectObject(hdc, CreatePen(PS_NULL, 0, 0));
92             SelectObject(hdc, GetSysColorBrush(COLOR_HIGHLIGHT));
93             switch (toolsModel.GetAirBrushWidth())
94             {
95                 case 5:
96                     Rectangle(hdc, 2, 2, 21, 31);
97                     break;
98                 case 8:
99                     Rectangle(hdc, 20, 2, 41, 31);
100                     break;
101                 case 3:
102                     Rectangle(hdc, 2, 30, 16, 61);
103                     break;
104                 case 12:
105                     Rectangle(hdc, 15, 30, 41, 61);
106                     break;
107             }
108             Airbrush(hdc, 10, 15,
109                      GetSysColor((toolsModel.GetAirBrushWidth() == 5) ? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT), 5);
110             Airbrush(hdc, 30, 15,
111                      GetSysColor((toolsModel.GetAirBrushWidth() == 8) ? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT), 8);
112             Airbrush(hdc, 8, 45,
113                      GetSysColor((toolsModel.GetAirBrushWidth() == 3) ? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT), 3);
114             Airbrush(hdc, 27, 45,
115                      GetSysColor((toolsModel.GetAirBrushWidth() == 12) ? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT), 12);
116             DeleteObject(SelectObject(hdc, oldPen));
117             break;
118         }
119         case TOOL_LINE:
120         case TOOL_BEZIER:
121         {
122             int i;
123             HPEN oldPen = (HPEN) SelectObject(hdc, CreatePen(PS_NULL, 0, 0));
124             for(i = 0; i < 5; i++)
125             {
126                 if (toolsModel.GetLineWidth() == i + 1)
127                 {
128                     SelectObject(hdc, GetSysColorBrush(COLOR_HIGHLIGHT));
129                     Rectangle(hdc, 2, i * 12 + 2, 41, i * 12 + 14);
130                     SelectObject(hdc, GetSysColorBrush(COLOR_HIGHLIGHTTEXT));
131                 }
132                 else
133                     SelectObject(hdc, GetSysColorBrush(COLOR_WINDOWTEXT));
134                 Rectangle(hdc, 5, i * 12 + 6, 38, i * 12 + 8 + i);
135             }
136             DeleteObject(SelectObject(hdc, oldPen));
137             break;
138         }
139         case TOOL_RECT:
140         case TOOL_SHAPE:
141         case TOOL_ELLIPSE:
142         case TOOL_RRECT:
143         {
144             int i;
145             HPEN oldPen = (HPEN) SelectObject(hdc, CreatePen(PS_NULL, 0, 0));
146             for(i = 0; i < 3; i++)
147             {
148                 if (toolsModel.GetShapeStyle() == i)
149                 {
150                     SelectObject(hdc, GetSysColorBrush(COLOR_HIGHLIGHT));
151                     Rectangle(hdc, 2, i * 20 + 2, 41, i * 20 + 22);
152                 }
153             }
154             Rect(hdc, 5, 6, 37, 16,
155                  GetSysColor((toolsModel.GetShapeStyle() == 0) ? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT),
156                  GetSysColor(COLOR_APPWORKSPACE), 1, 0);
157             Rect(hdc, 5, 26, 37, 36,
158                  GetSysColor((toolsModel.GetShapeStyle() == 1) ? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT),
159                  GetSysColor(COLOR_APPWORKSPACE), 1, 1);
160             Rect(hdc, 5, 46, 37, 56, GetSysColor(COLOR_APPWORKSPACE), GetSysColor(COLOR_APPWORKSPACE),
161                  1, 1);
162             for(i = 0; i < 5; i++)
163             {
164                 if (toolsModel.GetLineWidth() == i + 1)
165                 {
166                     SelectObject(hdc, GetSysColorBrush(COLOR_HIGHLIGHT));
167                     Rectangle(hdc, 2, i * 12 + 72, 41, i * 12 + 84);
168                     SelectObject(hdc, GetSysColorBrush(COLOR_HIGHLIGHTTEXT));
169                 }
170                 else
171                     SelectObject(hdc, GetSysColorBrush(COLOR_WINDOWTEXT));
172                 Rectangle(hdc, 5, i * 12 + 76, 38, i * 12 + 78 + i);
173             }
174             DeleteObject(SelectObject(hdc, oldPen));
175             break;
176         }
177         case TOOL_FILL:
178         case TOOL_COLOR:
179         case TOOL_ZOOM:
180         case TOOL_PEN:
181             break;
182     }
183     EndPaint(&ps);
184     return 0;
185 }
186 
187 LRESULT CToolSettingsWindow::OnLButtonDown(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
188 {
189     int x = GET_X_LPARAM(lParam);
190     int y = GET_Y_LPARAM(lParam);
191     switch (toolsModel.GetActiveTool())
192     {
193         case TOOL_FREESEL:
194         case TOOL_RECTSEL:
195         case TOOL_TEXT:
196             if ((y > 1) && (y < 64))
197                 toolsModel.SetBackgroundTransparent((y - 2) / 31);
198             break;
199         case TOOL_RUBBER:
200             if ((y > 1) && (y < 62))
201                 toolsModel.SetRubberRadius((y - 2) / 15 + 2);
202             break;
203         case TOOL_BRUSH:
204             if ((x > 1) && (x < 40) && (y > 1) && (y < 62))
205                 toolsModel.SetBrushStyle((y - 2) / 15 * 3 + (x - 2) / 13);
206             break;
207         case TOOL_AIRBRUSH:
208             if (y < 62)
209             {
210                 if (y < 30)
211                 {
212                     if (x < 20)
213                         toolsModel.SetAirBrushWidth(5);
214                     else
215                         toolsModel.SetAirBrushWidth(8);
216                 }
217                 else
218                 {
219                     if (x < 15)
220                         toolsModel.SetAirBrushWidth(3);
221                     else
222                         toolsModel.SetAirBrushWidth(12);
223                 }
224             }
225             break;
226         case TOOL_LINE:
227         case TOOL_BEZIER:
228             if (y <= 62)
229                 toolsModel.SetLineWidth((y - 2) / 12 + 1);
230             break;
231         case TOOL_RECT:
232         case TOOL_SHAPE:
233         case TOOL_ELLIPSE:
234         case TOOL_RRECT:
235             if (y <= 60)
236                 toolsModel.SetShapeStyle((y - 2) / 20);
237             if ((y >= 70) && (y <= 132))
238                 toolsModel.SetLineWidth((y - 72) / 12 + 1);
239             break;
240         case TOOL_FILL:
241         case TOOL_COLOR:
242         case TOOL_ZOOM:
243         case TOOL_PEN:
244             break;
245     }
246     return 0;
247 }
248 
249 LRESULT CToolSettingsWindow::OnToolsModelToolChanged(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
250 {
251     Invalidate();
252     trackbarZoom.ShowWindow((wParam == TOOL_ZOOM) ? SW_SHOW : SW_HIDE);
253     return 0;
254 }
255 
256 LRESULT CToolSettingsWindow::OnToolsModelSettingsChanged(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
257 {
258     Invalidate();
259     return 0;
260 }
261 
262 LRESULT CToolSettingsWindow::OnToolsModelZoomChanged(UINT nMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
263 {
264     int tbPos = 0;
265     int tempZoom = toolsModel.GetZoom();
266 
267     while (tempZoom > MIN_ZOOM)
268     {
269         tbPos++;
270         tempZoom = tempZoom >> 1;
271     }
272     trackbarZoom.SendMessage(TBM_SETPOS, (WPARAM) TRUE, (LPARAM) tbPos);
273     return 0;
274 }
275