edit_sequential.c
来自「CNC 的开放码,EMC2 V2.2.8版」· C语言 代码 · 共 886 行 · 第 1/2 页
C
886 行
/* Classic Ladder Project *//* Copyright (C) 2001-2006 Marc Le Douarain *//* http://www.multimania.com/mavati/classicladder *//* http://www.sourceforge.net/projects/classicladder *//* December 2003 *//* --------------------------- *//* Editor for Sequential Pages *//* --------------------------- *//* This part of the editor is the one who will not change even if if we use *//* another gui instead of gtk... who know? *//* ------------------------------------------------------------- *//* This library is free software; you can redistribute it and/or *//* modify it under the terms of the GNU Lesser General Public *//* License as published by the Free Software Foundation; either *//* version 2.1 of the License, or (at your option) any later version. *//* This library 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 *//* Lesser General Public License for more details. *//* You should have received a copy of the GNU Lesser General Public *//* License along with this library; if not, write to the Free Software *//* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include <gtk/gtk.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "classicladder.h"#include "global.h"#include "drawing.h"#include "edit.h"#include "editproperties_gtk.h"#include "classicladder_gtk.h"#include "calc_sequential.h"#include "edit_sequential.h"/* We modify the datas in this variable. It is only after clicking on apply that they are used */StrSequential EditSeqDatas;int TypeSeqEleEdited = -1;int OffsetSeqEleEdited = 0;char TopSeqEleEdited = FALSE;/* for elements requiring many clicks to be done (link, multi-steps, 'Or' in transitions) */int CptNbrClicksDone = 0;int NumElementSelectedInToolBarBak = -1;void LoadSeqElementProperties( void ){ char TextToWrite[100]; int NumParam; for(NumParam=0;NumParam<NBR_PARAMS_PER_OBJ;NumParam++) SetProperty(NumParam,"---",""); if ( TypeSeqEleEdited!=-1 ) { switch( TypeSeqEleEdited ) { case ELE_SEQ_STEP: sprintf( TextToWrite, "%d", EditSeqDatas.Step[ OffsetSeqEleEdited ].StepNumber ); SetProperty(0,"Step Nbr",TextToWrite); break; case ELE_SEQ_TRANSITION: strcpy( TextToWrite, DisplayInfo( EditSeqDatas.Transition[ OffsetSeqEleEdited ].VarTypeCondi, EditSeqDatas.Transition[ OffsetSeqEleEdited ].VarNumCondi ) ); SetProperty(0,"Variable",TextToWrite); break; case ELE_SEQ_COMMENT: SetProperty(0, "Comment", EditSeqDatas.SeqComment[ OffsetSeqEleEdited ].Comment ); break; } }}void SaveSeqElementProperties( void ){ int StepNbr; if ( TypeSeqEleEdited!=-1 ) { switch( TypeSeqEleEdited ) { case ELE_SEQ_STEP: if ( TextToNumber( GetProperty(0),0,9999,&StepNbr ) ) EditSeqDatas.Step[ OffsetSeqEleEdited ].StepNumber = StepNbr; break; case ELE_SEQ_TRANSITION: if ( TextParserForAVar( GetProperty(0),&EditSeqDatas.Transition[ OffsetSeqEleEdited ].VarTypeCondi, &EditSeqDatas.Transition[ OffsetSeqEleEdited ].VarNumCondi, NULL, FALSE/*PartialNames*/ )==FALSE ) { if (ErrorMessageVarParser) ShowMessageBox("Error",ErrorMessageVarParser,"Ok"); else ShowMessageBox( "Error", "Unknown variable...", "Ok" ); } break; case ELE_SEQ_COMMENT: strncpy( EditSeqDatas.SeqComment[ OffsetSeqEleEdited ].Comment, GetProperty(0), SEQ_COMMENT_LGT ); EditSeqDatas.SeqComment[ OffsetSeqEleEdited ].Comment[ SEQ_COMMENT_LGT-1 ] = '\0'; break; } /* display back to show what we have really understand... */ LoadSeqElementProperties( ); }}void ModifyCurrentSeqPage(){ memcpy( &EditSeqDatas, Sequential, sizeof( StrSequential ) ); EditDatas.ModeEdit = TRUE;// autorize_prevnext_buttons(FALSE);}void CancelSeqPageEdited(){ EditDatas.ModeEdit = FALSE; EditDatas.NumElementSelectedInToolBar = -1; TypeSeqEleEdited = -1; LoadSeqElementProperties( );// autorize_prevnext_buttons(TRUE);}void ApplySeqPageEdited(){ //TODO: passing in STOP and waiting not under calc... memcpy( Sequential, &EditSeqDatas, sizeof( StrSequential ) ); //TODO: passing in RUN now... EditDatas.ModeEdit = FALSE; EditDatas.NumElementSelectedInToolBar = -1; TypeSeqEleEdited = -1; LoadSeqElementProperties( );// autorize_prevnext_buttons(TRUE); PrepareSequential( ); InfosGene->AskConfirmationToQuit = TRUE;}int SearchStepElement( int PageNumber, int PositionX, int PositionY ){ int ScanStep; StrStep * pStep; int Result = -1;printf("step search posiX=%d, posiY=%d ; ", PositionX, PositionY ); for( ScanStep=0; ScanStep<NBR_STEPS; ScanStep++ ) { pStep = &EditSeqDatas.Step[ ScanStep ]; if ( pStep->NumPage==PageNumber ) { if ( pStep->PosiX==PositionX && pStep->PosiY==PositionY ) Result = ScanStep; } }printf("found=%d!!!\n", Result ); return Result;}int SearchTransiElement( int PageNumber, int PositionX, int PositionY ){ int ScanTransi; StrTransition * pTransi; int Result = -1;printf("transi search posiX=%d, posiY=%d ; ", PositionX, PositionY ); for( ScanTransi=0; ScanTransi<NBR_TRANSITIONS; ScanTransi++ ) { pTransi = &EditSeqDatas.Transition[ ScanTransi ]; if ( pTransi->NumPage==PageNumber ) { if ( pTransi->PosiX==PositionX && pTransi->PosiY==PositionY ) Result = ScanTransi; } }printf("found=%d!!!\n", Result ); return Result;}int SearchCommentElement( int PageNumber, int PositionX, int PositionY ){ int ScanComment; StrSeqComment * pComment; int Result = -1;printf("comment search posiX=%d, posiY=%d ; ", PositionX, PositionY ); for( ScanComment=0; ScanComment<NBR_SEQ_COMMENTS; ScanComment++ ) { pComment = &EditSeqDatas.SeqComment[ ScanComment ]; if ( pComment->NumPage==PageNumber ) { if ( ( pComment->PosiX<=PositionX && PositionX<pComment->PosiX+4 ) && pComment->PosiY==PositionY ) Result = ScanComment; } }printf("found=%d!!!\n", Result ); return Result;}/* -1 if not found */int FindFreeStep( void ){ int ScanStep = 0; StrStep * pStep; int Result = -1; do { pStep = &EditSeqDatas.Step[ ScanStep ]; if ( pStep->NumPage==-1 ) Result = ScanStep; else ScanStep++; } while( Result==-1 && ScanStep<NBR_STEPS );printf("found free step=%d!!!\n", Result ); return Result;}/* -1 if not found */int FindFreeTransi( void ){ int ScanTransi = 0; StrTransition * pTransi; int Result = -1; do { pTransi = &EditSeqDatas.Transition[ ScanTransi ]; if ( pTransi->NumPage==-1 ) Result = ScanTransi; else ScanTransi++; } while( Result==-1 && ScanTransi<NBR_TRANSITIONS );printf("found free transi=%d!!!\n", Result ); return Result;}/* -1 if not found */int FindFreeComment( void ){ int ScanComment = 0; StrSeqComment * pComment; int Result = -1; do { pComment = &EditSeqDatas.SeqComment[ ScanComment ];//printf("scan comment %d = page:%d\n", ScanComment, pComment->NumPage ); if ( pComment->NumPage==-1 ) Result = ScanComment; else ScanComment++; } while( Result==-1 && ScanComment<NBR_SEQ_COMMENTS );printf("found free comment=%d!!!\n", Result ); return Result;}void DestroyStep( int Offset ){ StrStep * pStep = &EditSeqDatas.Step[ Offset ]; pStep->NumPage = -1;}/* -1 if not created */int CreateStep( int page, int x, int y, char init ){ int TransiAssoc; int OffsetStepCreated = FindFreeStep( ); int TopStepForAutoNumber = -1; int NumStepForAutoNumber = 0; if ( OffsetStepCreated!=-1 ) { StrStep * pStep = &EditSeqDatas.Step[ OffsetStepCreated ]; pStep->NumPage = page; pStep->PosiX = x; pStep->PosiY = y; pStep->InitStep = init; // look if there is a step on the top, to directly give next step number TopStepForAutoNumber = SearchStepElement( page, x, y-2 ); if ( TopStepForAutoNumber==-1 ) { // search next step number available... int ScanStep = 0; for( ScanStep=0; ScanStep<NBR_STEPS; ScanStep++ ) { StrStep * pScanStep = &EditSeqDatas.Step[ ScanStep ]; if ( pScanStep->NumPage!=-1 ) { // already used ? if ( NumStepForAutoNumber==pScanStep->StepNumber ) NumStepForAutoNumber = pScanStep->StepNumber+1; } } } else { NumStepForAutoNumber = EditSeqDatas.Step[ TopStepForAutoNumber ].StepNumber+1; } pStep->StepNumber = NumStepForAutoNumber; // search top transi to connect with TransiAssoc = SearchTransiElement( page, x, y-1 ); if ( TransiAssoc!=-1 ) EditSeqDatas.Transition[ TransiAssoc ].NumStepToActiv[ 0 ] = OffsetStepCreated; // search bottom transi to connect with TransiAssoc = SearchTransiElement( page, x, y+1 ); if ( TransiAssoc!=-1 ) EditSeqDatas.Transition[ TransiAssoc ].NumStepToDesactiv[ 0 ] = OffsetStepCreated; } return OffsetStepCreated;}void DestroyTransi( int Offset ){ StrTransition * pTransi = &EditSeqDatas.Transition[ Offset ]; pTransi->NumPage = -1;}/* -1 if not created */int CreateTransi( int page, int x, int y ){ int StepAssoc; int NumSwitch; int OffsetCreated = FindFreeTransi( ); int TopTransiForAutoVar = -1; if ( OffsetCreated!=-1 ) { StrTransition * pTransi = &EditSeqDatas.Transition[ OffsetCreated ]; pTransi->NumPage = page; pTransi->PosiX = x; pTransi->PosiY = y; pTransi->VarTypeCondi = VAR_MEM_BIT; pTransi->VarNumCondi = 0; // look if there is a transition on the top, to directly give next variable number TopTransiForAutoVar = SearchTransiElement( page, x, y-2 ); if ( TopTransiForAutoVar!=-1 ) { pTransi->VarTypeCondi = EditSeqDatas.Transition[ TopTransiForAutoVar ].VarTypeCondi; pTransi->VarNumCondi = EditSeqDatas.Transition[ TopTransiForAutoVar ].VarNumCondi+1; } for( NumSwitch=0; NumSwitch<NBR_SWITCHS_MAX; NumSwitch++ ) { pTransi->NumStepToActiv[ NumSwitch ] = -1; pTransi->NumStepToDesactiv[ NumSwitch ] = -1; pTransi->NumTransLinkedForStart[ NumSwitch ] = -1; pTransi->NumTransLinkedForEnd[ NumSwitch ] = -1; } // search top step to connect with if ( y>0 ) { StepAssoc = SearchStepElement( page, x, y-1 ); if ( StepAssoc!=-1 ) pTransi->NumStepToDesactiv[ 0 ] = StepAssoc; } // search bottom step to connect with if ( y<SEQ_PAGE_HEIGHT-1 ) { StepAssoc = SearchStepElement( page, x, y+1 ); if ( StepAssoc!=-1 ) pTransi->NumStepToActiv[ 0 ] = StepAssoc; } } return OffsetCreated;}void DestroyComment( int Offset ){ StrSeqComment * pComment = &EditSeqDatas.SeqComment[ Offset ]; pComment->NumPage = -1;}/* -1 if could not be created */int CreateComment( int page, int x, int y ){ int OffsetCommentCreated = FindFreeComment( ); if ( OffsetCommentCreated!=-1 ) { StrSeqComment * pComment = &EditSeqDatas.SeqComment[ OffsetCommentCreated ]; pComment->Comment[ 0 ] = '\0'; pComment->NumPage = page; pComment->PosiX = x; pComment->PosiY = y; } return OffsetCommentCreated;}void DoLinkTransitionAndStep( int OffsetTransi, char TopOfTransi, int OffsetStep ){ StrTransition * pTransi = &EditSeqDatas.Transition[ OffsetTransi ];printf("Do link : transi=%d (top=%d), step=%d\n", OffsetTransi, TopOfTransi, OffsetStep); if ( TopOfTransi ) pTransi->NumStepToDesactiv[ 0 ] = OffsetStep; else pTransi->NumStepToActiv[ 0 ] = OffsetStep;}/* return TRUE is okay */char CommonSearchForManyStepsOrTransi( char ForManySteps, int TypeEle1, int OffEle1, int TypeEle2, int OffEle2, int * pOffsetTransiFound, int * pStepsBaseY, int * pTransitionsBaseY, int * pLeftX, int * pRightX ){ int OffsetTransiFound = -1; int StepsBaseY = -1; int TransitionsBaseY = -1; int Ele1X,Ele2X; int LeftX,RightX; if ( !ForManySteps && (TypeEle1==ELE_SEQ_STEP || TypeEle2==ELE_SEQ_STEP ) ) { ShowMessageBox("Error","Not selected first and last transitions to be joined !!??","Ok"); return FALSE; } if ( TypeEle1==ELE_SEQ_STEP ) { Ele1X = EditSeqDatas.Step[ OffEle1 ].PosiX; StepsBaseY = EditSeqDatas.Step[ OffEle1 ].PosiY; } else if ( TypeEle1==ELE_SEQ_TRANSITION ) { OffsetTransiFound = OffEle1; Ele1X = EditSeqDatas.Transition[ OffEle1 ].PosiX; TransitionsBaseY = EditSeqDatas.Transition[ OffEle1 ].PosiY; } else { ShowMessageBox("Error","Unknown element type for Ele1","Ok"); return FALSE; } if ( TypeEle2==ELE_SEQ_STEP ) { Ele2X = EditSeqDatas.Step[ OffEle2 ].PosiX; if ( StepsBaseY==-1 ) { StepsBaseY = EditSeqDatas.Step[ OffEle1 ].PosiY; } else { if ( StepsBaseY!=EditSeqDatas.Step[ OffEle1 ].PosiY ) { ShowMessageBox("Error","First and last steps selected are not on the same line !!??","Ok"); return FALSE; } } } else if ( TypeEle2==ELE_SEQ_TRANSITION ) { OffsetTransiFound = OffEle2; Ele2X = EditSeqDatas.Transition[ OffEle2 ].PosiX; if ( TransitionsBaseY==-1 ) { TransitionsBaseY = EditSeqDatas.Transition[ OffEle1 ].PosiY; } else { if ( TransitionsBaseY!=EditSeqDatas.Transition[ OffEle1 ].PosiY ) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?