summaryrefslogtreecommitdiff
path: root/ldmicro
diff options
context:
space:
mode:
authorRr422018-06-26 14:58:14 +0530
committerRr422018-06-26 14:58:14 +0530
commit9673954adfd5a563072e085df5e09d427557466b (patch)
treee83ee518b6c23a37d5668fbcfb401b838bb21b59 /ldmicro
parentf0d0df7fb0658b315f79748e713aa4103c4b335e (diff)
downloadLDMicroGtk-9673954adfd5a563072e085df5e09d427557466b.tar.gz
LDMicroGtk-9673954adfd5a563072e085df5e09d427557466b.tar.bz2
LDMicroGtk-9673954adfd5a563072e085df5e09d427557466b.zip
Added "export as text" functionality.
Diffstat (limited to 'ldmicro')
-rw-r--r--ldmicro/draw_outputdev.cpp187
-rw-r--r--ldmicro/lib/linuxUI/linuxUI.cpp18
2 files changed, 112 insertions, 93 deletions
diff --git a/ldmicro/draw_outputdev.cpp b/ldmicro/draw_outputdev.cpp
index eab7324..cf2cae4 100644
--- a/ldmicro/draw_outputdev.cpp
+++ b/ldmicro/draw_outputdev.cpp
@@ -102,7 +102,7 @@ BOOL BlinkCursor(BOOL kill = FALSE)
// if(!GDK_IS_DRAWING_CONTEXT(Hdc))
// return FALSE;
- HCRDC Hcr = gdk_cairo_create(gtk_widget_get_window(DrawWindow));//gdk_drawing_context_get_cairo_context(Hdc);//
+ HCRDC Hcr = gdk_cairo_create(gtk_widget_get_window(DrawWindow));
static int PREV_x = c.left;
static int PREV_y = c.top;
@@ -429,7 +429,7 @@ void InitForDrawing(void)
// DrawChars function, for drawing to the export buffer instead of to the
// screen.
//-----------------------------------------------------------------------------
-static void DrawCharsToExportBuffer(int cx, int cy, char *str)
+static void DrawCharsToExportBuffer(HCRDC Hcr, int cx, int cy, const char *str)
{
while(*str) {
if(*str >= 10) {
@@ -445,105 +445,106 @@ static void DrawCharsToExportBuffer(int cx, int cy, char *str)
//-----------------------------------------------------------------------------
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 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(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(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"));
+ 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));
+ 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];
+ PlcProgramSingleIo *io = &Prog.io.assignment[i];
+ char *type = IoTypeToString(io->type);
+ char pin[MAX_NAME_LEN];
- // PinNumberForIo(pin, io);
+ PinNumberForIo(pin, io);
- // sprintf(b, " | | %s\n",
- // pin);
+ sprintf(b, " | | %s\n",
+ pin);
- // memcpy(b+2, io->name, strlen(io->name));
- // memcpy(b+31, type, strlen(type));
- // fprintf(f, "%s", b);
- // }
+ memcpy(b+2, io->name, strlen(io->name));
+ memcpy(b+31, type, strlen(type));
+ fprintf(f, "%s", b);
+ }
- // fclose(f);
+ fclose(f);
- // // we may have trashed the grid tables a bit; a repaint will fix that
- // InvalidateRect(MainWindow, NULL, FALSE);
+ // we may have trashed the grid tables a bit; a repaint will fix that
+ InvalidateRect(MainWindow, NULL, FALSE);
}
//-----------------------------------------------------------------------------
diff --git a/ldmicro/lib/linuxUI/linuxUI.cpp b/ldmicro/lib/linuxUI/linuxUI.cpp
index e65c7d5..0d08de5 100644
--- a/ldmicro/lib/linuxUI/linuxUI.cpp
+++ b/ldmicro/lib/linuxUI/linuxUI.cpp
@@ -282,6 +282,9 @@ HANDLE GetStockObject(int fnObject)
void SelectObject(HCRDC hcr, HFONT hfont)
{
+ if (hcr ==NULL)
+ return;
+
cairo_select_font_face(hcr, hfont->lpszFace,
hfont->fdwItalic ? CAIRO_FONT_SLANT_ITALIC : CAIRO_FONT_SLANT_NORMAL,
hfont->fnWeight == FW_BOLD ? CAIRO_FONT_WEIGHT_BOLD : CAIRO_FONT_WEIGHT_NORMAL);
@@ -326,6 +329,9 @@ HFONT CreateFont(int nHeight, int nWidth, int nOrientation, int fnWeight,
void SetBkColor(HWID widget, HCRDC hcr, COLORREF bkCol)
{
+ if (hcr == NULL)
+ return;
+
gtk_widget_override_background_color(GTK_WIDGET(widget),
GTK_STATE_FLAG_NORMAL, &bkCol);
@@ -349,12 +355,18 @@ void SetBkColor(HWID widget, HCRDC hcr, COLORREF bkCol)
void SetTextColor(HCRDC hcr, COLORREF color)
{
+ if (hcr == NULL)
+ return;
+
HdcCurrentTextColor = color;
gdk_cairo_set_source_rgba (hcr, &color);
}
void TextOut(HWID hWid, HCRDC hcr, int nXStart, int nYStart, LPCTSTR lpString, int cchString)
{
+ if (hcr == NULL)
+ return;
+
nYStart += 30;
cairo_text_extents_t extents;
@@ -419,6 +431,9 @@ BOOL InvalidateRect(HWID hWid, const RECT *lpRect, BOOL bErase)
int FillRect(HCRDC hDC, const RECT *lprc, HBRUSH hbr)
{
+ if (hDC == NULL)
+ return -1;
+
GDRECT gdrc;
RECT_to_GDRECT(lprc, &gdrc);
@@ -432,6 +447,9 @@ int FillRect(HCRDC hDC, const RECT *lprc, HBRUSH hbr)
BOOL PatBlt(HCRDC hdc, int nXLeft, int nYLeft, int nWidth, int nHeight, DWORD dwRop, HBRUSH hbr)
{
+ if (hdc == NULL)
+ return FALSE;
+
cairo_set_source_rgb(hdc, hbr->red, hbr->green, hbr->blue);
cairo_rectangle(hdc, nXLeft, nYLeft + 20, nWidth, nHeight);
cairo_stroke_preserve(hdc);