Repository navigation
Expand file tree
/
Copy pathscribdoc.cpp
More file actions
305 lines (256 loc) · 7.34 KB
/
Copy pathscribdoc.cpp
File metadata and controls
305 lines (256 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// ScribDoc.cpp : implementation of the CScribbleDoc class
//
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.
#include "stdafx.h"
#include "Scribble.h"
#include "ScribDoc.h"
#include "ScribItm.h"
#include "PenDlg.h"
#include "ScribVw.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CScribbleDoc
IMPLEMENT_DYNCREATE(CScribbleDoc, COleServerDoc)
BEGIN_MESSAGE_MAP(CScribbleDoc, COleServerDoc)
//{{AFX_MSG_MAP(CScribbleDoc)
ON_COMMAND(ID_EDIT_CLEAR_ALL, OnEditClearAll)
ON_COMMAND(ID_PEN_THICK_OR_THIN, OnPenThickOrThin)
ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR_ALL, OnUpdateEditClearAll)
ON_UPDATE_COMMAND_UI(ID_PEN_THICK_OR_THIN, OnUpdatePenThickOrThin)
ON_COMMAND(ID_PEN_WIDTHS, OnPenWidths)
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
//}}AFX_MSG_MAP
ON_COMMAND(ID_FILE_SEND_MAIL, OnFileSendMail)
ON_UPDATE_COMMAND_UI(ID_FILE_SEND_MAIL, OnUpdateFileSendMail)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CScribbleDoc construction/destruction
CScribbleDoc::CScribbleDoc()
{
// Use OLE compound files
EnableCompoundFile();
m_sizeDoc = CSize(200, 200);
}
CScribbleDoc::~CScribbleDoc()
{
}
BOOL CScribbleDoc::OnNewDocument()
{
if (!COleServerDoc::OnNewDocument())
return FALSE;
InitDocument();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CScribbleDoc serialization
void CScribbleDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar << m_sizeDoc;
}
else
{
ar >> m_sizeDoc;
}
m_strokeList.Serialize(ar);
}
/////////////////////////////////////////////////////////////////////////////
// CScribbleDoc diagnostics
#ifdef _DEBUG
void CScribbleDoc::AssertValid() const
{
COleServerDoc::AssertValid();
}
void CScribbleDoc::Dump(CDumpContext& dc) const
{
COleServerDoc::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CScribbleDoc commands
BOOL CScribbleDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!COleServerDoc::OnOpenDocument(lpszPathName))
return FALSE;
InitDocument();
return TRUE;
}
void CScribbleDoc::DeleteContents()
{
while (!m_strokeList.IsEmpty())
{
delete m_strokeList.RemoveHead();
}
COleServerDoc::DeleteContents();
}
void CScribbleDoc::InitDocument()
{
m_bThickPen = FALSE;
m_nThinWidth = 2; // default thin pen is 2 pixels wide
m_nThickWidth = 5; // default thick pen is 5 pixels wide
ReplacePen(); // initialize pen according to current width
// default document size is 2 x 2 inches
m_sizeDoc = CSize(200,200);
}
CStroke* CScribbleDoc::NewStroke()
{
CStroke* pStrokeItem = new CStroke(m_nPenWidth);
m_strokeList.AddTail(pStrokeItem);
SetModifiedFlag(); // Mark the document as having been modified, for
// purposes of confirming File Close.
return pStrokeItem;
}
/////////////////////////////////////////////////////////////////////////////
// CStroke
IMPLEMENT_SERIAL(CStroke, CObject, 2)
CStroke::CStroke()
{
// This empty constructor should be used by serialization only
}
CStroke::CStroke(UINT nPenWidth)
{
m_nPenWidth = nPenWidth;
m_rectBounding.SetRectEmpty();
}
void CStroke::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar << m_rectBounding;
ar << (WORD)m_nPenWidth;
m_pointArray.Serialize(ar);
}
else
{
ar >> m_rectBounding;
WORD w;
ar >> w;
m_nPenWidth = w;
m_pointArray.Serialize(ar);
}
}
BOOL CStroke::DrawStroke(CDC* pDC)
{
CPen penStroke;
if (!penStroke.CreatePen(PS_SOLID, m_nPenWidth, RGB(0,0,0)))
return FALSE;
CPen* pOldPen = pDC->SelectObject(&penStroke);
pDC->MoveTo(m_pointArray[0]);
for (int i=1; i < m_pointArray.GetSize(); i++)
{
pDC->LineTo(m_pointArray[i]);
}
pDC->SelectObject(pOldPen);
return TRUE;
}
void CScribbleDoc::OnEditClearAll()
{
DeleteContents();
SetModifiedFlag(); // Mark the document as having been modified, for
// purposes of confirming File Close.
UpdateAllViews(NULL);
}
void CScribbleDoc::OnPenThickOrThin()
{
// Toggle the state of the pen between thin or thick.
m_bThickPen = !m_bThickPen;
// Change the current pen to reflect the new user-specified width.
ReplacePen();
}
void CScribbleDoc::ReplacePen()
{
m_nPenWidth = m_bThickPen? m_nThickWidth : m_nThinWidth;
// Change the current pen to reflect the new user-specified width.
m_penCur.DeleteObject();
m_penCur.CreatePen(PS_SOLID, m_nPenWidth, RGB(0,0,0)); // solid black
}
void CScribbleDoc::OnUpdateEditClearAll(CCmdUI* pCmdUI)
{
// Enable the command user interface object (menu item or tool bar
// button) if the document is non-empty, i.e., has at least one stroke.
pCmdUI->Enable(!m_strokeList.IsEmpty());
}
void CScribbleDoc::OnUpdatePenThickOrThin(CCmdUI* pCmdUI)
{
// Add check mark to Draw Thick Line menu item, if the current
// pen width is "thick".
pCmdUI->SetCheck(m_bThickPen);
}
void CScribbleDoc::OnPenWidths()
{
CPenWidthsDlg dlg;
// Initialize dialog data
dlg.m_nThinWidth = m_nThinWidth;
dlg.m_nThickWidth = m_nThickWidth;
// Invoke the dialog box
if (dlg.DoModal() == IDOK)
{
// retrieve the dialog data
m_nThinWidth = dlg.m_nThinWidth;
m_nThickWidth = dlg.m_nThickWidth;
// Update the pen that is used by views when drawing new strokes,
// to reflect the new pen width definitions for "thick" and "thin".
ReplacePen();
}
}
void CStroke::FinishStroke()
{
// Calculate the bounding rectangle. It's needed for smart
// repainting.
if (m_pointArray.GetSize()==0)
{
m_rectBounding.SetRectEmpty();
return;
}
CPoint pt = m_pointArray[0];
m_rectBounding = CRect(pt.x, pt.y, pt.x, pt.y);
for (int i=1; i < m_pointArray.GetSize(); i++)
{
// If the point lies outside of the accumulated bounding
// rectangle, then inflate the bounding rect to include it.
pt = m_pointArray[i];
m_rectBounding.left = min(m_rectBounding.left, pt.x);
m_rectBounding.right = max(m_rectBounding.right, pt.x);
m_rectBounding.top = max(m_rectBounding.top, pt.y);
m_rectBounding.bottom = min(m_rectBounding.bottom, pt.y);
}
// Add the pen width to the bounding rectangle. This is necessary
// to account for the width of the stroke when invalidating
// the screen.
m_rectBounding.InflateRect(CSize(m_nPenWidth, -(int)m_nPenWidth));
return;
}
COleServerItem* CScribbleDoc::OnGetEmbeddedItem()
{
// OnGetEmbeddedItem is called by the framework to get the COleServerItem
// that is associated with the document. It is only called when necessary.
CScribbleItem* pItem = new CScribbleItem(this);
ASSERT_VALID(pItem);
return pItem;
}
void CScribbleDoc::OnSetItemRects(LPCRECT lpPosRect, LPCRECT lpClipRect)
{
// call base class to change the size of the window
COleServerDoc::OnSetItemRects(lpPosRect, lpClipRect);
// notify first view that scroll info should change
POSITION pos = GetFirstViewPosition();
CScribbleView* pView = (CScribbleView*)GetNextView(pos);
pView->ResyncScrollSizes();
}
void CScribbleDoc::OnEditCopy()
{
CScribbleItem* pItem = GetEmbeddedItem();
pItem->CopyToClipboard(TRUE);
}