forked from Gao-Wenkai/CAD-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainFrm.cpp
More file actions
293 lines (254 loc) · 9.58 KB
/
Copy pathMainFrm.cpp
File metadata and controls
293 lines (254 loc) · 9.58 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
// MainFrm.cpp: Main Frame Implementation
// Toolbar (AutoCAD-style) + Command Line + Status Bar
#include "pch.h"
#include "framework.h"
#include "LargeHW.h"
#include "MainFrm.h"
#include "LargeHWView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_CREATE()
ON_WM_SIZE()
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR,
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
CMainFrame::CMainFrame() noexcept
{
}
CMainFrame::~CMainFrame() {}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// 1. Toolbar with drawing commands
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT,
WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER |
CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1;
}
// Customize toolbar: add more buttons via code
// The base toolbar has: New, Open, Save, Cut, Copy, Paste, Print, About
// We'll extend it programmatically
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
// Add CAD menus to the existing menu bar
CMenu* pMenu = GetMenu();
if (pMenu) {
// Insert "Redo" into the Edit menu (position 1, after Undo)
CMenu* pEditMenu = pMenu->GetSubMenu(1);
if (pEditMenu) {
pEditMenu->InsertMenu(1, MF_BYPOSITION | MF_STRING, ID_EDIT_REDO, L"&Redo\tCtrl+Y");
}
// Insert Draw menu before View (position 2)
CMenu drawMenu;
drawMenu.CreatePopupMenu();
drawMenu.AppendMenu(MF_STRING, ID_DRAW_LINE, L"&Line");
drawMenu.AppendMenu(MF_STRING, ID_DRAW_POLYLINE, L"&Polyline");
drawMenu.AppendMenu(MF_STRING, ID_DRAW_CIRCLE, L"&Circle");
drawMenu.AppendMenu(MF_STRING, ID_DRAW_ARC, L"&Arc");
drawMenu.AppendMenu(MF_STRING, ID_DRAW_RECTANGLE, L"&Rectangle");
drawMenu.AppendMenu(MF_STRING, ID_DRAW_POLYGON, L"P&olygon");
drawMenu.AppendMenu(MF_STRING, ID_DRAW_ELLIPSE, L"&Ellipse");
drawMenu.AppendMenu(MF_STRING, ID_DRAW_TEXT, L"&Text");
pMenu->InsertMenu(2, MF_BYPOSITION | MF_POPUP,
(UINT_PTR)drawMenu.Detach(), L"&Draw");
// Insert Modify menu after Draw (position 3)
CMenu modifyMenu;
modifyMenu.CreatePopupMenu();
modifyMenu.AppendMenu(MF_STRING, ID_MODIFY_MOVE, L"&Move");
modifyMenu.AppendMenu(MF_STRING, ID_MODIFY_COPY, L"&Copy");
modifyMenu.AppendMenu(MF_STRING, ID_MODIFY_ROTATE, L"&Rotate");
modifyMenu.AppendMenu(MF_STRING, ID_MODIFY_SCALE, L"&Scale");
modifyMenu.AppendMenu(MF_STRING, ID_MODIFY_DELETE, L"&Erase");
modifyMenu.AppendMenu(MF_STRING, ID_MODIFY_MIRROR, L"M&irror");
modifyMenu.AppendMenu(MF_STRING, ID_MODIFY_OFFSET, L"&Offset");
pMenu->InsertMenu(3, MF_BYPOSITION | MF_POPUP,
(UINT_PTR)modifyMenu.Detach(), L"&Modify");
// Insert Format menu after Modify (position 4)
CMenu formatMenu;
formatMenu.CreatePopupMenu();
formatMenu.AppendMenu(MF_STRING, ID_FORMAT_LAYER, L"&Layer...");
pMenu->InsertMenu(4, MF_BYPOSITION | MF_POPUP,
(UINT_PTR)formatMenu.Detach(), L"F&ormat");
}
// 2. Command line (bottom of window) - AutoCAD-style
CRect rcCmdLine(0, 0, 200, 60);
if (!m_wndCmdLine.Create(WS_CHILD | WS_VISIBLE | WS_BORDER |
ES_LEFT | ES_MULTILINE | ES_AUTOVSCROLL |
ES_WANTRETURN,
rcCmdLine, this, IDC_COMMAND_LINE))
{
TRACE0("Failed to create command line\n");
}
m_fontCmdLine.CreateFont(20, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
FIXED_PITCH | FF_MODERN, L"Consolas");
m_wndCmdLine.SetFont(&m_fontCmdLine);
m_wndCmdLine.SetWindowText(L"Command: ");
m_wndCmdLine.SetSel(lstrlen(L"Command: "), lstrlen(L"Command: "));
m_wndCmdLine.SetMargins(8, 4);
m_cmdLineBrush.CreateSolidBrush(RGB(40, 40, 44));
// 3. Status bar
if (!m_wndStatusBar.Create(this))
{
TRACE0("Failed to create status bar\n");
return -1;
}
m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT));
return 0;
}
void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
CFrameWnd::OnSize(nType, cx, cy);
if (m_wndCmdLine.GetSafeHwnd() && m_wndStatusBar.GetSafeHwnd()) {
CRect rcStatus;
m_wndStatusBar.GetWindowRect(&rcStatus);
ScreenToClient(&rcStatus);
int cmdLineHeight = 60;
int cmdLineTop = rcStatus.top - cmdLineHeight;
// Position command line at bottom, above status bar
m_wndCmdLine.MoveWindow(0, rcStatus.top - cmdLineHeight,
cx, cmdLineHeight);
// Resize view to not overlap command line
CView* pView = GetActiveView();
if (pView->GetSafeHwnd()) {
CRect rcView;
pView->GetWindowRect(&rcView);
ScreenToClient(&rcView);
rcView.bottom = cmdLineTop;
pView->MoveWindow(&rcView);
}
}
}
void CMainFrame::SetStatusBarText(const CString& strText)
{
if (m_wndStatusBar.GetSafeHwnd()) {
m_wndStatusBar.SetPaneText(0, strText);
}
}
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if (!CFrameWnd::PreCreateWindow(cs))
return FALSE;
cs.style &= ~FWS_ADDTOTITLE;
cs.lpszName = L"Engineering CAD v1.0";
return TRUE;
}
HBRUSH CMainFrame::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CFrameWnd::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd->GetDlgCtrlID() == IDC_COMMAND_LINE) {
pDC->SetTextColor(RGB(240, 240, 240));
pDC->SetBkColor(RGB(40, 40, 44));
return m_cmdLineBrush;
}
return hbr;
}
BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN) {
CWnd* pFocus = GetFocus();
if (pFocus && pFocus->GetSafeHwnd() == m_wndCmdLine.GetSafeHwnd()) {
if (pMsg->wParam == VK_RETURN) {
ProcessCommandLine();
return TRUE;
}
if (pMsg->wParam == VK_ESCAPE) {
CLargeHWView* pView = (CLargeHWView*)GetActiveView();
if (pView) pView->OnCancelCommand();
return TRUE;
}
}
}
// Fix cursor when user clicks in command line - ensure it's after prompt
if (pMsg->message == WM_LBUTTONDOWN && pMsg->hwnd == m_wndCmdLine.GetSafeHwnd()) {
PostMessage(WM_APP + 1);
}
if (pMsg->message == WM_APP + 1) {
CLargeHWView* pView = (CLargeHWView*)GetActiveView();
if (pView && pView->GetDocument()) {
int nLen = pView->GetDocument()->m_strCommandPrompt.GetLength();
int nStart, nEnd;
m_wndCmdLine.GetSel(nStart, nEnd);
if (nStart < nLen || nEnd < nLen) {
m_wndCmdLine.SetSel(nLen, max(nEnd, nLen));
}
}
return TRUE;
}
return CFrameWnd::PreTranslateMessage(pMsg);
}
void CMainFrame::ProcessCommandLine()
{
CLargeHWView* pView = (CLargeHWView*)GetActiveView();
if (!pView) return;
CString strRaw;
m_wndCmdLine.GetWindowText(strRaw);
strRaw.Trim();
if (strRaw.IsEmpty()) return;
// Remove the prompt prefix wherever it appears in the string
CLargeHWDoc* pDoc = pView->GetDocument();
CString strCmd;
if (pDoc) {
CString strPrompt = pDoc->m_strCommandPrompt;
int nLen = strPrompt.GetLength();
if (nLen > 0) {
// Take text after the LAST occurrence of the prompt
// (user might have typed before the prompt if cursor was misplaced)
int nPos = strRaw.Find(strPrompt);
if (nPos >= 0) {
// Extract everything after the prompt
strCmd = strRaw.Mid(nPos + nLen);
} else {
// No prompt found - user deleted the prompt, use entire text
strCmd = strRaw;
}
} else {
strCmd = strRaw;
}
} else {
strCmd = strRaw;
}
strCmd.Trim();
if (strCmd.IsEmpty()) {
pView->SendMessage(WM_KEYDOWN, VK_RETURN, 0);
}
// Coordinate input: comma, @, <, or single number while in a point-entry state
else if (strCmd.Find(L',') >= 0 || strCmd[0] == L'@' || strCmd.Find(L'<') >= 0 ||
(pDoc && pDoc->m_drawState != STATE_IDLE && _wtoi(strCmd) != 0 && strCmd != L"0")) {
pView->ProcessCoordinateInput(strCmd);
}
// Command input
else {
pView->ExecuteCommand(strCmd);
}
// Reset to default prompt if still in idle state
if (pDoc && pDoc->m_drawState == STATE_IDLE) {
pDoc->m_strCommandPrompt = L"Command: ";
}
// Reset command line text + cursor at end
CString strPrompt;
if (pDoc) strPrompt = pDoc->m_strCommandPrompt;
if (strPrompt.IsEmpty()) strPrompt = L"Command: ";
m_wndCmdLine.SetWindowText(strPrompt);
m_wndCmdLine.SetSel(strPrompt.GetLength(), strPrompt.GetLength());
}
#ifdef _DEBUG
void CMainFrame::AssertValid() const { CFrameWnd::AssertValid(); }
void CMainFrame::Dump(CDumpContext& dc) const { CFrameWnd::Dump(dc); }
#endif