/* \file misc.c * Main routine and initialization for the application */ /* XTrkCad - Model Railroad CAD * Copyright (C) 2005 Dave Bullis * * This program 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 2 of the License, or * (at your option) any later version. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #ifndef WINDOWS #include #include #endif #ifdef HAVE_MALLOC_H #include #endif #include #include #include #include #ifdef WINDOWS #include #include #include "getopt.h" #define R_OK (02) #define access _access #if _MSC_VER >1300 #define strdup _strdup #endif #else #include #endif #include #include #include "track.h" #include "common.h" #include "utility.h" #include "draw.h" #include "misc.h" #include "cjoin.h" #include "compound.h" #include "smalldlg.h" #include "i18n.h" #include #define DEFAULT_SCALE ("N") char *userLocale = NULL; extern wBalloonHelp_t balloonHelp[]; #ifdef DEBUG #define CHECK_BALLOONHELP /*#define CHECK_UNUSED_BALLOONHELP*/ #endif #ifdef CHECK_UNUSED_BALLOONHELP static void ShowUnusedBalloonHelp(void); #endif void DoCarDlg(void); /**************************************************************************** * EXPORTED VARIABLES * */ EXPORT int foobar = 0; EXPORT int log_error; static int log_command; EXPORT wWin_p mainW; EXPORT wIndex_t changed = 0; EXPORT char FAR message[STR_LONG_SIZE]; static char message2[STR_LONG_SIZE]; EXPORT REGION_T curRegion = 0; EXPORT long paramVersion = -1; EXPORT coOrd zero = { 0.0, 0.0 }; EXPORT wBool_t extraButtons = FALSE; EXPORT long onStartup; /**< controls behaviour after startup: load last layout if zero, else start with blank canvas */ EXPORT wButton_p undoB; EXPORT wButton_p redoB; EXPORT wButton_p zoomUpB; EXPORT wButton_p zoomDownB; EXPORT wIndex_t checkPtMark = 0; EXPORT wMenu_p demoM; EXPORT wMenu_p popup1M, popup2M; EXPORT wMenu_p popup1aM, popup2aM; static wIndex_t curCommand = 0; EXPORT void * commandContext; EXPORT wIndex_t cmdGroup; EXPORT wIndex_t joinCmdInx; EXPORT wIndex_t modifyCmdInx; EXPORT long rightClickMode = 0; EXPORT DIST_T easementVal = 0.0; EXPORT DIST_T easeR = 0.0; EXPORT DIST_T easeL = 0.0; EXPORT coOrd cmdMenuPos; EXPORT wPos_t DlgSepLeft = 12; EXPORT wPos_t DlgSepMid = 18; EXPORT wPos_t DlgSepRight = 12; EXPORT wPos_t DlgSepTop = 12; EXPORT wPos_t DlgSepBottom = 12; EXPORT wPos_t DlgSepNarrow = 6; EXPORT wPos_t DlgSepWide = 12; EXPORT wPos_t DlgSepFrmLeft = 4; EXPORT wPos_t DlgSepFrmRight = 4; EXPORT wPos_t DlgSepFrmTop = 4; EXPORT wPos_t DlgSepFrmBottom = 4; static int verbose = 0; static wMenuList_p winList_mi; static BOOL_T inMainW = TRUE; static long stickySet; static long stickyCnt = 0; static char * stickyLabels[33]; #define TOOLBARSET_INIT (0xFFFF) EXPORT long toolbarSet = TOOLBARSET_INIT; EXPORT wPos_t toolbarHeight = 0; static wPos_t toolbarWidth = 0; static wMenuList_p messageList_ml; static BOOL_T messageListEmpty = TRUE; #define MESSAGE_LIST_EMPTY N_("No Messages") #define NUM_FILELIST (5) extern long curTurnoutEp; static wIndex_t printCmdInx; static wIndex_t gridCmdInx; static paramData_t menuPLs[101] = { { PD_LONG, &toolbarSet, "toolbarset" }, { PD_LONG, &curTurnoutEp, "cur-turnout-ep" } }; static paramGroup_t menuPG = { "misc", PGO_RECORD, menuPLs, 2 }; /**************************************************************************** * * LOCAL UTILITIES * */ EXPORT long totalMallocs = 0; EXPORT long totalMalloced = 0; EXPORT long totalRealloced = 0; EXPORT long totalReallocs = 0; EXPORT long totalFreeed = 0; EXPORT long totalFrees = 0; static unsigned long guard0 = 0xDEADBEEF; static unsigned long guard1 = 0xAF00BA8A; static int log_malloc; EXPORT void * MyMalloc ( long size ) { void * p; totalMallocs++; totalMalloced += size; #if defined(WINDOWS) && ! defined(WIN32) if ( size > 65500L ) { AbortProg( "mallocing > 65500 bytes" ); } #endif p = malloc( (size_t)size + sizeof (size_t) + 2 * sizeof (unsigned long) ); if (p == NULL) AbortProg( "No memory" ); LOG1( log_malloc, ( "Malloc(%ld) = %lx (%lx-%lx)\n", size, (long)((char*)p+sizeof (size_t) + sizeof (unsigned long)), (long)p, (long)((char*)p+size+sizeof (size_t) + 2 * sizeof(unsigned long)) )); *(size_t*)p = (size_t)size; p = (char*)p + sizeof (size_t); *(unsigned long*)p = guard0; p = (char*)p + sizeof (unsigned long); *(unsigned long*)((char*)p+size) = guard1; memset( p, 0, (size_t)size ); return p; } EXPORT void * MyRealloc( void * old, long size ) { size_t oldSize; void * new; if (old==NULL) return MyMalloc( size ); totalReallocs++; totalRealloced += size; #if defined(WINDOWS) && ! defined(WIN32) if ( size > 65500L ) { AbortProg( "reallocing > 65500 bytes" ); } #endif if ( *(unsigned long*)((char*)old - sizeof (unsigned long)) != guard0 ) { AbortProg( "Guard0 is hosed" ); } oldSize = *(size_t*)((char*)old - sizeof (unsigned long) - sizeof (size_t)); if ( *(unsigned long*)((char*)old + oldSize) != guard1 ) { AbortProg( "Guard1 is hosed" ); } LOG1( log_malloc, ("Realloc(%lx,%ld) was %d\n", (long)old, size, oldSize ) ) if ((long)oldSize == size) { return old; } if (size == 0) { free( (char*)old - sizeof *(long*)0 - sizeof *(size_t*)0 ); return NULL; } new = MyMalloc( size ); if (new == NULL && size) AbortProg( "No memory" ); memcpy( new, old, min((size_t)size, oldSize) ); MyFree(old); return new; } EXPORT void MyFree( void * ptr ) { size_t oldSize; totalFrees++; if (ptr) { if ( *(unsigned long*)((char*)ptr - sizeof (unsigned long)) != guard0 ) { AbortProg( "Guard0 is hosed" ); } oldSize = *(size_t*)((char*)ptr - sizeof (unsigned long) - sizeof (size_t)); if ( *(unsigned long*)((char*)ptr + oldSize) != guard1 ) { AbortProg( "Guard1 is hosed" ); } LOG1( log_malloc, ("Free %d at %lx (%lx-%lx)\n", oldSize, (long)ptr, (long)((char*)ptr-sizeof *(size_t*)0-sizeof *(long*)0), (long)((char*)ptr+oldSize+sizeof *(long*)0)) ) totalFreeed += oldSize; free( (char*)ptr - sizeof *(long*)0 - sizeof *(size_t*)0 ); } } EXPORT void * memdup( void * src, size_t size ) { void * p; p = MyMalloc( size ); if (p == NULL) AbortProg( "No memory" ); memcpy( p, src, size ); return p; } EXPORT char * MyStrdup( const char * str ) { char * ret; ret = (char*)MyMalloc( strlen( str ) + 1 ); strcpy( ret, str ); return ret; } EXPORT void AbortProg( char * msg, ... ) { static BOOL_T abort2 = FALSE; int rc; va_list ap; va_start( ap, msg ); vsprintf( message, msg, ap ); va_end( ap ); if (abort2) { wNoticeEx( NT_ERROR, message, _("ABORT"), NULL ); } else { strcat( message, _("\nDo you want to save your layout?") ); rc = wNoticeEx( NT_ERROR, message, _("Ok"), _("ABORT") ); if (rc) { DoSaveAs( (doSaveCallBack_p)abort ); } else { abort(); } } } EXPORT char * Strcpytrimed( char * dst, char * src, BOOL_T double_quotes ) { char * cp; while (*src && isspace((unsigned char)*src) ) src++; if (!*src) return dst; cp = src+strlen(src)-1; while ( cp>src && isspace((unsigned char)*cp) ) cp--; while ( src<=cp ) { if (*src == '"' && double_quotes) *dst++ = '"'; *dst++ = *src++; } *dst = '\0'; return dst; } EXPORT char * BuildTrimedTitle( char * cp, char * sep, char * mfg, char * desc, char * partno ) { cp = Strcpytrimed( cp, mfg, FALSE ); strcpy( cp, sep ); cp += strlen(cp); cp = Strcpytrimed( cp, desc, FALSE ); strcpy( cp, sep ); cp += strlen(cp); cp = Strcpytrimed( cp, partno, FALSE ); return cp; } static void ShowMessageHelp( int index, const char * label, void * data ) { char msgKey[STR_SIZE], *cp, *msgSrc; msgSrc = (char*)data; if (!msgSrc) return; cp = strchr( msgSrc, '\t' ); if (cp==NULL) { sprintf( msgKey, _("No help for %s"), msgSrc ); wNoticeEx( NT_INFORMATION, msgKey, _("Ok"), NULL ); return; } memcpy( msgKey, msgSrc, cp-msgSrc ); msgKey[cp-msgSrc] = 0; wHelp( msgKey ); } static char * ParseMessage( char *msgSrc ) { char *cp1=NULL, *cp2=NULL; static char shortMsg[STR_SIZE]; cp1 = strchr( _(msgSrc), '\t' ); if (cp1) { cp2 = strchr( cp1+1, '\t' ); if (cp2) { cp1++; memcpy( shortMsg, cp1, cp2-cp1 ); shortMsg[cp2-cp1] = 0; cp1 = shortMsg; cp2++; } else { cp1++; cp2 = cp1; } if (messageListEmpty) { wMenuListDelete( messageList_ml, _(MESSAGE_LIST_EMPTY) ); messageListEmpty = FALSE; } wMenuListAdd( messageList_ml, 0, cp1, _(msgSrc) ); return cp2; } else { return _(msgSrc); } } EXPORT void InfoMessage( char * format, ... ) { va_list ap; va_start( ap, format ); format = ParseMessage( format ); vsprintf( message2, format, ap ); va_end( ap ); /*InfoSubstituteControl( NULL, NULL );*/ if (inError) return; SetMessage( message2 ); } EXPORT void ErrorMessage( char * format, ... ) { va_list ap; va_start( ap, format ); format = ParseMessage( format ); vsprintf( message2, format, ap ); va_end( ap ); InfoSubstituteControls( NULL, NULL ); SetMessage( message2 ); wBeep(); inError = TRUE; } EXPORT int NoticeMessage( char * format, char * yes, char * no, ... ) { va_list ap; va_start( ap, no ); format = ParseMessage( format ); vsprintf( message2, format, ap ); va_end( ap ); return wNotice( message2, yes, no ); } EXPORT int NoticeMessage2( int playbackRC, char * format, char * yes, char * no, ... ) { va_list ap; if ( inPlayback ) return playbackRC; va_start( ap, no ); format = ParseMessage( format ); vsprintf( message2, format, ap ); va_end( ap ); return wNoticeEx( NT_INFORMATION, message2, yes, no ); } /***************************************************************************** * * MAIN BUTTON HANDLERS * */ EXPORT void Confirm( char * label2, doSaveCallBack_p after ) { int rc; if (changed) { rc = wNotice3( _("Save changes to the layout design before closing?\n\n" "If you don't save now, your unsaved changes will be discarded."), _("&Save"), _("&Cancel"), _("&Don't Save") ); if (rc == 1) { DoSave( after ); return; } else if (rc == 0) { return; } } after(); return; } static void ChkLoad( void ) { Confirm(_("Load"), DoLoad); } static void ChkRevert( void ) { int rc; if( changed) { rc = wNoticeEx( NT_WARNING, _("Do you want to return to the last saved state?\n\n" "Revert will cause all changes done since last save to be lost."), _("&Revert"), _("&Cancel") ); if( rc ) { /* load the file */ LoadTracks( 1, &curFileName, NULL ); } } } static char * fileListPathName; static void AfterFileList( void ) { DoFileList( 0, NULL, fileListPathName ); } static void ChkFileList( int index, const char * label, void * data ) { fileListPathName = (char*)data; Confirm( _("Load"), AfterFileList ); } /** * Save information about current files and some settings to preferences file. */ EXPORT void SaveState( void ) { wPos_t width, height; const char * fileName; void * pathName; char file[6]; int inx; wWinGetSize( mainW, &width, &height ); wPrefSetInteger( "draw", "mainwidth", width ); wPrefSetInteger( "draw", "mainheight", height ); RememberParamFiles(); ParamUpdatePrefs(); wPrefSetString( "misc", "lastlayout", curPathName ); if ( fileList_ml ) { strcpy( file, "file" ); file[5] = 0; for ( inx=0; inx= demoWindows_da.cnt ) { for ( inx=0; inx= demoWindows_da.cnt ) { DYNARR_APPEND( wWin_p, demoWindows_da, 10 ); inx = demoWindows_da.cnt-1; } demoWindows(inx) = win; } } if (win != mainW) wMenuListAdd( winList_mi, -1, wWinGetTitle(win), win ); wWinShow( win, TRUE ); } EXPORT void wHide( wWin_p win ) { int inx; wWinShow( win, FALSE ); wWinSetBusy( win, FALSE ); if ( inMainW && win == aboutW ) return; wMenuListDelete( winList_mi, wWinGetTitle(win) ); if ( inPlayback ) for ( inx=0; inxname; bh++ ); balloonHelpCnts = (int*)malloc( (sizeof *(int*)0) * (bh-balloonHelp) ); memset( balloonHelpCnts, 0, (sizeof *(int*)0) * (bh-balloonHelp) ); } #endif for ( bh=balloonHelp; bh->name; bh++ ) { if ( strcmp( bh->name, helpKey ) == 0 ) { #ifdef CHECK_UNUSED_BALLOONHELP balloonHelpCnts[(bh-balloonHelp)]++; #endif return _(bh->value); } } #ifdef CHECK_BALLOONHELP fprintf( stderr, _("No balloon help for %s\n"), helpKey ); #endif return _("No Help"); } #ifdef CHECK_UNUSED_BALLOONHELP static void ShowUnusedBalloonHelp( void ) { int cnt; for ( cnt=0; balloonHelp[cnt].name; cnt++ ) if ( balloonHelpCnts[cnt] == 0 ) fprintf( stderr, "unused BH %s\n", balloonHelp[cnt].name ); } #endif EXPORT void EnableCommands( void ) { int inx, minx; wBool_t enable; LOG( log_command, 5, ( "COMMAND enable S%d M%d\n", selectedTrackCount, programMode ) ) for ( inx=0; inx= 0 ) wControlActive( buttonList[commandList[inx].buttInx].control, enable ); for ( minx=0; minx0 ); } } EXPORT void Reset( void ) { if (recordF) { fprintf( recordF, "RESET\n" ); fflush( recordF ); } LOG( log_command, 2, ( "COMMAND CANCEL %s\n", commandList[curCommand].helpKey ) ) commandList[curCommand].cmdProc( C_CANCEL, zero ); if ( commandList[curCommand].buttInx>=0 ) wButtonSetBusy( (wButton_p)buttonList[commandList[curCommand].buttInx].control, FALSE ); curCommand = (preSelect?selectCmdInx:describeCmdInx); commandContext = commandList[curCommand].context; if ( commandList[curCommand].buttInx >= 0 ) wButtonSetBusy( (wButton_p)buttonList[commandList[curCommand].buttInx].control, TRUE ); tempSegs_da.cnt = 0; if (checkPtInterval > 0 && changed >= checkPtMark+(wIndex_t)checkPtInterval && !inPlayback ) { DoCheckPoint(); checkPtMark = changed; } MainRedraw(); EnableCommands(); ResetMouseState(); LOG( log_command, 1, ( "COMMAND RESET %s\n", commandList[curCommand].helpKey ) ) (void)commandList[curCommand].cmdProc( C_START, zero ); } static BOOL_T CheckClick( wAction_t *action, coOrd *pos, BOOL_T checkLeft, BOOL_T checkRight ) { static long time0; static coOrd pos0; long time1; long timeDelta; DIST_T distDelta; switch (*action) { case C_DOWN: if (!checkLeft) return TRUE; time0 = wGetTimer() - adjTimer; pos0 = *pos; return FALSE; case C_MOVE: if (!checkLeft) return TRUE; if (time0 != 0) { time1 = wGetTimer() - adjTimer; timeDelta = time1 - time0; distDelta = FindDistance( *pos, pos0 ); if ( timeDelta > dragTimeout || distDelta > dragDistance ) { time0 = 0; *pos = pos0; *action = C_DOWN; } else { return FALSE; } } break; case C_UP: if (!checkLeft) return TRUE; if (time0 != 0) { time1 = wGetTimer() - adjTimer; timeDelta = time1 - time0; distDelta = FindDistance( *pos, pos0 ); time0 = 0; *action = C_LCLICK; } break; case C_RDOWN: if (!checkRight) return TRUE; time0 = wGetTimer() - adjTimer; pos0 = *pos; return FALSE; case C_RMOVE: if (!checkRight) return TRUE; if (time0 != 0) { time1 = wGetTimer() - adjTimer; timeDelta = time1 - time0; distDelta = FindDistance( *pos, pos0 ); if ( timeDelta > dragTimeout || distDelta > dragDistance ) { time0 = 0; *pos = pos0; *action = C_RDOWN; } else { return FALSE; } } break; case C_RUP: if (!checkRight) return TRUE; if (time0 != 0) { time0 = 0; *action = C_RCLICK; } break; } return TRUE; } EXPORT wBool_t DoCurCommand( wAction_t action, coOrd pos ) { wAction_t rc; int mode; if ( action == wActionMove && (commandList[curCommand].options & IC_WANT_MOVE) == 0 ) return C_CONTINUE; if ( !CheckClick( &action, &pos, (int)(commandList[curCommand].options & IC_LCLICK), TRUE ) ) return C_CONTINUE; if ( action == C_RCLICK && (commandList[curCommand].options&IC_RCLICK)==0 ) { if ( !inPlayback ) { mode = MyGetKeyState(); if ( ( mode & (~WKEY_SHIFT) ) != 0 ) { wBeep(); return C_CONTINUE; } if ( ((mode&WKEY_SHIFT) == 0) == (rightClickMode==0) ) { if ( selectedTrackCount > 0 ) { if (commandList[curCommand].options & IC_CMDMENU) { } wMenuPopupShow( popup2M ); } else { wMenuPopupShow( popup1M ); } return C_CONTINUE; } else if ( (commandList[curCommand].options & IC_CMDMENU) ) { cmdMenuPos = pos; action = C_CMDMENU; } else { wBeep(); return C_CONTINUE; } } else { return C_CONTINUE; } } LOG( log_command, 2, ( "COMMAND MOUSE %s %d @ %0.3f %0.3f\n", commandList[curCommand].helpKey, (int)action, pos.x, pos.y ) ) rc = commandList[curCommand].cmdProc( action, pos ); LOG( log_command, 4, ( " COMMAND returns %d\n", rc ) ) if ( (rc == C_TERMINATE || rc == C_INFO) && (commandList[curCommand].options & IC_STICKY) && (commandList[curCommand].stickyMask & stickySet) ) { tempSegs_da.cnt = 0; UpdateAllElevations(); MainRedraw(); if (commandList[curCommand].options & IC_NORESTART) { return C_CONTINUE; } LOG( log_command, 1, ( "COMMAND START %s\n", commandList[curCommand].helpKey ) ) rc = commandList[curCommand].cmdProc( C_START, pos ); LOG( log_command, 4, ( " COMMAND returns %d\n", rc ) ) switch( rc ) { case C_CONTINUE: break; case C_ERROR: Reset(); #ifdef VERBOSE lprintf( "Start returns Error"); #endif break; case C_TERMINATE: InfoMessage( "" ); case C_INFO: Reset(); break; } } return rc; } EXPORT void ConfirmReset( BOOL_T retry ) { wAction_t rc; if (curCommand != describeCmdInx && curCommand != selectCmdInx ) { LOG( log_command, 3, ( "COMMAND CONFIRM %s\n", commandList[curCommand].helpKey ) ) rc = commandList[curCommand].cmdProc( C_CONFIRM, zero ); LOG( log_command, 4, ( " COMMAND returns %d\n", rc ) ) if ( rc == C_ERROR ) { if (retry) rc = wNotice3( _("Cancelling the current command will undo the changes\n" "you are currently making. Do you want to update?"), _("Yes"), _("No"), _("Cancel") ); else rc = wNoticeEx( NT_WARNING, _("Cancelling the current command will undo the changes\n" "you are currently making. Do you want to update?"), _("Yes"), _("No") ); if (rc == 1) { LOG( log_command, 3, ( "COMMAND OK %s\n", commandList[curCommand].helpKey ) ) commandList[curCommand].cmdProc( C_OK, zero ); return; } else if (rc == -1) { return; } } else if ( rc == C_TERMINATE ) { return; } } Reset(); if (retry) { /* because user pressed esc */ SetAllTrackSelect( FALSE ); } LOG( log_command, 1, ( "COMMAND RESET %s\n", commandList[curCommand].helpKey ) ) commandList[curCommand].cmdProc( C_START, zero ); } EXPORT void ResetIfNotSticky( void ) { if ( (commandList[curCommand].options & IC_STICKY) == 0 || (commandList[curCommand].stickyMask & stickySet) == 0 ) Reset(); } EXPORT void DoCommandB( void * data ) { wIndex_t inx = (wIndex_t)(long)data; STATUS_T rc; static coOrd pos = {0,0}; static int inDoCommandB = FALSE; wIndex_t buttInx; if (inDoCommandB) return; inDoCommandB = TRUE; if (inx < 0 || inx >= commandCnt) { ASSERT( FALSE ); inDoCommandB = FALSE; return; } if ( (!inPlayback) && (!commandList[inx].enabled) ) { ErrorMessage( MSG_COMMAND_DISABLED ); inx = describeCmdInx; } InfoMessage( "" ); if (curCommand != selectCmdInx ) { LOG( log_command, 3, ( "COMMAND FINISH %s\n", commandList[curCommand].helpKey ) ) rc = commandList[curCommand].cmdProc( C_FINISH, zero ); LOG( log_command, 3, ( "COMMAND CONFIRM %s\n", commandList[curCommand].helpKey ) ) rc = commandList[curCommand].cmdProc( C_CONFIRM, zero ); LOG( log_command, 4, ( " COMMAND returns %d\n", rc ) ) if ( rc == C_ERROR ) { rc = wNotice3( _("Cancelling the current command will undo the changes\n" "you are currently making. Do you want to update?"), _("Yes"), _("No"), _("Cancel") ); if (rc == 1) commandList[curCommand].cmdProc( C_OK, zero ); else if (rc == -1) { inDoCommandB = FALSE; return; } } LOG( log_command, 3, ( "COMMAND CANCEL %s\n", commandList[curCommand].helpKey ) ) commandList[curCommand].cmdProc( C_CANCEL, pos ); tempSegs_da.cnt = 0; } if (commandList[curCommand].buttInx>=0) wButtonSetBusy( (wButton_p)buttonList[commandList[curCommand].buttInx].control, FALSE ); if (recordF) { fprintf( recordF, "COMMAND %s\n", commandList[inx].helpKey+3 ); fflush( recordF ); } curCommand = inx; commandContext = commandList[curCommand].context; if ( (buttInx=commandList[curCommand].buttInx) >= 0 ) { if ( buttonList[buttInx].cmdInx != curCommand ) { wButtonSetLabel( (wButton_p)buttonList[buttInx].control, (char*)commandList[curCommand].icon ); wControlSetHelp( buttonList[buttInx].control, GetBalloonHelpStr(commandList[curCommand].helpKey) ); wControlSetContext( buttonList[buttInx].control, (void*)(intptr_t)curCommand ); buttonList[buttInx].cmdInx = curCommand; } wButtonSetBusy( (wButton_p)buttonList[commandList[curCommand].buttInx].control, TRUE ); } LOG( log_command, 1, ( "COMMAND START %s\n", commandList[curCommand].helpKey ) ) rc = commandList[curCommand].cmdProc( C_START, pos ); LOG( log_command, 4, ( " COMMAND returns %d\n", rc ) ) switch( rc ) { case C_CONTINUE: break; case C_ERROR: Reset(); #ifdef VERBOSE lprintf( "Start returns Error"); #endif break; case C_TERMINATE: case C_INFO: if (rc == C_TERMINATE) InfoMessage( "" ); Reset(); break; } inDoCommandB = FALSE; } static void DoCommandBIndirect( void * cmdInxP ) { wIndex_t cmdInx; cmdInx = *(wIndex_t*)cmdInxP; DoCommandB( (void*)(intptr_t)cmdInx ); } EXPORT void LayoutSetPos( wIndex_t inx ) { wPos_t w, h; static wPos_t toolbarRowHeight = 0; static wPos_t width; static int lastGroup; static wPos_t gap; static int layerButtCnt; int currGroup; if ( inx == 0 ) { lastGroup = 0; wWinGetSize( mainW, &width, &h ); gap = 5; toolbarWidth = width+5; layerButtCnt = 0; toolbarHeight = 0; } if (buttonList[inx].control) { if ( toolbarRowHeight <= 0 ) toolbarRowHeight = wControlGetHeight( buttonList[inx].control ); currGroup = buttonList[inx].group & ~BG_BIGGAP; if ( currGroup != lastGroup && (buttonList[inx].group&BG_BIGGAP) ) { gap = 15; } if ((toolbarSet & (1<width) { toolbarWidth = 0; toolbarHeight += h + 5; } wControlSetPos( buttonList[inx].control, toolbarWidth, toolbarHeight-(h+5) ); buttonList[inx].x = toolbarWidth; buttonList[inx].y = toolbarHeight-(h+5); toolbarWidth += wControlGetWidth( buttonList[inx].control ); wControlShow( buttonList[inx].control, TRUE ); } else { wControlShow( buttonList[inx].control, FALSE ); } } } EXPORT void LayoutToolBar( void ) { int inx; for (inx = 0; inx= COMMAND_MAX-1) { AbortProg("addCommand: too many commands" ); } commandList[commandCnt].labelStr = MyStrdup(nameStr); commandList[commandCnt].helpKey = MyStrdup(helpKey); commandList[commandCnt].cmdProc = cmdProc; commandList[commandCnt].icon = icon; commandList[commandCnt].reqLevel = reqLevel; commandList[commandCnt].enabled = TRUE; commandList[commandCnt].options = options; commandList[commandCnt].acclKey = acclKey; commandList[commandCnt].context = context; commandList[commandCnt].buttInx = -1; commandList[commandCnt].menu[0] = NULL; commandList[commandCnt].menu[1] = NULL; commandList[commandCnt].menu[2] = NULL; commandList[commandCnt].menu[3] = NULL; commandCnt++; return commandCnt-1; } EXPORT void AddToolbarControl( wControl_p control, long options ) { if (buttonCnt >= COMMAND_MAX-1) { AbortProg("addToolbarControl: too many buttons" ); } buttonList[buttonCnt].enabled = TRUE; buttonList[buttonCnt].options = options; buttonList[buttonCnt].group = cmdGroup; buttonList[buttonCnt].x = 0; buttonList[buttonCnt].y = 0; buttonList[buttonCnt].control = control; buttonList[buttonCnt].cmdInx = -1; LayoutSetPos( buttonCnt ); buttonCnt++; } EXPORT wButton_p AddToolbarButton( char * helpStr, wIcon_p icon, long options, wButtonCallBack_p action, void * context ) { wButton_p bb; wIndex_t inx; GetBalloonHelpStr(helpStr); if ( context == NULL ) { for ( inx=0; inx= buttonCnt ) return; if ( buttonList[buttInx].control == NULL ) return; cmdX = buttonList[buttInx].x+17; cmdY = toolbarHeight - (buttonList[buttInx].y+17) + (wPos_t)(mainD.size.y/mainD.scale*mainD.dpi) + 30; MovePlaybackCursor( &mainD, cmdX, cmdY ); if ( playbackTimer == 0 ) { wButtonSetBusy( (wButton_p)buttonList[buttInx].control, TRUE ); wFlush(); wPause( 500 ); wButtonSetBusy( (wButton_p)buttonList[buttInx].control, FALSE ); wFlush(); } } #include "bitmaps/openbutt.xpm" static char * buttonGroupMenuTitle; static char * buttonGroupHelpKey; static char * buttonGroupStickyLabel; static wMenu_p buttonGroupPopupM; EXPORT void ButtonGroupBegin( char * menuTitle, char * helpKey, char * stickyLabel ) { buttonGroupMenuTitle = menuTitle; buttonGroupHelpKey = helpKey; buttonGroupStickyLabel = stickyLabel; buttonGroupPopupM = NULL; } EXPORT void ButtonGroupEnd( void ) { buttonGroupMenuTitle = NULL; buttonGroupHelpKey = NULL; buttonGroupPopupM = NULL; } #ifdef LATER EXPORT wIndex_t AddCommandControl( procCommand_t command, char * helpKey, char * nameStr, wControl_p control, int reqLevel, long options, long acclKey, void * context ) { wIndex_t buttInx = -1; wIndex_t cmdInx; BOOL_T newButtonGroup = FALSE; wMenu_p tm, p1m, p2m; static wIcon_p openbuttIcon = NULL; static wMenu_p commandsSubmenu; static wMenu_p popup1Submenu; static wMenu_p popup2Submenu; AddToolbarControl( control, options ); buttonList[buttInx].cmdInx = commandCnt; cmdInx = AddCommand( command, helpKey, nameStr, NULL, reqLevel, options, acclKey, context ); commandList[cmdInx].buttInx = buttInx; if (nameStr[0] == '\0') return cmdInx; if (commandList[cmdInx].options&IC_STICKY) { if ( buttonGroupPopupM==NULL || newButtonGroup ) { if ( stickyCnt > 32 ) AbortProg( "stickyCnt>32" ); stickyCnt++; } if ( buttonGroupPopupM==NULL) { stickyLabels[stickyCnt-1] = nameStr; } else { stickyLabels[stickyCnt-1] = buttonGroupStickyLabel; } stickyLabels[stickyCnt] = NULL; commandList[cmdInx].stickyMask = 1L<<(stickyCnt-1); } if ( buttonGroupPopupM ) { commandList[cmdInx].menu[0] = wMenuPushCreate( buttonGroupPopupM, helpKey, GetBalloonHelpStr(helpKey), 0, DoCommandB, (void*)cmdInx ); tm = commandsSubmenu; p1m = popup1Submenu; p2m = popup2Submenu; } else { tm = commandsM; p1m = (options&IC_POPUP2)?popup1aM:popup1M; p2m = (options&IC_POPUP2)?popup2aM:popup2M; } commandList[cmdInx].menu[1] = wMenuPushCreate( tm, helpKey, nameStr, acclKey, DoCommandB, (void*)cmdInx ); if ( (options & (IC_POPUP|IC_POPUP2)) ) { if ( !(options & IC_SELECTED) ) { commandList[cmdInx].menu[2] = wMenuPushCreate( p1m, helpKey, nameStr, 0, DoCommandB, (void*)cmdInx ); } commandList[cmdInx].menu[3] = wMenuPushCreate( p2m, helpKey, nameStr, 0, DoCommandB, (void*)cmdInx ); } return cmdInx; } #endif EXPORT wIndex_t AddMenuButton( wMenu_p menu, procCommand_t command, char * helpKey, char * nameStr, wIcon_p icon, int reqLevel, long options, long acclKey, void * context ) { wIndex_t buttInx = -1; wIndex_t cmdInx; BOOL_T newButtonGroup = FALSE; wMenu_p tm, p1m, p2m; static wIcon_p openbuttIcon = NULL; static wMenu_p commandsSubmenu; static wMenu_p popup1Submenu; static wMenu_p popup2Submenu; if ( icon ) { if ( buttonGroupPopupM!=NULL ) { buttInx = buttonCnt-2; } else { buttInx = buttonCnt; AddToolbarButton( helpKey, icon, options, (wButtonCallBack_p)DoCommandB, (void*)(intptr_t)commandCnt ); buttonList[buttInx].cmdInx = commandCnt; } if ( buttonGroupMenuTitle!=NULL && buttonGroupPopupM==NULL ) { if ( openbuttIcon == NULL ) openbuttIcon = wIconCreatePixMap(openbutt_xpm); buttonGroupPopupM = wMenuPopupCreate( mainW, buttonGroupMenuTitle ); AddToolbarButton( buttonGroupHelpKey, openbuttIcon, IC_ABUT, (wButtonCallBack_p)wMenuPopupShow, (void*)buttonGroupPopupM ); newButtonGroup = TRUE; commandsSubmenu = wMenuMenuCreate( menu, "", buttonGroupMenuTitle ); popup1Submenu = wMenuMenuCreate( ((options&IC_POPUP2)?popup1aM:popup1M), "", buttonGroupMenuTitle ); popup2Submenu = wMenuMenuCreate( ((options&IC_POPUP2)?popup2aM:popup2M), "", buttonGroupMenuTitle ); } } cmdInx = AddCommand( command, helpKey, nameStr, icon, reqLevel, options, acclKey, context ); commandList[cmdInx].buttInx = buttInx; if (nameStr[0] == '\0') return cmdInx; if (commandList[cmdInx].options&IC_STICKY) { if ( buttonGroupPopupM==NULL || newButtonGroup ) { if ( stickyCnt > 32 ) AbortProg( "stickyCnt>32" ); stickyCnt++; } if ( buttonGroupPopupM==NULL) { stickyLabels[stickyCnt-1] = nameStr; } else { stickyLabels[stickyCnt-1] = buttonGroupStickyLabel; } stickyLabels[stickyCnt] = NULL; commandList[cmdInx].stickyMask = 1L<<(stickyCnt-1); } if ( buttonGroupPopupM ) { commandList[cmdInx].menu[0] = wMenuPushCreate( buttonGroupPopupM, helpKey, GetBalloonHelpStr(helpKey), 0, DoCommandB, (void*)(intptr_t)cmdInx ); tm = commandsSubmenu; p1m = popup1Submenu; p2m = popup2Submenu; } else { tm = menu; p1m = (options&IC_POPUP2)?popup1aM:popup1M; p2m = (options&IC_POPUP2)?popup2aM:popup2M; } commandList[cmdInx].menu[1] = wMenuPushCreate( tm, helpKey, nameStr, acclKey, DoCommandB, (void*)(intptr_t)cmdInx ); if ( (options & (IC_POPUP|IC_POPUP2)) ) { if ( !(options & IC_SELECTED) ) { commandList[cmdInx].menu[2] = wMenuPushCreate( p1m, helpKey, nameStr, 0, DoCommandB, (void*)(intptr_t)cmdInx ); } commandList[cmdInx].menu[3] = wMenuPushCreate( p2m, helpKey, nameStr, 0, DoCommandB, (void*)(intptr_t)cmdInx ); } return cmdInx; } EXPORT wIndex_t InitCommand( wMenu_p menu, procCommand_t command, char * nameStr, char * bits, int reqLevel, long options, long acclKey ) { char helpKey[STR_SHORT_SIZE]; wIcon_p icon = NULL; if (bits) icon = wIconCreateBitMap( 16, 16, bits, wDrawColorBlack ); strcpy( helpKey, "cmd" ); strcat( helpKey, nameStr ); return AddMenuButton( menu, command, helpKey, _(nameStr), icon, reqLevel, options, acclKey, NULL ); } /*--------------------------------------------------------------------*/ EXPORT void PlaybackCommand( char * line, wIndex_t lineNum ) { wIndex_t inx; wIndex_t buttInx; int len1, len2; len1 = strlen(line+8); for (inx=0;inx= commandCnt) { fprintf(stderr, "Unknown playback COMMAND command %d : %s\n", lineNum, line ); } else { wPos_t cmdX, cmdY; if ((buttInx=commandList[inx].buttInx)>=0) { cmdX = buttonList[buttInx].x+17; cmdY = toolbarHeight - (buttonList[buttInx].y+17) + (wPos_t)(mainD.size.y/mainD.scale*mainD.dpi) + 30; MovePlaybackCursor( &mainD, cmdX, cmdY ); } if (strcmp( line+8, "Undo") == 0) { if (buttInx>0 && playbackTimer == 0) { wButtonSetBusy( (wButton_p)buttonList[buttInx].control, TRUE ); wFlush(); wPause( 500 ); wButtonSetBusy( (wButton_p)buttonList[buttInx].control, FALSE ); wFlush(); } UndoUndo(); } else if (strcmp( line+8, "Redo") == 0) { if (buttInx>=0 && playbackTimer == 0) { wButtonSetBusy( (wButton_p)buttonList[buttInx].control, TRUE ); wFlush(); wPause( 500 ); wButtonSetBusy( (wButton_p)buttonList[buttInx].control, FALSE ); wFlush(); } UndoRedo(); } else { if ( buttInx>=0 && playbackTimer == 0 ) { wButtonSetBusy( (wButton_p)buttonList[buttInx].control, TRUE ); wFlush(); wPause( 500 ); wButtonSetBusy( (wButton_p)buttonList[buttInx].control, FALSE ); wFlush(); } DoCommandB( (void*)(intptr_t)inx ); } } } /*--------------------------------------------------------------------*/ typedef struct { char * label; wMenu_p menu; } menuTrace_t, *menuTrace_p; static dynArr_t menuTrace_da; #define menuTrace(N) DYNARR_N( menuTrace_t, menuTrace_da, N ) static void DoMenuTrace( wMenu_p menu, const char * label, void * data ) { /*printf( "MENUTRACE: %s/%s\n", (char*)data, label );*/ if (recordF) { fprintf( recordF, "MOUSE 1 %0.3f %0.3f\n", oldMarker.x, oldMarker.y ); fprintf( recordF, "MENU %0.3f %0.3f \"%s\" \"%s\"\n", oldMarker.x, oldMarker.y, (char*)data, label ); } } EXPORT wMenu_p MenuRegister( char * label ) { wMenu_p m; menuTrace_p mt; m = wMenuPopupCreate( mainW, label ); DYNARR_APPEND( menuTrace_t, menuTrace_da, 10 ); mt = &menuTrace( menuTrace_da.cnt-1 ); mt->label = strdup(label); mt->menu = m; wMenuSetTraceCallBack( m, DoMenuTrace, mt->label ); return m; } void MenuPlayback( char * line ) { char * menuName, * itemName; coOrd pos; wPos_t x, y; menuTrace_p mt; if (!GetArgs( line, "pqq", &pos, &menuName, &itemName )) return; for ( mt=&menuTrace(0); mt<&menuTrace(menuTrace_da.cnt); mt++ ) { if ( strcmp( mt->label, menuName ) == 0 ) { mainD.CoOrd2Pix( &mainD, pos, &x, &y ); MovePlaybackCursor( &mainD, x, y ); oldMarker = cmdMenuPos = pos; wMenuAction( mt->menu, _(itemName) ); return; } } AbortProg( "menuPlayback: %s not found", menuName ); } /*--------------------------------------------------------------------*/ static wWin_p stickyW; static void StickyOk( void * ); static paramData_t stickyPLs[] = { { PD_TOGGLE, &stickySet, "set", 0, stickyLabels } }; static paramGroup_t stickyPG = { "sticky", PGO_RECORD, stickyPLs, sizeof stickyPLs/sizeof stickyPLs[0] }; static void StickyOk( void * junk ) { wHide( stickyW ); } static void DoSticky( void ) { if ( !stickyW ) stickyW = ParamCreateDialog( &stickyPG, MakeWindowTitle(_("Sticky Commands")), _("Ok"), StickyOk, NULL, TRUE, NULL, 0, NULL ); ParamLoadControls( &stickyPG ); wShow( stickyW ); } /*--------------------------------------------------------------------*/ /* * These array control the choices available in the Toolbar setup. * For each choice, the text is given and the respective mask is * specified in the following array. * Note: text and choices must be given in the same order. */ static char *AllToolbarLabels[] = { N_("File Buttons"), N_("Zoom Buttons"), N_("Undo Buttons"), N_("Easement Button"), N_("SnapGrid Buttons"), N_("Create Track Buttons"), N_("Layout Control Elements"), N_("Modify Track Buttons"), N_("Describe/Select"), N_("Track Group Buttons"), N_("Train Group Buttons"), N_("Create Misc Buttons"), N_("Ruler Button"), N_("Layer Buttons"), N_("Hot Bar"), NULL }; static long AllToolbarMasks[] = { 1<= sizeof debugPLs/sizeof debugPLs[0] ) AbortProg( "Too many debug flags" ); memset( &debugPLs[debugCnt], 0, sizeof debugPLs[debugCnt] ); debugPLs[debugCnt].type = PD_LONG; debugPLs[debugCnt].valueP = valueP; debugPLs[debugCnt].nameStr = label; debugPLs[debugCnt].winData = &r0_100; debugPLs[debugCnt].winLabel = label; debugCnt++; } void RecomputeElevations( void ); static void MiscMenuItemCreate( wMenu_p m1, wMenu_p m2, char * name, char * label, long acclKey, void * func, long option, void * context ) { wMenuPush_p mp; mp = wMenuPushCreate( m1, name, label, acclKey, ParamMenuPush, &menuPLs[menuPG.paramCnt] ); if ( m2 ) wMenuPushCreate( m2, name, label, acclKey, ParamMenuPush, &menuPLs[menuPG.paramCnt] ); menuPLs[menuPG.paramCnt].control = (wControl_p)mp; menuPLs[menuPG.paramCnt].type = PD_MENUITEM; menuPLs[menuPG.paramCnt].valueP = func; menuPLs[menuPG.paramCnt].nameStr = name; menuPLs[menuPG.paramCnt].option = option; menuPLs[menuPG.paramCnt].context = context; if ( name ) GetBalloonHelpStr( name ); menuPG.paramCnt++; } static char * accelKeyNames[] = { "Del", "Ins", "Home", "End", "Pgup", "Pgdn", "Up", "Down", "Right", "Left", "Back", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12" }; static void SetAccelKey( char * prefName, wAccelKey_e key, int mode, wAccelKeyCallBack_p func, void * context ) { int mode1 = 0; int inx; const char * prefValue = wPrefGetString( "accelKey", prefName ); if ( prefValue != NULL ) { while ( prefValue[1] == '-' ) { switch ( prefValue[0] ) { case 'S': mode1 |= WKEY_SHIFT; break; case 'C': mode1 |= WKEY_CTRL; break; case 'A': mode1 |= WKEY_ALT; break; default: ; } prefValue += 2; } for ( inx=0; inx=0; inx--) { file[4] = '0'+inx; cp = wPrefGetString( "filelist", file ); if (!cp) continue; pathName = MyStrdup(cp); fileName = strrchr( pathName, FILE_SEP_CHAR[0] ); if (fileName) wMenuListAdd( fileList_ml, 0, fileName+1, pathName ); } } EXPORT void InitCmdEnumerate( void ) { AddToolbarButton( "cmdEnumerate", wIconCreatePixMap(partlist_xpm), IC_SELECTED|IC_ACCLKEY, (addButtonCallBack_t)EnumerateTracks, NULL ); } EXPORT void InitCmdExport( void ) { AddToolbarButton( "cmdExport", wIconCreatePixMap(export_xpm), IC_SELECTED|IC_ACCLKEY, (addButtonCallBack_t)DoExport, NULL ); AddToolbarButton( "cmdImport", wIconCreatePixMap(import_xpm), IC_ACCLKEY, (addButtonCallBack_t)DoImport, NULL ); } /* Give user the option to continue work after crash. This function gives the user * the option to load the checkpoint file to continue working after a crash. * * \param none * \return none * */ static void OfferCheckpoint( void ) { int ret; /* sProdName */ ret = wNoticeEx( NT_INFORMATION, _("Program was not terminated properly. Do you want to resume working on the previous trackplan?"), _("Resume"), _("Ignore") ); if( ret ) { /* load the checkpoint file */ LoadCheckpoint(); } } EXPORT wWin_p wMain( int argc, char * argv[] ) { int c; char * logFileName = NULL; int log_init = 0; int initialZoom = 0; char * initialFile = NULL; const char * pref; coOrd roomSize; long oldToolbarMax; long newToolbarMax; char *cp; char *oldLocale = NULL; char buffer[ STR_SIZE ]; unsigned int i; strcpy( buffer, sProdNameLower ); /* Initialize application name */ wInitAppName(buffer); /* Initialize gettext */ InitGettext(); /* Save user locale */ oldLocale = setlocale(LC_ALL, NULL); if (oldLocale) userLocale = strdup( oldLocale ); /* * ARGUMENTS */ opterr = 0; while ((c = getopt (argc, argv, "vl:d:c:")) != -1) switch (c) { case 'c': /* configuration name */ /* test for valid filename */ for( i = 0; i < strlen( optarg ); i++ ) { if( !isalnum( (unsigned char)optarg[ i ]) && optarg[ i ] != '.' ) { NoticeMessage( MSG_BAD_OPTION, _("Ok"), NULL, optarg ); exit( 1 ); } } /* append delimiter and argument to configuration name */ if( strlen( optarg ) < STR_SIZE - strlen( ";" ) - strlen( buffer ) - 1 ){ strcat( buffer, ";" ); strcat( buffer, optarg ); } else { NoticeMessage( MSG_BAD_OPTION, _("Ok"), NULL, optarg ); exit( 1 ); } break; case 'v': /* verbose flag */ verbose++; break; case 'd': /* define loglevel for a group */ cp = strchr( optarg, '=' ); if ( cp != NULL ) { *cp++ = '\0'; LogSet( optarg, atoi(cp) ); } else { LogSet( optarg, 1 ); } break; case 'l': /* define log filename */ logFileName = strdup(optarg); break; case '?': NoticeMessage( MSG_BAD_OPTION, _("Ok"), NULL, argv[ optind - 1 ] ); exit( 1 ); case ':': NoticeMessage( "Missing parameter for %s", _("Ok"), NULL, argv[ optind - 1 ] ); exit( 1 ); break; default: abort (); } if( optind < argc ) initialFile = strdup( argv[ optind ] ); extraButtons = ( getenv(sEnvExtra) != NULL ); LogOpen( logFileName ); log_init = LogFindIndex( "init" ); log_malloc = LogFindIndex( "malloc" ); log_error = LogFindIndex( "error" ); log_command = LogFindIndex( "command" ); LOG1( log_init, ( "initCustom\n" ) ) InitCustom(); /* * MAIN WINDOW */ LOG1( log_init, ( "create main window\n" ) ) strcpy( Title1, sProdName ); sprintf( message, _("Unnamed Trackplan - %s(%s)"), sProdName, sVersion ); wSetBalloonHelp( balloonHelp ); mainW = wWinMainCreate( buffer, 600, 350, "xtrkcadW", message, "main", F_RESIZE|F_MENUBAR|F_NOTAB|F_RECALLPOS|F_HIDE, MainProc, NULL ); if ( mainW == NULL ) return NULL; drawColorBlack = wDrawFindColor( wRGB( 0, 0, 0) ); drawColorWhite = wDrawFindColor( wRGB(255,255,255) ); drawColorRed = wDrawFindColor( wRGB(255, 0, 0) ); drawColorBlue = wDrawFindColor( wRGB( 0, 0,255) ); drawColorGreen = wDrawFindColor( wRGB( 0,255, 0) ); drawColorAqua = wDrawFindColor( wRGB( 0,255,255) ); drawColorPurple = wDrawFindColor( wRGB(255, 0,255) ); drawColorGold = wDrawFindColor( wRGB(255,215, 0) ); snapGridColor = drawColorGreen; markerColor = drawColorRed; borderColor = drawColorBlack; crossMajorColor = drawColorRed; crossMinorColor = drawColorBlue; selectedColor = drawColorRed; normalColor = drawColorBlack; elevColorIgnore = drawColorBlue; elevColorDefined = drawColorGold; profilePathColor = drawColorPurple; exceptionColor = wDrawFindColor( wRGB(255,0,128) ); tieColor = wDrawFindColor( wRGB(255,128,0) ); newToolbarMax = (1<