1 /*
2  * PROJECT:    PAINT for ReactOS
3  * LICENSE:    LGPL-2.0-or-later (https://spdx.org/licenses/LGPL-2.0-or-later)
4  * PURPOSE:    Keep track of tool parameters, notify listeners
5  * COPYRIGHT:  Copyright 2015 Benedikt Freisen <b.freisen@gmx.net>
6  */
7 
8 #include "precomp.h"
9 
10 ToolsModel toolsModel;
11 
12 /* FUNCTIONS ********************************************************/
13 
14 ToolsModel::ToolsModel()
15 {
16     m_lineWidth = 1;
17     m_shapeStyle = 0;
18     m_brushStyle = 0;
19     m_oldActiveTool = m_activeTool = TOOL_PEN;
20     m_airBrushWidth = 5;
21     m_rubberRadius = 4;
22     m_transpBg = FALSE;
23     m_zoom = 1000;
24     ZeroMemory(&m_tools, sizeof(m_tools));
25     m_pToolObject = GetOrCreateTool(m_activeTool);
26 }
27 
28 ToolsModel::~ToolsModel()
29 {
30     for (size_t i = 0; i < _countof(m_tools); ++i)
31         delete m_tools[i];
32 }
33 
34 ToolBase *ToolsModel::GetOrCreateTool(TOOLTYPE nTool)
35 {
36     if (!m_tools[nTool])
37         m_tools[nTool] = ToolBase::createToolObject(nTool);
38 
39     return m_tools[nTool];
40 }
41 
42 BOOL ToolsModel::IsSelection() const
43 {
44     return (GetActiveTool() == TOOL_RECTSEL || GetActiveTool() == TOOL_FREESEL);
45 }
46 
47 int ToolsModel::GetLineWidth() const
48 {
49     return m_lineWidth;
50 }
51 
52 void ToolsModel::SetLineWidth(int nLineWidth)
53 {
54     m_lineWidth = nLineWidth;
55     NotifyToolSettingsChanged();
56 }
57 
58 int ToolsModel::GetShapeStyle() const
59 {
60     return m_shapeStyle;
61 }
62 
63 void ToolsModel::SetShapeStyle(int nShapeStyle)
64 {
65     m_shapeStyle = nShapeStyle;
66     NotifyToolSettingsChanged();
67 }
68 
69 int ToolsModel::GetBrushStyle() const
70 {
71     return m_brushStyle;
72 }
73 
74 void ToolsModel::SetBrushStyle(int nBrushStyle)
75 {
76     m_brushStyle = nBrushStyle;
77     NotifyToolSettingsChanged();
78 }
79 
80 TOOLTYPE ToolsModel::GetActiveTool() const
81 {
82     return m_activeTool;
83 }
84 
85 TOOLTYPE ToolsModel::GetOldActiveTool() const
86 {
87     return m_oldActiveTool;
88 }
89 
90 void ToolsModel::SetActiveTool(TOOLTYPE nActiveTool)
91 {
92     OnFinishDraw();
93 
94     switch (m_activeTool)
95     {
96         case TOOL_FREESEL:
97         case TOOL_RECTSEL:
98         case TOOL_RUBBER:
99         case TOOL_COLOR:
100         case TOOL_ZOOM:
101         case TOOL_TEXT:
102             break;
103 
104         default:
105             m_oldActiveTool = m_activeTool;
106             break;
107     }
108 
109     m_activeTool = nActiveTool;
110     m_pToolObject = GetOrCreateTool(m_activeTool);
111     NotifyToolChanged();
112 }
113 
114 int ToolsModel::GetAirBrushWidth() const
115 {
116     return m_airBrushWidth;
117 }
118 
119 void ToolsModel::SetAirBrushWidth(int nAirBrushWidth)
120 {
121     m_airBrushWidth = nAirBrushWidth;
122     NotifyToolSettingsChanged();
123 }
124 
125 int ToolsModel::GetRubberRadius() const
126 {
127     return m_rubberRadius;
128 }
129 
130 void ToolsModel::SetRubberRadius(int nRubberRadius)
131 {
132     m_rubberRadius = nRubberRadius;
133     NotifyToolSettingsChanged();
134 }
135 
136 BOOL ToolsModel::IsBackgroundTransparent() const
137 {
138     return m_transpBg;
139 }
140 
141 void ToolsModel::SetBackgroundTransparent(BOOL bTransparent)
142 {
143     m_transpBg = bTransparent;
144     NotifyToolSettingsChanged();
145     imageModel.NotifyImageChanged();
146 }
147 
148 int ToolsModel::GetZoom() const
149 {
150     return m_zoom;
151 }
152 
153 void ToolsModel::SetZoom(int nZoom)
154 {
155     m_zoom = nZoom;
156     NotifyZoomChanged();
157 }
158 
159 void ToolsModel::NotifyToolChanged()
160 {
161     if (toolBoxContainer.IsWindow())
162         toolBoxContainer.SendMessage(WM_TOOLSMODELTOOLCHANGED, m_activeTool);
163     if (toolSettingsWindow.IsWindow())
164         toolSettingsWindow.SendMessage(WM_TOOLSMODELTOOLCHANGED, m_activeTool);
165     if (fontsDialog.IsWindow())
166         fontsDialog.SendMessage(WM_TOOLSMODELTOOLCHANGED, m_activeTool);
167     if (textEditWindow.IsWindow())
168         textEditWindow.SendMessage(WM_TOOLSMODELTOOLCHANGED, m_activeTool);
169 }
170 
171 void ToolsModel::NotifyToolSettingsChanged()
172 {
173     if (toolSettingsWindow.IsWindow())
174         toolSettingsWindow.SendMessage(WM_TOOLSMODELSETTINGSCHANGED);
175     if (textEditWindow.IsWindow())
176         textEditWindow.SendMessage(WM_TOOLSMODELSETTINGSCHANGED);
177 }
178 
179 void ToolsModel::NotifyZoomChanged()
180 {
181     if (toolSettingsWindow.IsWindow())
182         toolSettingsWindow.SendMessage(WM_TOOLSMODELZOOMCHANGED);
183     if (textEditWindow.IsWindow())
184         textEditWindow.SendMessage(WM_TOOLSMODELZOOMCHANGED);
185     if (canvasWindow.IsWindow())
186         canvasWindow.SendMessage(WM_TOOLSMODELZOOMCHANGED);
187 }
188 
189 void ToolsModel::OnButtonDown(BOOL bLeftButton, LONG x, LONG y, BOOL bDoubleClick)
190 {
191     m_pToolObject->beginEvent();
192     updateStartAndLast(x, y);
193     m_pToolObject->OnButtonDown(bLeftButton, x, y, bDoubleClick);
194     m_pToolObject->endEvent();
195 }
196 
197 void ToolsModel::OnMouseMove(BOOL bLeftButton, LONG x, LONG y)
198 {
199     m_pToolObject->beginEvent();
200     m_pToolObject->OnMouseMove(bLeftButton, x, y);
201     updateLast(x, y);
202     m_pToolObject->endEvent();
203 }
204 
205 void ToolsModel::OnButtonUp(BOOL bLeftButton, LONG x, LONG y)
206 {
207     m_pToolObject->beginEvent();
208     m_pToolObject->OnButtonUp(bLeftButton, x, y);
209     updateLast(x, y);
210     m_pToolObject->endEvent();
211 }
212 
213 void ToolsModel::OnCancelDraw()
214 {
215     ATLTRACE("ToolsModel::OnCancelDraw()\n");
216     m_pToolObject->beginEvent();
217     m_pToolObject->OnCancelDraw();
218     m_pToolObject->endEvent();
219 }
220 
221 void ToolsModel::OnFinishDraw()
222 {
223     ATLTRACE("ToolsModel::OnFinishDraw()\n");
224     m_pToolObject->beginEvent();
225     m_pToolObject->OnFinishDraw();
226     m_pToolObject->endEvent();
227 }
228 
229 void ToolsModel::OnDrawOverlayOnImage(HDC hdc)
230 {
231     m_pToolObject->OnDrawOverlayOnImage(hdc);
232 }
233 
234 void ToolsModel::OnDrawOverlayOnCanvas(HDC hdc)
235 {
236     m_pToolObject->OnDrawOverlayOnCanvas(hdc);
237 }
238 
239 void ToolsModel::resetTool()
240 {
241     m_pToolObject->reset();
242 }
243 
244 void ToolsModel::selectAll()
245 {
246     SetActiveTool(TOOL_RECTSEL);
247     OnButtonDown(TRUE, 0, 0, FALSE);
248     OnMouseMove(TRUE, imageModel.GetWidth(), imageModel.GetHeight());
249     OnButtonUp(TRUE, imageModel.GetWidth(), imageModel.GetHeight());
250 }
251