summaryrefslogtreecommitdiff
path: root/app/bin/cmodify.c
blob: 8f8201295c111172fc58d402a423a5e7c98ac871 (plain)
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/** \file cmodify.c
 * TRACK MODIFY
 */

/*  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 <math.h>

#include "cjoin.h"
#include "ccurve.h"
#include "cbezier.h"
#include "ccornu.h"
#include "cstraigh.h"
#include "cundo.h"
#include "fileio.h"
#include "i18n.h"
#include "messages.h"
#include "param.h"
#include "track.h"
#include "utility.h"
#include "drawgeom.h"
#include "common.h"
#include "layout.h"

static struct {
		track_p Trk;
		trackParams_t params;
		coOrd pos00, pos00x, pos01;
		ANGLE_T angle;
		curveData_t curveData;
		easementData_t jointD;
		DIST_T r1;
		BOOL_T valid;
		BOOL_T first;
		} Dex;

static wMenu_p modPopupM;

extern wIndex_t selectCmdInx;
extern wIndex_t joinCmdInx;
extern wIndex_t describeCmdInx;

static dynArr_t anchors_da;
#define anchors(N) DYNARR_N(trkSeg_t,anchors_da,N)

static int log_modify;
static BOOL_T modifyBezierMode;
static BOOL_T modifyCornuMode;
static BOOL_T modifyDrawMode;
static BOOL_T modifyRulerMode;
static BOOL_T modifyExtendMode;


static void CreateEndAnchor(coOrd p, wBool_t lock) {
	DIST_T d = tempD.scale*0.15;

	DYNARR_APPEND(trkSeg_t,anchors_da,1);
	int i = anchors_da.cnt-1;
	anchors(i).type = lock?SEG_FILCRCL:SEG_CRVLIN;
	anchors(i).color = wDrawColorBlue;
	anchors(i).u.c.center = p;
	anchors(i).u.c.radius = d/2;
	anchors(i).u.c.a0 = 0.0;
	anchors(i).u.c.a1 = 360.0;
	anchors(i).width = 0;
}

static void CreateCornuAnchor(coOrd p, wBool_t lock) {
	DIST_T d = tempD.scale*0.15;

	DYNARR_APPEND(trkSeg_t,anchors_da,1);
	int i = anchors_da.cnt-1;
	anchors(i).type = lock?SEG_FILCRCL:SEG_CRVLIN;
	anchors(i).color = wDrawColorBlue;
	anchors(i).u.c.center = p;
	anchors(i).u.c.radius = d/2;
	anchors(i).u.c.a0 = 0.0;
	anchors(i).u.c.a1 = 360.0;
	anchors(i).width = 0;
	DYNARR_APPEND(trkSeg_t,anchors_da,1);
	i = anchors_da.cnt-1;
	anchors(i).type = SEG_CRVLIN;
	anchors(i).color = wDrawColorBlue;
	anchors(i).u.c.center = p;
	anchors(i).u.c.radius = d;
	anchors(i).u.c.a0 = 0.0;
	anchors(i).u.c.a1 = 360.0;
	anchors(i).width = 0;
}


static void CreateRadiusAnchor(coOrd p, ANGLE_T a, BOOL_T bi) {
	DYNARR_SET(trkSeg_t,anchors_da,anchors_da.cnt+5);
	DrawArrowHeads(&DYNARR_N(trkSeg_t,anchors_da,anchors_da.cnt-5),p,a,bi,wDrawColorBlue);
}

/*
 * Call cbezier.c CmdBezModify to alter Bezier Track and Lines.
 * Picking a Bezier will allow control point(s) modifications until terminated with "Enter"
 */
static STATUS_T ModifyBezier(wAction_t action, coOrd pos) {
	STATUS_T rc = C_CONTINUE;
	if (Dex.Trk == NULL) return C_ERROR;   //No track picked yet!
	switch (action&0xFF) {
		case C_START:
		case C_DOWN:
		case C_MOVE:
		case C_UP:
		case C_OK:
		case C_TEXT:
		case wActionMove:
			trackGauge = (IsTrack(Dex.Trk)?GetTrkGauge(Dex.Trk):0.0);
			rc = CmdBezModify(Dex.Trk, action, pos, trackGauge);
			break;
		case C_TERMINATE:
			rc = CmdBezModify(Dex.Trk, action, pos, trackGauge);
			Dex.Trk = NULL;
			modifyBezierMode = FALSE;
			break;
		case C_REDRAW:
			rc = CmdBezModify(Dex.Trk, action, pos, trackGauge);
			break;
	}
	return rc;
}

/*
 * Call ccornu.c CmdCornuModify to alter Cornu Track and Lines.
 * Picking a Cornu will allow end point(s) modifications until terminated with "Enter"
 */
static STATUS_T ModifyCornu(wAction_t action, coOrd pos) {
	STATUS_T rc = C_CONTINUE;
	if (Dex.Trk == NULL) return C_ERROR;   //No track picked yet!
	switch (action&0xFF) {
		case C_LCLICK:
		case C_START:
		case C_DOWN:
		case C_MOVE:
		case C_UP:
		case C_OK:
		case C_TEXT:
		case wActionMove:
			trackGauge = (IsTrack(Dex.Trk)?GetTrkGauge(Dex.Trk):0.0);
			rc = CmdCornuModify(Dex.Trk, action, pos, trackGauge);
			break;
		case C_TERMINATE:
			rc = CmdCornuModify(Dex.Trk, action, pos, trackGauge);
			Dex.Trk = NULL;
			modifyCornuMode = FALSE;
			break;
		case C_REDRAW:
			rc = CmdCornuModify(Dex.Trk, action, pos, trackGauge);
			break;
	}
	return rc;
}

/*
 * Picking a DRAW will allow point modifications until terminated with "Enter"
 */
static STATUS_T ModifyDraw(wAction_t action, coOrd pos) {
	STATUS_T rc = C_CONTINUE;
	if (Dex.Trk == NULL) return C_ERROR;   //No item picked yet!
	switch (action&0xFF) {
		case C_START:
		case C_DOWN:
		case C_MOVE:
		case C_UP:
			rc = ModifyTrack( Dex.Trk, action, pos );
			break;
		case wActionMove:
			rc = ModifyTrack( Dex.Trk, action, pos );
			break;
		case C_TEXT:
			//Delete or '0' - continues
			if ((action>>8 !=32) && (action >>8 !=13))
				return ModifyTrack( Dex.Trk, action, pos );
			//Enter/Space does not
			if ((action>>8 !=32) && (action>>8 != 13)) return C_CONTINUE;
			/*no break*/
		case C_OK:
			UndoStart( _("Modify Track"), "Modify( T%d[%d] )", GetTrkIndex(Dex.Trk), Dex.params.ep );
			UndoModify( Dex.Trk );
			rc = ModifyTrack( Dex.Trk, C_TEXT | (13<<8), pos );
			if (rc != C_CONTINUE) modifyDrawMode = FALSE;
			UndoEnd();
			break;
		case C_CANCEL:
		case C_FINISH:
		case C_CONFIRM:
		case C_TERMINATE:
			rc = ModifyTrack( Dex.Trk, action, pos );
			Dex.Trk = NULL;
			modifyDrawMode = FALSE;
			tempSegs_da.cnt = 0;
			rc = C_CONTINUE;
			break;
		case C_REDRAW:
			rc = ModifyTrack( Dex.Trk, action, pos );
			break;
		case C_CMDMENU:
			menuPos = pos;
			rc = ModifyTrack( Dex.Trk, action, pos );
			break;
		default:
			break;
	}
	return rc;
}


STATUS_T CmdModify(
		wAction_t action,
		coOrd pos )
/*
 * Extend and alter a track.
 * Extend a track with a curve or straight and optionally an easement.
 * Alter a ruler.
 * Modify a Bezier.
 */
{

	track_p trk, trk1;
	ANGLE_T a0;
	DIST_T d;
	ANGLE_T a;
	EPINX_T inx;
	curveType_e curveType;
	static BOOL_T changeTrackMode;


	STATUS_T rc;
	static DIST_T trackGauge;

	if ( changeTrackMode ) {
		if ( action == C_MOVE )
			action = C_RMOVE;
		if ( action == C_UP )
			action = C_RUP;
	}

	switch (action&0xFF) {

	case C_START:
		DYNARR_RESET(trkSeg_t,anchors_da);
		InfoMessage( _("Select a track to modify, Left-Click change length, Right-Click to add flextrack") );
		Dex.Trk = NULL;
		tempSegs_da.cnt = 0;
		/*ChangeParameter( &easementPD );*/
		trackGauge = 0.0;
		changeTrackMode = modifyRulerMode = FALSE;
		modifyBezierMode = FALSE;
		modifyCornuMode = FALSE;
		modifyDrawMode = FALSE;
		modifyExtendMode = FALSE;
		return C_CONTINUE;

	case C_DOWN:
		DYNARR_RESET(trkSeg_t,anchors_da);
		if (modifyBezierMode)
			return ModifyBezier(C_DOWN, pos);
		if (modifyCornuMode)
			return ModifyCornu(C_DOWN, pos);
		if (modifyDrawMode)
			return ModifyDraw(C_DOWN, pos);
		/*no break*/
	case C_LDOUBLE:
		DYNARR_SET( trkSeg_t, tempSegs_da, 2 );
		tempSegs(0).color = wDrawColorBlack;
		tempSegs(0).width = 0;
		tempSegs(1).color = wDrawColorBlack;
		tempSegs(1).width = 0;
		tempSegs_da.cnt = 0;
		Dex.Trk = OnTrack( &pos, TRUE, FALSE );
		//Dex.Trk = trk;
		if (Dex.Trk == NULL) {
			if ( ModifyRuler( C_DOWN, pos ) == C_CONTINUE )
				modifyRulerMode = TRUE;
			return C_CONTINUE;
		}
		if (!CheckTrackLayer( Dex.Trk ) ) {
			Dex.Trk = NULL;

			return C_CONTINUE;
		}
		trackGauge = (IsTrack(Dex.Trk)?GetTrkGauge(Dex.Trk):0.0);
		if (QueryTrack( Dex.Trk, Q_CAN_MODIFY_CONTROL_POINTS )) { //Bezier
			modifyBezierMode = TRUE;
			if (ModifyBezier(C_START, pos) != C_CONTINUE) {			//Call Start with track
				modifyBezierMode = FALSE;							//Function rejected Bezier
				Dex.Trk =NULL;
				tempSegs_da.cnt = 0;
			}
			return C_CONTINUE;										//That's it
		}
		if (QueryTrack( Dex.Trk, Q_IS_CORNU )) { 					//Cornu
			modifyCornuMode = TRUE;
			if (ModifyCornu(C_START, pos) != C_CONTINUE) {			//Call Start with track
				modifyCornuMode = FALSE;							//Function rejected Cornu
				Dex.Trk =NULL;
				tempSegs_da.cnt = 0;

			}
			return C_CONTINUE;										//That's it
		}

		if (QueryTrack( Dex.Trk, Q_IS_DRAW )) {
			modifyDrawMode = TRUE;
			if (ModifyDraw(C_START, pos) != C_CONTINUE) {
				modifyDrawMode = FALSE;
				Dex.Trk = NULL;
				tempSegs_da.cnt = 0;
			}
			return C_CONTINUE;
		}

		if ((action&0xFF) == C_LDOUBLE) return C_ERROR;

		if ((MyGetKeyState()&WKEY_CTRL)) goto extendTrack;



		if ( (MyGetKeyState()&WKEY_SHIFT) &&      //Free to change radius
			 QueryTrack( Dex.Trk, Q_CAN_MODIFYRADIUS )&&
			 ((inx=PickUnconnectedEndPoint(pos,Dex.Trk)) >= 0 )) {
			trk = Dex.Trk;
			while ( (trk1=GetTrkEndTrk(trk,1-inx)) &&        //Means next track to mine even if can be end...
					QueryTrack(trk1, Q_CANNOT_BE_ON_END) ) {
				inx = GetEndPtConnectedToMe( trk1, trk );
				trk = trk1;
			}
			if (trk1) {
				UndoStart( _("Change Track"), "Change( T%d[%d] )", GetTrkIndex(Dex.Trk), Dex.params.ep );
				inx = GetEndPtConnectedToMe( trk1, trk );
                Dex.Trk = NULL;
		UndrawNewTrack( trk );
                DeleteTrack(trk, TRUE);					//Get rid of original track
				if ( !GetTrkEndTrk( trk1, inx ) ) {
					Dex.Trk = trk1;
					Dex.pos00 = GetTrkEndPos( Dex.Trk, inx );
					changeTrackMode = TRUE;
					goto CHANGE_TRACK;
				}
			}
			ErrorMessage( MSG_CANNOT_CHANGE );
		}
		ModifyTrack(Dex.Trk, C_START, pos);         //Basically trim via Modify...
		rc = ModifyTrack( Dex.Trk, C_DOWN, pos );
		if ( rc != C_CONTINUE ) {
			Dex.Trk = NULL;
			rc = C_CONTINUE;
		}
		return rc;

	case wActionMove:
		DYNARR_RESET(trkSeg_t,anchors_da);
		if (modifyCornuMode) return ModifyCornu(wActionMove,pos);
		if (modifyDrawMode) return ModifyDraw(wActionMove,pos);
		if (modifyBezierMode) return ModifyBezier(wActionMove, pos);
		track_p t;
		if (((t=OnTrack(&pos,FALSE,TRUE))!= NULL) && CheckTrackLayerSilent( t )) {
			EPINX_T ep = PickUnconnectedEndPointSilent(pos, t);
			if (QueryTrack( t, Q_IS_CORNU )) {
				CreateCornuAnchor(pos,FALSE);
			} else if ( QueryTrack( t, Q_CAN_MODIFY_CONTROL_POINTS )) {
				CreateRadiusAnchor(pos,NormalizeAngle(GetAngleAtPoint(t,pos,NULL,NULL)+90.0),TRUE);
				CreateEndAnchor(pos,FALSE);
			} else if (QueryTrack(t,Q_CAN_ADD_ENDPOINTS)){     //Turntable
				trackParams_t tp;
				if (!GetTrackParams(PARAMS_CORNU, t, pos, &tp)) return C_CONTINUE;
				ANGLE_T a = tp.angle;
				Translate(&pos,tp.ttcenter,a,tp.ttradius);
				CreateRadiusAnchor(pos,a,FALSE);
			} else if (QueryTrack(t,Q_CAN_EXTEND)) {
				if (ep != -1) {
					if (MyGetKeyState()&WKEY_CTRL) {
						pos = GetTrkEndPos(t,ep);
						CreateEndAnchor(pos,FALSE);
						CreateRadiusAnchor(pos,GetTrkEndAngle(t,ep),FALSE);
						CreateRadiusAnchor(pos,GetTrkEndAngle(t,ep)+90,TRUE);
					} else {
						CreateEndAnchor(pos,FALSE);
						if ((MyGetKeyState()&WKEY_SHIFT) && 					//Shift Down
							QueryTrack( t, Q_CAN_MODIFYRADIUS ) &&				// Straight or Curve
							((inx=PickUnconnectedEndPointSilent(pos,t)) >= 0 )) { //Which has an open end
							if (GetTrkEndTrk(t,1-inx))					// Has to have a track on other end
								CreateRadiusAnchor(pos,NormalizeAngle(GetAngleAtPoint(t,pos,NULL,NULL)+90.0),TRUE);
						}
						CreateRadiusAnchor(pos,GetAngleAtPoint(t,pos,NULL,NULL),TRUE);
					}
				}
			} else if (ep>=0){													//Turnout
				pos = GetTrkEndPos(t, ep);
				CreateEndAnchor(pos,TRUE);
				if ( (MyGetKeyState()&WKEY_CTRL)) {
					CreateRadiusAnchor(pos,NormalizeAngle(GetTrkEndAngle(t,ep)),FALSE);
					CreateRadiusAnchor(pos,GetTrkEndAngle(t,ep)+90,TRUE);
					CreateEndAnchor(pos,TRUE);
				} else {
					CreateRadiusAnchor(pos,NormalizeAngle(GetTrkEndAngle(t,ep)),FALSE);
					CreateEndAnchor(pos,TRUE);
				}
			}
		} else if (((t=OnTrack(&pos,FALSE,FALSE))!= NULL)
				&& (!(GetLayerFrozen(GetTrkLayer(t)) && GetLayerModule(GetTrkLayer(t))))
				&& (QueryTrack(t, Q_IS_DRAW ) && !QueryTrack(t, Q_IS_TEXT)) ) {
			CreateEndAnchor(pos,FALSE);
		}
		return C_CONTINUE;

	case C_MOVE:
		if ( modifyRulerMode )
			return ModifyRuler( C_MOVE, pos );
		if (Dex.Trk == NULL)
			return C_CONTINUE;
		if ( modifyBezierMode )
			return ModifyBezier(C_MOVE, pos);
		if ( modifyCornuMode )
			return ModifyCornu(C_MOVE, pos);
		if ( modifyDrawMode)
			return ModifyDraw(C_MOVE, pos);
		if (modifyExtendMode && (MyGetKeyState()&WKEY_CTRL))
			goto extendTrackMove;
		tempSegs_da.cnt = 0;

		SnapPos( &pos );
		rc = ModifyTrack( Dex.Trk, C_MOVE, pos );
		if ( rc != C_CONTINUE ) {
			rc = C_CONTINUE;
			Dex.Trk = NULL;
		}
		return rc;

	case C_UP:
		DYNARR_RESET(trkSeg_t,anchors_da);
		if (Dex.Trk == NULL)
			return C_CONTINUE;
		if ( modifyRulerMode )
			return ModifyRuler( C_MOVE, pos );
		if ( modifyBezierMode )
			return ModifyBezier( C_UP, pos);
		if (modifyCornuMode)
			return ModifyCornu(C_UP, pos);
		if (modifyDrawMode)
			return ModifyDraw(C_UP, pos);
		if ((MyGetKeyState()&WKEY_CTRL)) goto extendTrackUp;

		tempSegs_da.cnt = 0;

		SnapPos( &pos );
		UndoStart( _("Modify Track"), "Modify( T%d[%d] )", GetTrkIndex(Dex.Trk), Dex.params.ep );
		UndoModify( Dex.Trk );
		rc = ModifyTrack( Dex.Trk, C_UP, pos );
		UndoEnd();
        Dex.Trk = NULL;
		return rc;

	case C_RDOWN:									//This is same as context menu....
extendTrack:
		DYNARR_RESET(trkSeg_t,anchors_da);
		changeTrackMode = TRUE;
		modifyExtendMode = TRUE;
		modifyRulerMode = FALSE;
		modifyBezierMode = FALSE;
		modifyCornuMode = FALSE;
		modifyDrawMode = FALSE;
		Dex.first = FALSE;
		if (((action&0xFF) == C_RDOWN) || ((action&0xFF)== C_DOWN)) {
			Dex.Trk = OnTrack( &pos, TRUE, TRUE );
			if (Dex.Trk) {
				if (!CheckTrackLayer( Dex.Trk ) ) {
					Dex.Trk = NULL;
					return C_CONTINUE;
				}
				trackGauge = GetTrkGauge( Dex.Trk );
				Dex.pos00 = pos;
	CHANGE_TRACK:
				if (GetTrackParams( PARAMS_EXTEND, Dex.Trk, Dex.pos00, &Dex.params)) {
					if (Dex.params.ep == -1) {
						Dex.Trk = NULL;
						return C_CONTINUE;
						break;
					}
					if (Dex.params.ep == 0) {
						Dex.params.arcR = -Dex.params.arcR;
					}
					Dex.pos00 = GetTrkEndPos(Dex.Trk,Dex.params.ep);
					Dex.angle = GetTrkEndAngle( Dex.Trk,Dex.params.ep);
					Translate( &Dex.pos00x, Dex.pos00, Dex.angle, 10.0 );
	LOG( log_modify, 1, ("extend endPt[%d] = [%0.3f %0.3f] A%0.3f\n",
								Dex.params.ep, Dex.pos00.x, Dex.pos00.y, Dex.angle ) )
					InfoMessage( _("Drag to add flex track") );
				} else {
					return C_ERROR;
				}
			} else {
				InfoMessage ( _("No track to extend"));
				return C_ERROR;
			}
			Dex.first = TRUE;
		} else if (!Dex.Trk) {
			InfoMessage ( _("No track selected"));
			return C_ERROR;
		}
        /* no break */
	case C_RMOVE:
extendTrackMove:
		DYNARR_RESET(trkSeg_t,anchors_da);
		tempSegs_da.cnt = 0;
		Dex.valid = FALSE;
		if (Dex.Trk == NULL) return C_CONTINUE;
		SnapPos( &pos );
		if ( Dex.first && FindDistance( pos, Dex.pos00 ) <= minLength )
			return C_CONTINUE;
		Dex.first = FALSE;
		Dex.pos01 = Dex.pos00;

		if (Dex.params.type == curveTypeCornu) {    			//Always Restrict Cornu drag out to match end
			ANGLE_T angle2 = NormalizeAngle(FindAngle(pos, Dex.pos00)-Dex.angle);
			if (angle2 > 90.0 && angle2 < 270.0) {
				if (Dex.params.cornuRadius[Dex.params.ep] == 0) {
					Translate( &pos, Dex.pos00, Dex.angle, FindDistance( Dex.pos00, pos ) );
				} else {
					ANGLE_T angle = FindAngle(Dex.params.cornuCenter[Dex.params.ep],pos)-
							FindAngle(Dex.params.cornuCenter[Dex.params.ep],Dex.pos00);
					pos=Dex.pos00;
					Rotate(&pos,Dex.params.cornuCenter[Dex.params.ep],angle);
				}
			} else pos = Dex.pos00;					//Only out from end
			PlotCurve( crvCmdFromCornu, Dex.pos00, Dex.pos00x, pos, &Dex.curveData, FALSE );
		} else
			PlotCurve( crvCmdFromEP1, Dex.pos00, Dex.pos00x, pos, &Dex.curveData, TRUE );
		curveType = Dex.curveData.type;
		if ( curveType == curveTypeStraight ) {
			Dex.r1 = 0.0;
			if (Dex.params.type == curveTypeCurve) {
				if (ComputeJoint( Dex.params.arcR, Dex.r1, &Dex.jointD ) == E_ERROR)
					return C_CONTINUE;
				d = Dex.params.len - Dex.jointD.d0;
				if (d <= minLength) {
					ErrorMessage( MSG_TRK_TOO_SHORT, "First ", PutDim(fabs(minLength-d)) );
					return C_CONTINUE;
				}
			} else {
				Dex.jointD.d1 = 0.0;
			}
			tempSegs(0).type = SEG_STRTRK;
			tempSegs(0).width = 0;
			tempSegs(0).u.l.pos[0] = Dex.pos01;
			tempSegs(0).u.l.pos[1] = Dex.curveData.pos1;
			d = FindDistance( Dex.pos01, Dex.curveData.pos1 );
			d -= Dex.jointD.d1;
			if (d <= minLength) {
				ErrorMessage( MSG_TRK_TOO_SHORT, "Extending ", PutDim(fabs(minLength-d)) );
				return C_CONTINUE;
			}
			tempSegs_da.cnt = 1;
			Dex.valid = TRUE;
			if (action != C_RDOWN)
				InfoMessage( _("Straight Track: Length=%s Angle=%0.3f"),
					FormatDistance( FindDistance( Dex.curveData.pos1, Dex.pos01 ) ),
					PutAngle( FindAngle( Dex.pos01, Dex.curveData.pos1 ) ) );
		} else if ( curveType == curveTypeNone ) {
			if (action != C_RDOWN)
				InfoMessage( _("Back") );
			return C_CONTINUE;
		} else if ( curveType == curveTypeCurve ) {
			Dex.r1 = Dex.curveData.curveRadius;
			if ( QueryTrack( Dex.Trk, Q_IGNORE_EASEMENT_ON_EXTEND ) ) {
							       /* Ignore easements when extending turnouts or turntables */
				Dex.jointD.x =
				Dex.jointD.r0 = Dex.jointD.r1 = 
				Dex.jointD.l0 = Dex.jointD.l1 = 
				Dex.jointD.d0 = Dex.jointD.d1 = 0.0;
				Dex.jointD.flip = Dex.jointD.negate = Dex.jointD.Scurve = FALSE;
				d = Dex.curveData.curveRadius * Dex.curveData.a1 * 2.0*M_PI/360.0;
			} else {					/* Easement code */
				if (easementVal<0.0) {  //Cornu Join - need to estimate a "good" easement length
					d = Dex.curveData.curveRadius * Dex.curveData.a1 * 2.0*M_PI/360.0;
					Dex.jointD.d0 = Dex.jointD.d1 =0.75*72*12/GetTrkScale(Dex.Trk); //Easement 1.5 cars long to start
					if (Dex.jointD.d0>(GetTrkLength(Dex.Trk,0,1)/2))
						Dex.jointD.d0 = GetTrkLength(Dex.Trk,0,1)/2;
					if (Dex.jointD.d1>d/2)
						Dex.jointD.d1 = d/2;
					Dex.jointD.negate = DifferenceBetweenAngles(Dex.angle,FindAngle(Dex.pos00,pos))<0.0;
					Dex.jointD.x = 2*trackGauge;  //Signal an easement present to JoinTracks
				} else {
					if ( easeR > 0.0 && Dex.r1 < easeR ) {
						ErrorMessage( MSG_RADIUS_LSS_EASE_MIN,
							FormatDistance( Dex.r1 ), FormatDistance( easeR ) );
						return C_CONTINUE;
					}
					if ( Dex.r1*2.0*M_PI*Dex.curveData.a1/360.0 > mapD.size.x+mapD.size.y ) {
						ErrorMessage( MSG_CURVE_TOO_LARGE );
						return C_CONTINUE;
					}
					if ( NormalizeAngle( FindAngle( Dex.pos00, pos ) - Dex.angle ) > 180.0 )
						Dex.r1 = - Dex.r1;
					if (ComputeJoint( Dex.params.arcR, Dex.r1, &Dex.jointD ) == E_ERROR)
						return C_CONTINUE;
					d = Dex.params.len - Dex.jointD.d0;
					if (d <= minLength) {
						ErrorMessage( MSG_TRK_TOO_SHORT, "First ", PutDim(fabs(minLength-d)) );
						return C_CONTINUE;
					}
				}
				d -= Dex.jointD.d1;
				a0 = Dex.angle + (Dex.jointD.negate?-90.0:+90.0);
				Translate( &Dex.pos01, Dex.pos00, a0, Dex.jointD.x );
				Translate( &Dex.curveData.curvePos, Dex.curveData.curvePos,
							a0, Dex.jointD.x );
LOG( log_modify, 2, ("A=%0.3f X=%0.3f\n", a0, Dex.jointD.x ) )
			}
			if (d <= minLength) {
				ErrorMessage( MSG_TRK_TOO_SHORT, "Extending ", PutDim(fabs(minLength-d)) );
				return C_CONTINUE;
			}
			tempSegs(0).type = SEG_CRVTRK;
			tempSegs(0).width = 0;
			tempSegs(0).u.c.center = Dex.curveData.curvePos;
			tempSegs(0).u.c.radius = Dex.curveData.curveRadius,
			tempSegs(0).u.c.a0 = Dex.curveData.a0;
			tempSegs(0).u.c.a1 = Dex.curveData.a1;
			tempSegs_da.cnt = 1;
			double da = D2R(Dex.curveData.a1);
			if (da < 0.0)
				da = 2*M_PI + da;
			a = NormalizeAngle( Dex.angle - FindAngle( Dex.pos00, Dex.curveData.curvePos ) );
			if ( a < 180.0 )
				a = NormalizeAngle( Dex.curveData.a0-90 );
			else
				a = NormalizeAngle( Dex.curveData.a0+Dex.curveData.a1+90.0 );
			Dex.valid = TRUE;
			if (action != C_RDOWN)
				InfoMessage( _("Curve Track: Radius=%s Length=%s Angle=%0.3f"),
					FormatDistance( Dex.curveData.curveRadius ),
					FormatDistance( Dex.curveData.curveRadius * da),
					Dex.curveData.a1 );
		}
		return C_CONTINUE;

	case C_RUP:
extendTrackUp:
		changeTrackMode = FALSE;
		modifyExtendMode = FALSE;
		tempSegs_da.cnt = 0;
		if (Dex.Trk == NULL) return C_CONTINUE;
		if (!Dex.valid)
			return C_CONTINUE;
		UndoStart( _("Extend Track"), "Extend( T%d[%d] )", GetTrkIndex(Dex.Trk), Dex.params.ep );
		trk = NULL;
		curveType = Dex.curveData.type;
   
		if ( curveType == curveTypeStraight ) {
			if (QueryTrack(Dex.Trk,Q_CAN_EXTEND))   //Check it isn't a turnout end....
				if ( Dex.params.type == curveTypeStraight &&
						FindDistance(Dex.pos01, Dex.curveData.pos1) > 0 ) {
					UndoModify( Dex.Trk );
					AdjustStraightEndPt( Dex.Trk, Dex.params.ep, Dex.curveData.pos1 );
					UndoEnd();
					DrawNewTrack(Dex.Trk );
					return C_TERMINATE;
			}
			if (FindDistance(Dex.pos01, Dex.curveData.pos1) == 0) return C_ERROR;
LOG( log_modify, 1, ("L = %0.3f, P0 = %0.3f, P1 = %0.3f\n",
						Dex.params.len, Dex.pos01, Dex.curveData.pos1 ) )
			trk = NewStraightTrack( Dex.pos01, Dex.curveData.pos1 );
			inx = 0;

		} else if ( curveType == curveTypeCurve ) {
LOG( log_modify, 1, ("R = %0.3f, A0 = %0.3f, A1 = %0.3f\n",
						Dex.curveData.curveRadius, Dex.curveData.a0, Dex.curveData.a1 ) )
			trk = NewCurvedTrack( Dex.curveData.curvePos, Dex.curveData.curveRadius,
						Dex.curveData.a0, Dex.curveData.a1, 0 );
			inx = PickUnconnectedEndPoint( Dex.pos01, trk );
			if (inx == -1)
				return C_ERROR;

		} else {
			return C_ERROR;
		}
		CopyAttributes( Dex.Trk, trk );
		if (Dex.jointD.d1 == 0) {
			DrawEndPt( &mainD, Dex.Trk, Dex.params.ep, wDrawColorWhite );
			ConnectTracks(Dex.Trk, Dex.params.ep, trk, inx);
			DrawEndPt( &mainD, Dex.Trk, Dex.params.ep, wDrawColorBlack );
		} else {
			UndrawNewTrack( Dex.Trk );
			JoinTracks( Dex.Trk, Dex.params.ep, Dex.pos00, trk, inx, Dex.pos01, &Dex.jointD );
			DrawNewTrack( Dex.Trk );
		}
		UndoEnd();
		tempSegs_da.cnt = 0;
		DrawNewTrack( trk );
		return C_TERMINATE;

	case C_REDRAW:
		if (modifyBezierMode) return ModifyBezier(C_REDRAW, pos);
		if (modifyCornuMode) return ModifyCornu(C_REDRAW, pos);
		if (modifyDrawMode) return ModifyDraw(C_REDRAW, pos);
		DrawSegs( &tempD, zero, 0.0, &tempSegs(0), tempSegs_da.cnt, trackGauge, wDrawColorBlack );
		if (anchors_da.cnt)
			DrawSegs( &tempD, zero, 0.0, &anchors(0), anchors_da.cnt, trackGauge, wDrawColorBlack );

		return C_CONTINUE;

	case C_TEXT:
		if ((action>>8) == 'c') {
			panCenter = pos;
			LOG( log_pan, 2, ( "PanCenter:Mod-%d %0.3f %0.3f\n", __LINE__, panCenter.x, panCenter.y ) );
			PanHere((void*)0);
			return C_CONTINUE;
		}
		if ((action>>8) == 'e') {
			DoZoomExtents(0);
		}
		if ((action>>8) == '0' || (action>>8 == 'o')) {
			PanMenuEnter('o');
		}
		if ( !Dex.Trk )
			return C_CONTINUE;
		if (modifyBezierMode)
			return ModifyBezier(action, pos);
		if (modifyCornuMode)
			return ModifyCornu(action, pos);
		if (modifyDrawMode)
			return ModifyDraw(action, pos);
		return ModifyTrack( Dex.Trk, action, pos );

	case C_CMDMENU:
		if ( !Dex.Trk ) {
			menuPos = pos;
			wMenuPopupShow(modPopupM);
			return C_CONTINUE;
		}
		if (modifyBezierMode)
			return ModifyBezier(action, pos);
		if (modifyCornuMode)
			return ModifyCornu(action, pos);
		if (modifyDrawMode)
			return ModifyDraw(action, pos);
		return ModifyTrack( Dex.Trk, action, pos );

	case C_LCLICK:
		if ( modifyDrawMode) {
			rc = ModifyDraw(C_DOWN, pos);
			if (rc == C_CONTINUE)
				return ModifyDraw(C_UP, pos);
		}
		if (modifyCornuMode)
			return ModifyCornu(action, pos);
		/*no break*/
	default:
		if (modifyBezierMode) return ModifyBezier(action, pos);
		if (modifyCornuMode)  return ModifyCornu(action, pos);
		if (modifyDrawMode) return ModifyDraw(action, pos);
		if (Dex.Trk)
			return ModifyTrack( Dex.Trk, action, pos );
		return C_CONTINUE;
	}
	return C_CONTINUE;
}


/*****************************************************************************
 *
 * INITIALIZATION
 *
 */

#include "bitmaps/extend.xpm"
extern wIndex_t panCmdInx;
extern wIndex_t selectCmdInx;
extern wIndex_t describeCmdInx;


void InitCmdModify( wMenu_p menu )
{
	modifyCmdInx = AddMenuButton( menu, CmdModify, "cmdModify", _("Modify"), wIconCreatePixMap(extend_xpm), LEVEL0_50, IC_STICKY|IC_POPUP|IC_WANT_MOVE|IC_CMDMENU, ACCL_MODIFY, NULL );
	log_modify = LogFindIndex( "modify" );
	modPopupM = MenuRegister( "Modify Context Menu" );
	wMenuPushCreate(modPopupM, "cmdSelectMode", GetBalloonHelpStr("cmdSelectMode"), 0, DoCommandB, (void*) (intptr_t) selectCmdInx);
	wMenuPushCreate(modPopupM, "cmdDescribeMode", GetBalloonHelpStr("cmdDescribeMode"), 0, DoCommandB, (void*) (intptr_t) describeCmdInx);
	wMenuPushCreate(modPopupM, "cmdPanMode", GetBalloonHelpStr("cmdPanMode"), 0, DoCommandB, (void*) (intptr_t) panCmdInx);
	wMenuSeparatorCreate(modPopupM);
	wMenuPushCreate(modPopupM, "", _("Zoom In"), 0,(wMenuCallBack_p) DoZoomUp, (void*) 1);
	wMenuPushCreate(modPopupM, "", _("Zoom Out"), 0,	(wMenuCallBack_p) DoZoomDown, (void*) 1);
	wMenuPushCreate(modPopupM, "", _("Pan center - 'c'"), 0,	(wMenuCallBack_p) PanHere, (void*) 3);
}