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