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
|
//-----------------------------------------------------------------------------
// Copyright 2007 Jonathan Westhues
//
// This file is part of LDmicro.
//
// LDmicro is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// LDmicro is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LDmicro. If not, see <http://www.gnu.org/licenses/>.
//------
//
// The two `output devices' for the drawing code: either the export as text
// stuff to write to a file, or all the routines concerned with drawing to
// the screen.
// Jonathan Westhues, Dec 2004
//
// Ported to linus by: R Ramana, 2018
//-----------------------------------------------------------------------------
#include "linuxUI.h"
#include <stdio.h>
#include <stdlib.h>
#include "ldmicro.h"
void (*DrawChars)(HCRDC Hcr, int, int, const char *);
// After an undo all the memory addresses change but make an effort to put
// the cursor roughly where it should be.
int SelectedGxAfterNextPaint = -1;
int SelectedGyAfterNextPaint = -1;
// After pushing a rung up or down the position of that rung in the table
// changes, but the cursor should stay where it was.
BOOL ScrollSelectedIntoViewAfterNextPaint;
// Buffer that we write to when exporting (drawing) diagram to a text file.
// Dynamically allocated so that we're at least slightly efficient.
static char **ExportBuffer;
// The fonts that we will use to draw the ladder diagram: fixed width, one
// normal-weight, one bold.
HFONT FixedWidthFont;
HFONT FixedWidthFontBold;
// Different colour brushes for right and left buses in simulation, but same
// colour for both in edit mode; also for the backgrounds in simulation and
// edit modes.
static HBRUSH BusRightBus;
static HBRUSH BusLeftBrush;
static HBRUSH BusBrush;
static HBRUSH BgBrush;
static HBRUSH SimBgBrush;
// Parameters that determine our offset if we are scrolled
// int ScrollXOffset;
// int ScrollYOffset;
// int ScrollXOffsetMax;
// int ScrollYOffsetMax;
// Is the cursor currently drawn? We XOR it so this gets toggled.
static BOOL CursorDrawn;
// Colours with which to do syntax highlighting, configurable
SyntaxHighlightingColours HighlightColours;
#define X_RIGHT_PADDING 30
//-----------------------------------------------------------------------------
// Blink the cursor on the schematic; called by a Windows timer. We XOR
// draw it so just draw the same rectangle every time to show/erase the
// cursor. Cursor may be in one of four places in the selected leaf (top,
// bottom, left, right) but we don't care; just go from the coordinates
// computed when we drew the schematic in the paint procedure.
//-----------------------------------------------------------------------------
BOOL BlinkCursor(BOOL kill = FALSE)
{
// if(GetFocus(MainWindow) != !CursorDrawn) return TRUE;
if(Cursor.left() == 0)
{
CursorObject->setVisible(FALSE);
return TRUE;
}
QRect c;
c = Cursor;
c.setTop(c.top() - POS_HEIGHT*FONT_HEIGHT);
c.setLeft(c.left());
if(DrawWindow == NULL)
return FALSE;
HWID Hcr = DrawWindow;
static int PREV_x = c.left();
static int PREV_y = c.top();
static int PREV_w = c.width();
static int PREV_h = c.height();
if (PREV_x != c.left() || PREV_y != c.top() ||
PREV_w != c.width() || PREV_h != c.height())
{
scrollbar->setFrameRect(c);
CursorObject->setGeometry(c);
CursorObject->setVisible(TRUE);
scrollbar->ensureWidgetVisible(CursorObject);
PREV_x = c.left();
PREV_y = c.top();
PREV_w = c.width();
PREV_h = c.height();
}
CursorObject->setGeometry(Cursor);
if (CursorObject->isVisible())
CursorObject->setVisible(FALSE);
else
CursorObject->setVisible(TRUE);
InvalidateRect(DrawWindow, NULL, FALSE);
return !kill;
}
//-----------------------------------------------------------------------------
// Output a string to the screen at a particular location, in character-
// sized units.
//-----------------------------------------------------------------------------
static void DrawCharsToScreen(HCRDC Hcr, int cx, int cy, const char *str)
{
if(cy < -2) return;
COLORREF prev;
BOOL firstTime = TRUE;
BOOL inNumber = FALSE;
BOOL inComment = FALSE;
int inBrace = 0;
for(; *str; str++, cx++) {
int x = cx * FONT_WIDTH + X_PADDING;
int y = cy * FONT_HEIGHT + Y_PADDING;
BOOL hiOk = !(InSimulationMode || ThisHighlighted);
if(strchr("{}[]", *str) && hiOk && !inComment) {
if(*str == '{' || *str == '[') inBrace++;
if(inBrace == 1) {
prev = GetTextColor(Hcr);
SetTextColor(Hcr, HighlightColours.punct);
TextOut(DrawWindow, Hcr, x, y, str, 1);
SetTextColor(Hcr, prev);
} else {
TextOut(DrawWindow, Hcr, x, y, str, 1);
}
if(*str == ']' || *str == '}') inBrace--;
} else if((
(isdigit(*str) && (firstTime || isspace(str[-1])
|| str[-1] == ':' || str[-1] == '[')) ||
(*str == '-' && isdigit(str[1]))) && hiOk && !inComment)
{
prev = GetTextColor(Hcr);
SetTextColor(Hcr, HighlightColours.lit);
TextOut(DrawWindow, Hcr, x, y, str, 1);
SetTextColor(Hcr, prev);
inNumber = TRUE;
} else if(*str == '\x01') {
cx--;
if(hiOk) {
prev = GetTextColor(Hcr);
SetTextColor(Hcr, HighlightColours.op);
}
} else if(*str == '\x02') {
cx--;
if(hiOk) {
SetTextColor(Hcr, prev);
inComment = FALSE;
}
} else if(*str == '\x03') {
cx--;
if(hiOk) {
prev = GetTextColor(Hcr);
SetTextColor(Hcr, HighlightColours.comment);
inComment = TRUE;
}
} else if(inNumber) {
if(isdigit(*str) || *str == '.') {
prev = GetTextColor(Hcr);
SetTextColor(Hcr, HighlightColours.lit);
TextOut(DrawWindow, Hcr, x, y, str, 1);
SetTextColor(Hcr, prev);
} else {
TextOut(DrawWindow, Hcr, x, y, str, 1);
inNumber = FALSE;
}
} else {
TextOut(DrawWindow, Hcr, x, y, str, 1);
}
firstTime = FALSE;
}
}
//-----------------------------------------------------------------------------
// Total number of columns that we can display in the given amount of
// window area. Need to leave some slop on the right for the scrollbar, of
// course.
//-----------------------------------------------------------------------------
int ScreenColsAvailable(void)
{
int rw = DrawWindow->width();
return (rw - (X_PADDING + X_RIGHT_PADDING)) / (POS_WIDTH*FONT_WIDTH);
}
//-----------------------------------------------------------------------------
// Total number of columns that we can display in the given amount of
// window area. Need to leave some slop on the right for the scrollbar, of
// course, and extra slop at the bottom for the horiz scrollbar if it is
// shown.
//-----------------------------------------------------------------------------
int ScreenRowsAvailable(void)
{
int adj;
/*if(ScrollXOffsetMax == 0) {
adj = 0;
} else {*/
adj = 18;
// }
return (IoListTop - Y_PADDING - adj) / (POS_HEIGHT*FONT_HEIGHT);
}
void PaintWidget::timerEvent(QTimerEvent *event)
{
if(event->timerId() == CursorTimer)
{
BlinkCursor();
// printf("CursorTimer\n");
}
if(event->timerId() == SimulateTimer)
{
PlcCycleTimer(FALSE);
}
}
//-----------------------------------------------------------------------------
// Paint the ladder logic program to the screen. Also figure out where the
// cursor should go and fill in coordinates for BlinkCursor. Not allowed to
// draw deeper than IoListTop, as we would run in to the I/O listbox.
//-----------------------------------------------------------------------------
void PaintWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setClipping(true);
painter.setRenderHint(QPainter::TextAntialiasing);
HCRDC Hcr = &painter;
ok();
int bw = this->width();// = r.right;
int bh = this->height();
/// now figure out how we should draw the ladder logic
ColsAvailable = ProgCountWidestRow();
if(ColsAvailable < ScreenColsAvailable()) {
ColsAvailable = ScreenColsAvailable();
}
memset(DisplayMatrix, 0, sizeof(DisplayMatrix));
SelectionActive = FALSE;
memset(&Cursor, 0, sizeof(Cursor));
DrawChars = DrawCharsToScreen;
int i;
int cy = 0;
int cx = 0;
// int rowsAvailable = ScreenRowsAvailable();
for(i = 0; i < Prog.numRungs; i++) {
int thisHeight = POS_HEIGHT*CountHeightOfElement(ELEM_SERIES_SUBCKT,
Prog.rungs[i]);
// For speed, there is no need to draw everything all the time, but
// we still must draw a bit above and below so that the DisplayMatrix
// is filled in enough to make it possible to reselect using the
// cursor keys.
/*if(((cy + thisHeight) >= (ScrollYOffset - 8)*POS_HEIGHT) &&
(cy < (ScrollYOffset + rowsAvailable + 8)*POS_HEIGHT))*/
{
SetBkColor(DrawWindow, Hcr,
InSimulationMode ? HighlightColours.simBg :
HighlightColours.bg);
SetTextColor(Hcr, InSimulationMode ? HighlightColours.simRungNum :
HighlightColours.rungNum);
SelectObject(Hcr, FixedWidthFont);
int rung = i + 1;
int y = Y_PADDING + FONT_HEIGHT*cy;
int yp = y; /*+ FONT_HEIGHT*(POS_HEIGHT/2) -
POS_HEIGHT*FONT_HEIGHT*ScrollYOffset;*/
if(rung < 10) {
char r[1] = { rung + '0' };
TextOut(DrawWindow, Hcr, 8 + FONT_WIDTH, yp, r, 1);
} else {
char r[2] = { (rung / 10) + '0', (rung % 10) + '0' };
TextOut(DrawWindow, Hcr, 8, yp, r, 2);
}
cx = 0;
DrawElement(Hcr, ELEM_SERIES_SUBCKT, Prog.rungs[i], &cx, &cy,
Prog.rungPowered[i]);
}
cy += thisHeight;
cy += POS_HEIGHT;
}
QSize DWSize = this->size();
int newHeight = ((cy + (POS_HEIGHT/2)) * FONT_HEIGHT + Y_PADDING + 50);
if(DWSize.height() + POS_HEIGHT < newHeight)
{
DWSize.setHeight(newHeight);
}
int newWidth = ((cx + (POS_WIDTH/2)) * FONT_WIDTH + X_PADDING);
if(DWSize.width() + POS_WIDTH < newWidth)
{
DWSize.setWidth(newWidth);
}
DrawWindow->resize(DWSize);
cy -= 2;
DrawEndRung(Hcr, 0, cy);
if(SelectedGxAfterNextPaint >= 0) {
MoveCursorNear(SelectedGxAfterNextPaint, SelectedGyAfterNextPaint);
// InvalidateRect(DrawWindow, NULL, FALSE);
SelectedGxAfterNextPaint = -1;
SelectedGyAfterNextPaint = -1;
} else if(ScrollSelectedIntoViewAfterNextPaint && Selected) {
SelectElement(-1, -1, Selected->selectedState);
ScrollSelectedIntoViewAfterNextPaint = FALSE;
// InvalidateRect(DrawWindow, NULL, FALSE);
} else {
if(!SelectionActive) {
if(Prog.numRungs > 0) {
if(MoveCursorTopLeft()) {
// InvalidateRect(DrawWindow, NULL, FALSE);
}
}
}
}
/// draw the `buses' at either side of the screen
QRect r;
r.setLeft(X_PADDING - FONT_WIDTH);
r.setTop(0);
r.setRight(r.left() + 4);
r.setBottom(this->height());
FillRect(Hcr, &r, InSimulationMode ? BusLeftBrush : BusBrush);
r.setLeft(POS_WIDTH*FONT_WIDTH*ColsAvailable + 32);
r.setRight(POS_WIDTH*FONT_WIDTH*ColsAvailable + 32);
FillRect(Hcr, &r, InSimulationMode ? BusRightBus : BusBrush);
// InvalidateRect(DrawWindow, NULL, FALSE);
CursorDrawn = FALSE;
ok();
}
//-----------------------------------------------------------------------------
// Set up the syntax highlighting colours, according to the currently selected
// scheme.
//-----------------------------------------------------------------------------
static void SetSyntaxHighlightingColours(void)
{
static const SyntaxHighlightingColours Schemes[] = {
{
RGB(0, 0, 0), // bg
RGB(255, 255, 225), // def
RGB(255, 110, 90), // selected
RGB(255, 150, 90), // op
RGB(255, 255, 100), // punct
RGB(255, 160, 160), // lit
RGB(120, 255, 130), // name
RGB(130, 130, 130), // rungNum
RGB(130, 130, 245), // comment
RGB(255, 255, 255), // bus
RGB(0, 0, 0), // simBg
RGB(130, 130, 130), // simRungNum
RGB(170, 200, 200), // simOff
RGB(255, 150, 150), // simOn
RGB(255, 150, 150), // simBusLeft
RGB(150, 150, 255), // simBusRight
},
};
memcpy(&HighlightColours, &Schemes[0], sizeof(Schemes[0]));
}
//-----------------------------------------------------------------------------
// Set up the stuff we'll need to draw our schematic diagram. Fonts, brushes,
// pens, etc.
//-----------------------------------------------------------------------------
void InitForDrawing(void)
{
DrawWindow = new PaintWidget();
DrawWindow->setFocusPolicy(Qt::StrongFocus);
SetSyntaxHighlightingColours();
FixedWidthFont = CreateFont(
FONT_HEIGHT, FONT_WIDTH,
0,
FW_REGULAR,
FALSE,
"Lucida Console");
FixedWidthFontBold = CreateFont(
FONT_HEIGHT, FONT_WIDTH,
0,
FW_BOLD, // the bold text renders funny under Vista
FALSE,
"Lucida Console");
LOGBRUSH lb;
lb.lbStyle = BS_SOLID;
lb.lbColor = HighlightColours.simBusRight;
BusRightBus = CreateBrushIndirect(&lb);
lb.lbColor = HighlightColours.simBusLeft;
BusLeftBrush = CreateBrushIndirect(&lb);
lb.lbColor = HighlightColours.bus;
BusBrush = CreateBrushIndirect(&lb);
lb.lbColor = HighlightColours.bg;
BgBrush = CreateBrushIndirect(&lb);
lb.lbColor = HighlightColours.simBg;
SimBgBrush = CreateBrushIndirect(&lb);
}
//-----------------------------------------------------------------------------
// DrawChars function, for drawing to the export buffer instead of to the
// screen.
//-----------------------------------------------------------------------------
static void DrawCharsToExportBuffer(HCRDC Hcr, int cx, int cy, const char *str)
{
while(*str) {
if(*str >= 10) {
ExportBuffer[cy][cx] = *str;
cx++;
}
str++;
}
}
//-----------------------------------------------------------------------------
// Export a text drawing of the ladder logic program to a file.
//-----------------------------------------------------------------------------
void ExportDrawingAsText(char *file)
{
int maxWidth = ProgCountWidestRow();
ColsAvailable = maxWidth;
int totalHeight = 0;
int i;
for(i = 0; i < Prog.numRungs; i++) {
totalHeight += CountHeightOfElement(ELEM_SERIES_SUBCKT, Prog.rungs[i]);
totalHeight += 1;
}
totalHeight *= POS_HEIGHT;
totalHeight += 3;
ExportBuffer = (char **)CheckMalloc(totalHeight * sizeof(char *));
int l = maxWidth*POS_WIDTH + 8;
for(i = 0; i < totalHeight; i++) {
ExportBuffer[i] = (char *)CheckMalloc(l);
memset(ExportBuffer[i], ' ', l-1);
ExportBuffer[i][4] = '|';
ExportBuffer[i][3] = '|';
ExportBuffer[i][l-3] = '|';
ExportBuffer[i][l-2] = '|';
ExportBuffer[i][l-1] = '\0';
}
DrawChars = DrawCharsToExportBuffer;
int cy = 1;
for(i = 0; i < Prog.numRungs; i++) {
int cx = 5;
DrawElement(NULL, ELEM_SERIES_SUBCKT, Prog.rungs[i], &cx, &cy,
Prog.rungPowered[i]);
if((i + 1) < 10) {
ExportBuffer[cy+1][1] = '0' + (i + 1);
} else {
ExportBuffer[cy+1][1] = '0' + ((i + 1) % 10);
ExportBuffer[cy+1][0] = '0' + ((i + 1) / 10);
}
cy += POS_HEIGHT*CountHeightOfElement(ELEM_SERIES_SUBCKT,
Prog.rungs[i]);
cy += POS_HEIGHT;
}
cy -= 2;
DrawEndRung(NULL, 5, cy);
FILE *f = fopen(file, "w");
if(!f) {
Error(_("Couldn't open '%s'\n"), f);
return;
}
fprintf(f, "LDmicro export text\n");
if(Prog.mcu) {
fprintf(f, "for '%s', %.6f MHz crystal, %.1f ms cycle time\n\n",
Prog.mcu->mcuName, Prog.mcuClock/1e6, Prog.cycleTime/1e3);
} else {
fprintf(f, "no MCU assigned, %.6f MHz crystal, %.1f ms cycle time\n\n",
Prog.mcuClock/1e6, Prog.cycleTime/1e3);
}
fprintf(f, "\nLADDER DIAGRAM:\n\n");
for(i = 0; i < totalHeight; i++) {
ExportBuffer[i][4] = '|';
fprintf(f, "%s\n", ExportBuffer[i]);
CheckFree(ExportBuffer[i]);
}
CheckFree(ExportBuffer);
ExportBuffer = NULL;
fprintf(f, _("\n\nI/O ASSIGNMENT:\n\n"));
fprintf(f, _(" Name | Type | Pin\n"));
fprintf(f, " ----------------------------+--------------------+------\n");
for(i = 0; i < Prog.io.count; i++) {
char b[1024];
memset(b, '\0', sizeof(b));
PlcProgramSingleIo *io = &Prog.io.assignment[i];
char *type = IoTypeToString(io->type);
char pin[MAX_NAME_LEN];
PinNumberForIo(pin, io);
sprintf(b, " | | %s\n",
pin);
memcpy(b+2, io->name, strlen(io->name));
memcpy(b+31, type, strlen(type));
fprintf(f, "%s", b);
}
fclose(f);
// we may have trashed the grid tables a bit; a repaint will fix that
InvalidateRect(MainWindow, NULL, FALSE);
}
|