-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCutterPreviewWindow.cpp
More file actions
783 lines (637 loc) · 21.1 KB
/
CutterPreviewWindow.cpp
File metadata and controls
783 lines (637 loc) · 21.1 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
// CutterPreviewWindow.cpp : implementation file
//
#include "stdafx.h"
#include <assert.h>
#include <string>
#include <sstream>
#include "Aerofoil.h"
#include "CutterPreviewWindow.h"
#include "Kernel/CutterGeometry.h"
#include "Kernel/OutputDevice.h"
#include "Kernel/CutterSimulation.h"
#include "Kernel/CutterSimulationOutputDevice.h"
#include "Kernel/ObjectSerializer.h"
// CutterPreviewWindow
// Provides a window of the display of a cut. Knows how to plot a single point on all 4 axes (via inheriting PointPlotter).
// To allow immediate update and still redraw properly the window draws directly to its DC but also maintains
// a bitmap. Note also that as a side-effect plot processes windows message so that the display doesn't freeze.
// Note for scrolling see: https://docs.microsoft.com/en-us/windows/desktop/controls/scroll-a-bitmap-in-scroll-bars
IMPLEMENT_DYNAMIC(CutterPreviewWindow, CFrameWnd)
CutterPreviewWindow::CutterPreviewWindow(CWnd* parentWindow, const Cut* cut, const CutterGeometry* geometry, const CutStructure::Context& context)
: bitmap(0)
, nWidth(0)
, nHeight(0)
, xMinScroll(0)
, xCurrentScroll(0)
, xMaxScroll(0)
, yMinScroll(0)
, yCurrentScroll(0)
, yMaxScroll(0)
, terminate(false)
, simulation(0)
, device(0)
, isCutting(false)
, isPaused(false)
, hasFlash(false)
, context(context)
{
// Set up timing info for cut to control speed.
plotTime.QuadPart = 0;
QueryPerformanceFrequency(&counterFrequency);
stepTime.QuadPart = counterFrequency.QuadPart / DEFAULT_STEPS_PER_SEC;
windowClass = AfxRegisterWndClass( CS_VREDRAW | CS_HREDRAW ,
::LoadCursor(NULL, IDC_ARROW),
0, // always draw bitmap so no need for (HBRUSH) ::GetStockObject(WHITE_BRUSH),
::LoadIcon(NULL, IDI_APPLICATION)
);
// Choose size from parent Window.
RECT r;
parentWindow->GetClientRect(&r);
Create(windowClass, "Cutter Preview Window",
WS_BORDER | WS_CAPTION | WS_VISIBLE | WS_THICKFRAME | WS_SYSMENU | WS_HSCROLL | WS_VSCROLL,
CRect(r.left, r.top, r.right, r.bottom),
parentWindow,
MAKEINTRESOURCEA(IDR_CONTROLCUTTER)
);
// Set up simulation and geometry
simulation = new CutterSimulation(this);
simulation->setGeometry(*geometry);
device = new CutterSimulationOutputDevice(simulation);
// Now geometry is set up can decide the size of the bitmap / drawing area.
nWidth = (int)round((xTravel + 2 * border) * xStepsPerMM) + 2; // +2 to keep a 1 pixel border
nHeight = (int)round((yTravel + 2 * border) * yStepsPerMM) + 2;
CDC* pdc = GetDC();
createBitmap(pdc);
ReleaseDC(pdc);
// Wake up the scroll bars
yCurrentScroll = nHeight; // scroll to bottom as we do.
OnSize(SIZE_RESTORED, r.right, r.bottom);
// Initialise the local cut object by searializing/deserializing. Detaches this from
// any sneaky edits that might happen in the background.
std::stringstream stream;
CObjectSerializer serializer(&stream);
cut->serializeTo(serializer);
this->cut.serializeFrom(serializer);
// Simulate start command to fire off preview process.
PostMessage(WM_COMMAND, ID_CONTROLCUTTER_START, 0);
}
CutterPreviewWindow::~CutterPreviewWindow()
{
delete bitmap;
delete device;
delete simulation;
}
void CutterPreviewWindow::runCut() {
if (bitmapDirty) {
delete bitmap;
CDC* pdc = GetDC();
createBitmap(pdc);
ReleaseDC(pdc);
// Force repaint of window now bitmap reset
Invalidate();
toolbar.Invalidate();
UpdateWindow();
}
terminate = false;
device->reset();
simulation->reset();
try {
cut.cut(*device, context);
}
catch (COutputDevice::StoppedException & ) {
// Nop
}
catch (COutputDevice::LimitsException & limits) {
std::string msg("Output device out of limits: ");
msg.append(limits.what());
AfxMessageBox(msg.c_str(), MB_OK | MB_ICONWARNING);
}
}
void CutterPreviewWindow::createBitmap(CDC* pdc)
{
bitmap = new CBitmap();
bitmap->CreateCompatibleBitmap(
pdc, nWidth, nHeight);
// Colours / shades of grey to use
const DWORD background = RGB(64, 64, 64);
const DWORD grid = RGB(80, 80, 80); // arbitrary but slightly brighter than background
// Fill the bitmap with grey background
int nPixels = nWidth * nHeight;
DWORD* bits = new DWORD[nPixels];
DWORD* here = bits;
for (int i = 0; i < nPixels; ++i) {
*here++ = background;
}
// Draw a grid on the background.
const double xGridSpacing = 10.0;
const double yGridSpacing = 10.0;
// Vertical lines
int yStart = (int)round(yStepsPerMM * border);
int yStop = (int)round(yStepsPerMM * (border + yTravel));
assert(yStart < nHeight);
assert(yStop < nHeight);
for (double x = border; x <= xTravel + border; x += xGridSpacing) {
int ix = 1 + (int)round(x * xStepsPerMM);
assert(ix < nWidth);
for (int iy = yStart; iy <= yStop; ++iy) {
here = bits + ix + (intptr_t)iy * (intptr_t)nWidth;
*here = grid;
}
}
// Horizontal lines
int xStart = (int)round(xStepsPerMM * border);
int xStop = (int)round(xStepsPerMM * (border + xTravel));
assert(xStart < nWidth);
assert(xStop < nWidth);
for (double y = border; y <= yTravel + border; y += yGridSpacing) {
int iy = 1 + (int)round(y * yStepsPerMM);
assert(iy < nHeight);
here = bits + (intptr_t)nWidth * (intptr_t)iy;
for (int ix = xStart; ix < xStop; ++ix) {
here = bits + ix + (intptr_t)iy * (intptr_t)nWidth;
*here = grid;
}
}
bitmap->SetBitmapBits(sizeof(DWORD) * nPixels, bits);
delete[] bits;
bitmapDirty = false;
}
void CutterPreviewWindow::drawFlash(CDC* pdc)
{
int oldMode = pdc->GetROP2();
pdc->SetROP2(R2_XORPEN);
CPen penl(PS_DOT, 1, leftFlashColour);
CPen penr(PS_DOT, 1, rightFlashColour);
CPen* penOld = pdc->SelectObject(&penl);
pdc->MoveTo(previousFlash.x - flashSize, previousFlash.y - flashSize);
pdc->LineTo(previousFlash.x + flashSize, previousFlash.y + flashSize);
pdc->MoveTo(previousFlash.x - flashSize, previousFlash.y + flashSize);
pdc->LineTo(previousFlash.x + flashSize, previousFlash.y - flashSize);
pdc->LineTo(previousFlash.x + flashSize, previousFlash.y + flashSize);
pdc->LineTo(previousFlash.x - flashSize, previousFlash.y + flashSize);
pdc->LineTo(previousFlash.x - flashSize, previousFlash.y - flashSize);
pdc->LineTo(previousFlash.x + flashSize, previousFlash.y - flashSize);
pdc->SelectObject(&penr);
pdc->MoveTo(previousFlash.u - flashSize, previousFlash.v - flashSize);
pdc->LineTo(previousFlash.u + flashSize, previousFlash.v + flashSize);
pdc->MoveTo(previousFlash.u - flashSize, previousFlash.v + flashSize);
pdc->LineTo(previousFlash.u + flashSize, previousFlash.v - flashSize);
pdc->LineTo(previousFlash.u + flashSize, previousFlash.v + flashSize);
pdc->LineTo(previousFlash.u - flashSize, previousFlash.v + flashSize);
pdc->LineTo(previousFlash.u - flashSize, previousFlash.v - flashSize);
pdc->LineTo(previousFlash.u + flashSize, previousFlash.v - flashSize);
pdc->SelectObject(penOld);
pdc->SetROP2(oldMode);
}
void CutterPreviewWindow::unflash(CDC* pdc) {
if (hasFlash) {
drawFlash(pdc);
hasFlash = false;
}
}
// Provides a bit of a flicker at the drawing point so that it's possible to see where the wire is even if
// going over old cuts.
void CutterPreviewWindow::flash(CDC* pdc, const Position<long long>& axes, DWORD leftColour, DWORD rightColour) {
assert(!hasFlash);
// Calculate new point
previousFlash.x = (int)(axes.x - xCurrentScroll + 1);
previousFlash.y = (int)(axes.y - yCurrentScroll + 1);
previousFlash.u = (int)(axes.u - xCurrentScroll + 1);
previousFlash.v = (int)(axes.v - yCurrentScroll + 1);
// Set new colours
leftFlashColour = leftColour;
rightFlashColour = rightColour;
drawFlash(pdc);
hasFlash = true;
}
bool CutterPreviewWindow::plot(Position<long long> axes)
{
// If being asked to plot when the window is being destroyed then treat the same
// as any hardware being stopped: all bets are off so abort the cut.
if (terminate) {
throw COutputDevice::StoppedException();
}
CDC* pdc = GetDC();
// Create a bitmap if not already done.
if (bitmap == 0) {
createBitmap(pdc);
}
bool axesSuperimposed = axes.x == axes.u && axes.y == axes.v;
// Set up colours and clip axes. Show error colour if cutter might
// go out of bounds (e.g. to cut a taper where LE might be at x=0 but
// to get there the actual hardware has to go negative.
DWORD leftColour = RGB(0, 255, 0);
DWORD rightColour = RGB(80, 80, 255); // slight lighter blue much easier to see
DWORD combinedColour = RGB(0, 255, 255);
DWORD errorLeftColour = RGB(255, 128, 0);
DWORD errorRightColour = RGB(255, 0, 128);
DWORD errorCombinedColour = RGB(255, 128, 128);
// Set colour if we're out of limits. Note that due to border we can draw out
// of limits a bit.
int maxx = (int)round(xTravel * xStepsPerMM);
int maxy = (int)round(yTravel * yStepsPerMM);
if (axes.x < 0) {
leftColour = errorLeftColour; combinedColour = errorCombinedColour;
}
if (axes.y < 0) {
leftColour = errorLeftColour; combinedColour = errorCombinedColour;
}
if (axes.u < 0) {
rightColour = errorRightColour; combinedColour = errorCombinedColour;
}
if (axes.v < 0) {
rightColour = errorRightColour; combinedColour = errorCombinedColour;
}
if (axes.x > maxx) {
leftColour = errorLeftColour; combinedColour = errorCombinedColour;
}
if (axes.y > maxy) {
leftColour = errorLeftColour; combinedColour = errorCombinedColour;
}
if (axes.u > maxx) {
rightColour = errorRightColour; combinedColour = errorCombinedColour;
}
if (axes.v > maxy){
rightColour = errorRightColour; combinedColour = errorCombinedColour;
}
// Shift drawing to allow for border
int xOffset = (int)(border * xStepsPerMM);
int yOffset = (int)(border * yStepsPerMM);
axes.x += xOffset;
axes.y += yOffset;
axes.u += xOffset;
axes.v += yOffset;
// Make sure zero at the bottom:
axes.y = nHeight - axes.y;
axes.v = nHeight - axes.v;
// Now constrain to drawing area
if (axes.x < 0) axes.x = 0;
if (axes.y < 0) axes.y = 0;
if (axes.u < 0) axes.u = 0;
if (axes.v < 0) axes.v = 0;
if (axes.x > nWidth) axes.x = nWidth;
if (axes.y > nHeight) axes.y = nHeight;
if (axes.u > nWidth) axes.u = nWidth;
if (axes.v > nHeight) axes.v = nHeight;
unflash(pdc);
// Write to the screen
// Note +1 to allow 1 pixel border to mark attempts to draw/move out of area.
if (axesSuperimposed) {
pdc->SetPixel((int)(axes.x - xCurrentScroll + 1), (int)(axes.y - yCurrentScroll + 1), combinedColour);
}
else {
pdc->SetPixel((int)(axes.x - xCurrentScroll + 1), (int)(axes.y - yCurrentScroll + 1), leftColour);
pdc->SetPixel((int)(axes.u - xCurrentScroll + 1), (int)(axes.v -yCurrentScroll + 1), rightColour);
}
flash(pdc, axes, leftColour, rightColour);
// Write into the bitmap
CDC memDC;
if (memDC.CreateCompatibleDC(pdc) != 0) { //create a memory DC compatible with pDC
CBitmap* pOldBm = memDC.SelectObject(bitmap); //Select the bitmap into the DC
if (axesSuperimposed) {
memDC.SetPixel((int)(axes.x + 1), (int)(axes.y + 1), combinedColour);
} else {
memDC.SetPixel((int)(axes.x + 1), (int)(axes.y + 1), leftColour);
memDC.SetPixel((int)(axes.u + 1), (int)(axes.v + 1), rightColour);
}
memDC.SelectObject(pOldBm);
bitmapDirty = true;
}
ReleaseDC(pdc);
processMessages();
return terminate;
}
void CutterPreviewWindow::setResolution(double xStepsPerMM, double yStepsPerMM)
{
this->xStepsPerMM = xStepsPerMM;
this->yStepsPerMM = yStepsPerMM;
}
void CutterPreviewWindow::setCutterBounds(double xTravel, double yTravel)
{
this->xTravel = xTravel;
this->yTravel = yTravel;
}
void CutterPreviewWindow::processMessages() {
if (plotTime.QuadPart == 0) {
QueryPerformanceCounter(&plotTime);
}
LARGE_INTEGER now;
do {
MSG msg;
while (::PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
if (!AfxGetApp()->PumpMessage()) {
::PostQuitMessage(0);
terminate = true;
break;
}
}
QueryPerformanceCounter(&now);
if (terminate) {
break;
}
// If we're paused then include idle time processing to allow any MFC operations to complete.
// also update timer
if (isPaused) {
LONG lIdle = 0;
while (AfxGetApp()->OnIdle(lIdle++))
;
QueryPerformanceCounter(&now);
}
} while (isPaused || (now.QuadPart - plotTime.QuadPart) < stepTime.QuadPart);
plotTime = now;
// Possible that the message pump has allowed the user to close the window.
// If so, terminate will be true so signal that processing is stopped.
if (terminate) {
throw COutputDevice::StoppedException(); // unravel back up through message loop and terminate plot
}
// let MFC do its idle processing
LONG lIdle = 0;
while (AfxGetApp()->OnIdle(lIdle++))
;
}
void CutterPreviewWindow::PostNcDestroy()
{
delete this; // goodbye cruel world...
}
BEGIN_MESSAGE_MAP(CutterPreviewWindow, CWnd)
ON_WM_PAINT()
ON_WM_SIZE()
ON_WM_HSCROLL()
ON_WM_VSCROLL()
ON_WM_DESTROY()
ON_WM_CREATE()
ON_COMMAND(ID_CONTROLCUTTER_START, &CutterPreviewWindow::OnControlcutterStart)
ON_COMMAND(ID_CONTROLCUTTER_PAUSE, &CutterPreviewWindow::OnControlcutterPause)
ON_COMMAND(ID_CONTROLCUTTER_STOP, &CutterPreviewWindow::OnControlcutterStop)
ON_UPDATE_COMMAND_UI(ID_CONTROLCUTTER_PAUSE, &CutterPreviewWindow::OnUpdateControlcutterPause)
ON_UPDATE_COMMAND_UI(ID_CONTROLCUTTER_START, &CutterPreviewWindow::OnUpdateControlcutterStart)
ON_UPDATE_COMMAND_UI(ID_CONTROLCUTTER_STOP, &CutterPreviewWindow::OnUpdateControlcutterStop)
ON_COMMAND(ID_SPEED_X1, &CutterPreviewWindow::OnSpeedX1)
ON_COMMAND(ID_SPEED_X10, &CutterPreviewWindow::OnSpeedX10)
ON_COMMAND(ID_SPEED_X2, &CutterPreviewWindow::OnSpeedX2)
ON_COMMAND(ID_SPEED_X5, &CutterPreviewWindow::OnSpeedX5)
ON_COMMAND(ID_SPEED_FULL, &CutterPreviewWindow::OnSpeedFull)
END_MESSAGE_MAP()
// CutterPreviewWindow message handlers
void CutterPreviewWindow::OnPaint()
{
if (bitmap) {
if (hasFlash) {
CDC* pdc = GetDC();
unflash(pdc);
ReleaseDC(pdc);
}
// Paint DC to copy to
CPaintDC dc(this); // device context for painting
BITMAP bmpStruct; //Bitmap information structure
bitmap->GetObject(sizeof(bmpStruct), &bmpStruct); //fill the structure with useful data
// Memory DC to copy from
CDC memDC;
if (memDC.CreateCompatibleDC(&dc) != 0) { //create a memory DC compatible with pDC
CBitmap* pOldBm = memDC.SelectObject(bitmap); //Select the bitmap into the DC
// And BitBlit pixels fom bitmap via memory DC to paint DC
int status = dc.BitBlt(0, 0, bmpStruct.bmWidth, bmpStruct.bmHeight, &memDC, xCurrentScroll, yCurrentScroll, SRCCOPY); // This is the heart of bitmap drawing
printf("status %d", status);
memDC.SelectObject(pOldBm);
}
}
}
void CutterPreviewWindow::OnSize(UINT nType, int cx, int cy)
{
__super::OnSize(nType, cx, cy);
int xNewSize = cx;
int yNewSize = cy;
// if (fBlt)
// fSize = TRUE;
SCROLLINFO si;
// The horizontal scrolling range is defined by
// (bitmap_width) - (client_width). The current horizontal
// scroll value remains within the horizontal scrolling range.
xMaxScroll = max(nWidth - xNewSize, 0);
xCurrentScroll = min(xCurrentScroll, xMaxScroll);
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
si.nMin = xMinScroll;
si.nMax = nWidth;
si.nPage = xNewSize;
si.nPos = xCurrentScroll;
SetScrollInfo( SB_HORZ, &si, TRUE);
// The vertical scrolling range is defined by
// (bitmap_height) - (client_height). The current vertical
// scroll value remains within the vertical scrolling range.
yMaxScroll = max(nHeight - yNewSize, 0);
yCurrentScroll = min(yCurrentScroll, yMaxScroll);
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
si.nMin = yMinScroll;
si.nMax = nHeight;
si.nPage = yNewSize;
si.nPos = yCurrentScroll;
SetScrollInfo( SB_VERT, &si, TRUE);
}
void CutterPreviewWindow::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
int xDelta; // xDelta = new_pos - current_pos
int xNewPos; // new position
int yDelta = 0;
switch (nSBCode)
{
// User clicked the scroll bar shaft left of the scroll box.
case SB_PAGEUP:
xNewPos = xCurrentScroll - 50;
break;
// User clicked the scroll bar shaft right of the scroll box.
case SB_PAGEDOWN:
xNewPos = xCurrentScroll + 50;
break;
// User clicked the left arrow.
case SB_LINEUP:
xNewPos = xCurrentScroll - 5;
break;
// User clicked the right arrow.
case SB_LINEDOWN:
xNewPos = xCurrentScroll + 5;
break;
// User dragged the scroll box.
case SB_THUMBPOSITION:
xNewPos = nPos;
break;
default:
xNewPos = xCurrentScroll;
}
// New position must be between 0 and the screen width.
xNewPos = max(0, xNewPos);
xNewPos = min(xMaxScroll, xNewPos);
// If the current position does not change, do not scroll.
if (xNewPos != xCurrentScroll) {
if (hasFlash) {
CDC* pdc = GetDC();
unflash(pdc);
ReleaseDC(pdc);
}
// Determine the amount scrolled (in pixels).
xDelta = xNewPos - xCurrentScroll;
// Reset the current scroll position.
xCurrentScroll = xNewPos;
// Scroll the window. (The system repaints most of the
// client area when ScrollWindowEx is called; however, it is
// necessary to call UpdateWindow in order to repaint the
// rectangle of pixels that were invalidated.)
ScrollWindowEx(-xDelta, -yDelta, 0, 0, 0, 0, SW_INVALIDATE);
UpdateWindow();
// Reset the scroll bar.
SCROLLINFO si;
si.cbSize = sizeof(si);
si.fMask = SIF_POS;
si.nPos = xCurrentScroll;
SetScrollInfo(SB_HORZ, &si, TRUE);
}
}
void CutterPreviewWindow::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
int xDelta = 0;
int yDelta; // yDelta = new_pos - current_pos
int yNewPos; // new position
switch (nSBCode)
{
// User clicked the scroll bar shaft above the scroll box.
case SB_PAGEUP:
yNewPos = yCurrentScroll - 50;
break;
// User clicked the scroll bar shaft below the scroll box.
case SB_PAGEDOWN:
yNewPos = yCurrentScroll + 50;
break;
// User clicked the top arrow.
case SB_LINEUP:
yNewPos = yCurrentScroll - 5;
break;
// User clicked the bottom arrow.
case SB_LINEDOWN:
yNewPos = yCurrentScroll + 5;
break;
// User dragged the scroll box.
case SB_THUMBPOSITION:
yNewPos = nPos;
break;
default:
yNewPos = yCurrentScroll;
}
// New position must be between 0 and the screen height.
yNewPos = max(0, yNewPos);
yNewPos = min(yMaxScroll, yNewPos);
// If the current position does not change, do not scroll.
if (yNewPos != yCurrentScroll) {
if (hasFlash) {
CDC* pdc = GetDC();
unflash(pdc);
ReleaseDC(pdc);
}
// Determine the amount scrolled (in pixels).
yDelta = yNewPos - yCurrentScroll;
// Reset the current scroll position.
yCurrentScroll = yNewPos;
// Scroll the window. (The system repaints most of the
// client area when ScrollWindowEx is called; however, it is
// necessary to call UpdateWindow in order to repaint the
// rectangle of pixels that were invalidated.)
ScrollWindowEx( -xDelta, -yDelta, 0, 0, 0, 0, SW_INVALIDATE);
Invalidate(); // get whole window to redraw as otherwise it scrolls toolbar image down.
UpdateWindow();
// Reset the scroll bar.
SCROLLINFO si;
si.cbSize = sizeof(si);
si.fMask = SIF_POS;
si.nPos = yCurrentScroll;
SetScrollInfo( SB_VERT, &si, TRUE);
}
}
// Need to pick up the window being destroyed so that the message pump
// can terminate any plot.
void CutterPreviewWindow::OnDestroy()
{
__super::OnDestroy();
terminate = true;
}
int CutterPreviewWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (__super::OnCreate(lpCreateStruct) == -1)
return -1;
if (!toolbar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!toolbar.LoadToolBar(IDR_CONTROLCUTTER))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
// Command IDs for the toolbar: ORDERING MUST MATCH BITMAP - see IDR_CONTROLCUTTER toolbar
UINT commands[] = {
ID_CONTROLCUTTER_START,
ID_CONTROLCUTTER_PAUSE,
ID_CONTROLCUTTER_STOP,
ID_SPEED_X1,
ID_SPEED_X2,
ID_SPEED_X5,
ID_SPEED_X10,
ID_SPEED_FULL,
};
toolbar.SetButtons(commands, sizeof(commands)/sizeof(commands[0]));
// Make toolbar dockable
/*toolbar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&toolbar);*/
return 0;
}
void CutterPreviewWindow::OnControlcutterStart()
{
if (isCutting) {
isPaused = false;
}
else {
isCutting = true;
runCut();
isCutting = false;
}
}
void CutterPreviewWindow::OnControlcutterPause()
{
isPaused = true;
}
void CutterPreviewWindow::OnControlcutterStop()
{
terminate = true;
isPaused = false;
isCutting = false;
}
void CutterPreviewWindow::OnUpdateControlcutterPause(CCmdUI* pCmdUI)
{
pCmdUI->Enable((isCutting & !isPaused) ? TRUE : FALSE);
}
void CutterPreviewWindow::OnUpdateControlcutterStart(CCmdUI* pCmdUI)
{
pCmdUI->Enable((!isCutting || isPaused) ? TRUE : FALSE);
}
void CutterPreviewWindow::OnUpdateControlcutterStop(CCmdUI* pCmdUI)
{
pCmdUI->Enable(isCutting ? TRUE : FALSE);
}
void CutterPreviewWindow::OnSpeedFull()
{
stepTime.QuadPart = 0;
}
void CutterPreviewWindow::OnSpeedX1()
{
stepTime.QuadPart = counterFrequency.QuadPart / DEFAULT_STEPS_PER_SEC;
}
void CutterPreviewWindow::OnSpeedX10()
{
stepTime.QuadPart = counterFrequency.QuadPart / (DEFAULT_STEPS_PER_SEC * 10);
}
void CutterPreviewWindow::OnSpeedX2()
{
stepTime.QuadPart = counterFrequency.QuadPart / (DEFAULT_STEPS_PER_SEC * 2);
}
void CutterPreviewWindow::OnSpeedX5()
{
stepTime.QuadPart = counterFrequency.QuadPart / (DEFAULT_STEPS_PER_SEC * 5);
}