summaryrefslogtreecommitdiff
path: root/app/bin/dcmpnd.c
blob: 9b7dfcc2d3a709d7c3ef43d88d16e5641b015c29 (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
/* \file dcmpnd.c
 * Compound tracks: Turnouts and Structures
 */

/*  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "compound.h"
#include "cundo.h"
#include "custom.h"
#include "fileio.h"
#include "param.h"
#include "include/paramfile.h"
#include "shrtpath.h"
#include "track.h"
#include "trkendpt.h"
#include "common-ui.h"

/*****************************************************************************
 *
 * Update Titles
 *
 */

static wWin_p updateTitleW;
typedef enum { updateUnknown, updateTurnout, updateStructure } updateType_e;
static updateType_e updateListType;
static BOOL_T updateWVisible;
static BOOL_T updateWStale;
typedef struct {
	updateType_e type;
	SCALEINX_T scale;
	char * old;
	char * new;
} updateTitleElement;
static dynArr_t updateTitles_da;
#define updateTitles(N) DYNARR_N( updateTitleElement, updateTitles_da, N )

static void UpdateTitleIgnore( void* junk );
static wIndex_t updateTitleInx;
static paramData_t updateTitlePLs[] = {
	{	PD_MESSAGE, "This file contains Turnout and Structure Titles which should be updated." },
	{	PD_MESSAGE, "This dialog allows you to change the definitions in this file." },
	{	PD_MESSAGE, "To replace the old name, choose a definition from the list." },
	{	PD_MESSAGE, "If the required definition is not loaded you can use the Load button" },
	{	PD_MESSAGE, "to invoke the Parameter Files dialog to load the required Parameter File." },
	{	PD_MESSAGE, "If you choose Cancel then the Titles will not be changed and some" },
	{	PD_MESSAGE, "features (Price List and Label selection) may not be fully functional." },
	{	PD_MESSAGE, "You can use the List Labels control on the Preferences dialog to" },
	{	PD_MESSAGE, "control the format of the list entries" },
#define I_UPDATESTR		(9)
	{	PD_STRING, NULL, "old", PDO_NOPREF, I2VP(400), NULL, BO_READONLY },
#define I_UPDATELIST	(10)
#define updateTitleL	((wList_p)updateTitlePLs[I_UPDATELIST].control)
	{	PD_DROPLIST, NULL, "sel", PDO_NOPREF, I2VP(400) },
	{	PD_BUTTON, UpdateTitleIgnore, "ignore", PDO_DLGCMDBUTTON, NULL, N_("Ignore") },
#define I_UPDATELOAD	(12)
	{	PD_BUTTON, NULL, "load", 0, NULL, N_("Load") }
};
static paramGroup_t updateTitlePG = { "updatetitle", 0, updateTitlePLs, COUNT( updateTitlePLs ) };


static void UpdateTitleChange( long changes )
{
	if ( (changes & (CHANGE_SCALE|CHANGE_PARAMS)) == 0 ) {
		return;
	}
	if (!updateWVisible) {
		updateWStale = TRUE;
		return;
	}
	wControlShow( (wControl_p)updateTitleL, FALSE );
	wListClear( updateTitleL );
	if (updateTitles(updateTitleInx).type == updateTurnout) {
		TurnoutAdd( listLabels, updateTitles(updateTitleInx).scale, updateTitleL, NULL,
		            -1 );
	} else {
		StructAdd( listLabels, updateTitles(updateTitleInx).scale, updateTitleL,
		           NULL );
	}
	wControlShow( (wControl_p)updateTitleL, TRUE );
	updateListType = updateTitles(updateTitleInx).type;
}


static void UpdateTitleNext( void )
{
	wIndex_t inx;
	wIndex_t cnt;
	track_p trk;
	struct extraDataCompound_t *xx;
	updateTitleInx++;
	if (updateTitleInx >= updateTitles_da.cnt) {
		wHide( updateTitleW );
		updateWVisible = FALSE;
		InfoMessage( _("Updating definitions, please wait") );
		cnt = 0;
		trk = NULL;
		while (TrackIterate( &trk ) ) {
			InfoCount(cnt++);
			TRKTYP_T trkType = GetTrkType(trk);
			if (trkType == T_TURNOUT || trkType == T_STRUCTURE) {
				xx = GET_EXTRA_DATA(trk, trkType, extraDataCompound_t);
				for (inx=0; inx<updateTitles_da.cnt; inx++) {
					if ( updateTitles(inx).old &&
					     strcmp( xx->title, updateTitles(inx).old ) == 0 ) {
						xx->title = MyStrdup( updateTitles(inx).new );
						break;
					}
				}
			}
		}
		DYNARR_RESET( updateTitleElement, updateTitles_da );
		InfoMessage("");
		InfoCount( trackCount );
		SetFileChanged();
		DoChangeNotification( CHANGE_MAIN );
		return;
	}
	ParamLoadMessage( &updateTitlePG, I_UPDATESTR,
	                  updateTitles(updateTitleInx).old );
	if (updateWStale || updateTitles(updateTitleInx).type != updateListType) {
		UpdateTitleChange( CHANGE_SCALE|CHANGE_PARAMS );
	}
}


static void UpdateTitleUpdate( void* junk )
{
	void * selP;
	turnoutInfo_t * to;
	wListGetValues( updateTitleL, NULL, 0, NULL, &selP );
	if (selP != NULL) {
		to = (turnoutInfo_t*)selP;
		updateTitles(updateTitleInx).new = to->title;
	}
	UpdateTitleNext();
}

static void UpdateTitleIgnore( void* junk )
{
	updateTitles(updateTitleInx).old = NULL;
	UpdateTitleNext();
}

static void UpdateTitleCancel( wWin_p junk )
{
	wHide( updateTitleW );
	DYNARR_RESET( updateTitleElement, updateTitles_da );
	updateWVisible = FALSE;
}


void DoUpdateTitles( void )
{
	if (updateTitles_da.cnt <= 0) {
		return;
	}
	if (updateTitleW == NULL) {
		ParamRegister( &updateTitlePG );
		updateTitlePLs[I_UPDATELOAD].valueP = ParamFilesInit();
		updateTitleW = ParamCreateDialog( &updateTitlePG,
		                                  MakeWindowTitle(_("Update Title")), _("Update"), UpdateTitleUpdate,
		                                  UpdateTitleCancel, TRUE, NULL, 0, NULL );
		RegisterChangeNotification( UpdateTitleChange );
	}
	updateTitleInx = -1;
	wShow( updateTitleW );
	updateWVisible = TRUE;
	updateListType = updateUnknown;
	UpdateTitleNext();
}

EXPORT void UpdateTitleMark(
        char * title,
        SCALEINX_T scale )
{
	int inx;
	updateTitleElement * ut;
	if ( inPlayback ) {
		return;
	}
	for (inx=0; inx<updateTitles_da.cnt; inx++) {
		if (strcmp(title,updateTitles(inx).old) == 0) {
			return;
		}
	}
	DYNARR_APPEND( updateTitleElement, updateTitles_da, 10 );
	ut = &updateTitles(updateTitles_da.cnt-1);
	if ( TempEndPtsCount() > 0) {
		ut->type = updateTurnout;
	} else {
		ut->type = updateStructure;
	}
	ut->scale = scale;
	ut->old = MyStrdup(title);
	ut->new = NULL;
}

/*****************************************************************************
 *
 * Refresh Compound
 *
 */

static BOOL_T CheckCompoundEndPoint(
        track_p trk,
        EPINX_T trkEp,
        turnoutInfo_t * to,
        EPINX_T toEp,
        BOOL_T flip )
{

	struct extraDataCompound_t *xx = GET_EXTRA_DATA(trk, T_TURNOUT,
	                                 extraDataCompound_t);
	coOrd pos;
	DIST_T d;
	ANGLE_T a, a2;
	pos = GetTrkEndPos( trk, trkEp );
	Rotate( &pos, xx->orig, -xx->angle );
	pos.x -= xx->orig.x;
	pos.y -= xx->orig.y;
	if ( flip ) {
		pos.y = - pos.y;
	}
	d = FindDistance( pos, GetEndPtPos(EndPtIndex(to->endPt,toEp)));
	if ( d > connectDistance ) {
		sprintf( message,
		         _("End-Point #%d of the selected and actual turnouts are not close"), toEp );
		return FALSE;
	}
	a = GetTrkEndAngle( trk, trkEp );
	a2 = GetEndPtAngle(EndPtIndex(to->endPt,toEp));
	if ( flip ) {
		a2 = 180.0 - a2;
	}
	a = NormalizeAngle( a - xx->angle - a2 + connectAngle/2.0 );
	if ( a > connectAngle ) {
		sprintf( message,
		         _("End-Point #%d of the selected and actual turnouts are not aligned"), toEp );
		return FALSE;
	}
	return TRUE;
}


int refreshCompoundCnt;
static BOOL_T RefreshCompound1(
        track_p trk,
        turnoutInfo_t * to )
{
	EPINX_T ep, epCnt;
	BOOL_T ok;
	BOOL_T flip = FALSE;

	epCnt = GetTrkEndPtCnt(trk);
	if ( epCnt != to->endCnt ) {
		strcpy( message,
		        _("The selected Turnout had a differing number of End-Points") );
		return FALSE;
	}
	ok = TRUE;
	for ( ep=0; ep<epCnt; ep++ )
		if (!CheckCompoundEndPoint( trk, ep, to, ep, FALSE )) {
			ok = FALSE;
			break;
		}
	if ( !ok ) {
		if ( ep > 0 && epCnt == 2 &&
		     CheckCompoundEndPoint( trk, 1, to, 1, TRUE ) ) {
			flip = TRUE;
			ok = TRUE;
		} else if ( ep > 0 && epCnt == 3 &&
		            CheckCompoundEndPoint( trk, 1, to, 2, FALSE ) &&
		            CheckCompoundEndPoint( trk, 2, to, 1, FALSE ) ) {
			ok = TRUE;
		} else if ( ep > 0 && epCnt == 4 &&
		            CheckCompoundEndPoint( trk, 1, to, 3, FALSE ) &&
		            CheckCompoundEndPoint( trk, 2, to, 2, FALSE ) &&
		            CheckCompoundEndPoint( trk, 3, to, 1, FALSE ) ) {
			ok = TRUE;
		} else {
			return FALSE;
		}
	}
	UndoModify( trk );
	struct extraDataCompound_t *xx = GET_EXTRA_DATA(trk, T_NOTRACK,
	                                 extraDataCompound_t);
	FreeFilledDraw( xx->segCnt, xx->segs );
	MyFree( xx->segs );
	xx->segCnt = to->segCnt;
	xx->segs = (trkSeg_p)MyMalloc( xx->segCnt * sizeof *(trkSeg_p)0 );
	memcpy( xx->segs, to->segs, xx->segCnt * sizeof *(trkSeg_p)0 );
	xx->pathOverRide = to->pathOverRide;
	xx->pathNoCombine = to->pathNoCombine;
	SetPaths( trk, GetParamPaths(to) );
	if ( flip ) {
		FlipSegs( xx->segCnt, xx->segs, zero, 90.0 );
	}
	ClrTrkBits( trk, TB_SELECTED );
	refreshCompoundCnt++;
	CloneFilledDraw( xx->segCnt, xx->segs, FALSE );
	return TRUE;
}


typedef struct {
	char * name;
	turnoutInfo_t * to;
} refreshSpecial_t;
static dynArr_t refreshSpecial_da;
#define refreshSpecial(N) DYNARR_N( refreshSpecial_t, refreshSpecial_da, N )
static wIndex_t refreshSpecialInx;
static BOOL_T refreshReturnVal;
static void RefreshSkip( void * junk );
static paramListData_t refreshSpecialListData = { 30, 600, 0, NULL, NULL };
static paramData_t refreshSpecialPLs[] = {
#define REFRESH_M1		(0)
	{ PD_MESSAGE, NULL, NULL, 0/*PDO_DLGRESIZEW*/, I2VP(380) },
#define REFRESH_M2		(1)
	{ PD_MESSAGE, NULL, NULL, 0/*PDO_DLGRESIZEW*/, I2VP(380) },
#define REFRESH_S		(2)
	{ PD_MESSAGE, NULL, NULL, 0/*PDO_DLGRESIZEW*/, I2VP(380) },
#define REFRESH_L		(3)
	{ PD_LIST, &refreshSpecialInx, "list", PDO_LISTINDEX|PDO_NOPREF|PDO_DLGRESIZE, &refreshSpecialListData, NULL, BO_READONLY },
	{ PD_BUTTON, RefreshSkip, "skip", PDO_DLGCMDBUTTON, NULL, N_("Skip") }
};
static paramGroup_t refreshSpecialPG = { "refreshSpecial", 0, refreshSpecialPLs, COUNT( refreshSpecialPLs ) };
static void RefreshSpecialOk(
        void * junk )
{
	wHide( refreshSpecialPG.win );
}
static void RefreshSpecialCancel(
        wWin_p win )
{
	refreshSpecialInx = -1;
	refreshReturnVal = FALSE;
	wHide( refreshSpecialPG.win );
}
static void RefreshSkip(
        void * junk )
{
	refreshSpecialInx = -1;
	wHide( refreshSpecialPG.win );
}

EXPORT BOOL_T RefreshCompound(
        track_p trk,
        BOOL_T junk )
{
	TRKTYP_T trkType;
	struct extraDataCompound_t *xx;
	int inx;
	turnoutInfo_t *to;
	SCALEINX_T scale;

	if ( trk == NULL ) {
		InfoMessage( _("%d Track(s) refreshed"), refreshCompoundCnt );
		refreshCompoundCnt = 0;
		for ( inx=0; inx<refreshSpecial_da.cnt; inx++ )
			if ( refreshSpecial(inx).name != NULL &&
			     refreshSpecial(inx).to == NULL ) {
				refreshSpecial(inx).name = NULL;
			}
		return FALSE;
	}
	trkType = GetTrkType(trk);
	scale = GetTrkScale(trk);
	if ( trkType != T_TURNOUT && trkType != T_STRUCTURE ) {
		ClrTrkBits( trk, TB_SELECTED );
		return TRUE;
	}
	xx = GET_EXTRA_DATA(trk, trkType, extraDataCompound_t);
	refreshReturnVal = TRUE;
	for ( inx=0; inx<refreshSpecial_da.cnt; inx++ ) {
		if ( refreshSpecial(inx).name != NULL &&
		     strcasecmp( xx->title, refreshSpecial(inx).name ) == 0 ) {
			to = refreshSpecial(inx).to;
			if ( to == NULL ) {
				return TRUE;
			}
			if ( IsParamValid(to->paramFileIndex) &&
			     to->segCnt > 0 &&
			     (FIT_NONE != CompatibleScale( GetTrkEndPtCnt(trk)>0, to->scaleInx, scale ) )) {
				if ( RefreshCompound1( trk, refreshSpecial(inx).to ) ) {
					if ( strcasecmp( xx->title, to->title ) != 0 ) {
						MyFree( xx->title );
						xx->title = MyStrdup( to->title );
					}
					return TRUE;
				}
			}
		}
	}
	if ( ( to = FindCompound( FIND_TURNOUT|FIND_STRUCT, NULL, xx->title ) ) != NULL
	     &&
	     RefreshCompound1( trk, to ) ) {
		return TRUE;
	}
	if ( refreshSpecialPG.win == NULL ) {
		ParamRegister( &refreshSpecialPG );
		ParamCreateDialog( &refreshSpecialPG,
		                   MakeWindowTitle(_("Refresh Turnout/Structure")), _("Ok"), RefreshSpecialOk,
		                   RefreshSpecialCancel, TRUE, NULL, F_BLOCK|F_RESIZE|F_RECALLSIZE, NULL );
	}
	ParamLoadMessage( &refreshSpecialPG, REFRESH_M1,
	                  _("Choose a Turnout/Structure to replace:") );
	ParamLoadMessage( &refreshSpecialPG, REFRESH_M2, "" );
	refreshSpecialInx = -1;
	wListClear( (wList_p)refreshSpecialPLs[REFRESH_L].control );
	if ( GetTrkEndPtCnt(trk) > 0 ) {
		to = TurnoutAdd( listLabels, scale,
		                 (wList_p)refreshSpecialPLs[REFRESH_L].control, NULL, GetTrkEndPtCnt(trk) );
	} else {
		to = StructAdd( listLabels, scale,
		                (wList_p)refreshSpecialPLs[REFRESH_L].control, NULL );
	}
	if ( to == NULL ) {
		NoticeMessage( MSG_NO_TURNOUTS_AVAILABLE, _("Ok"), NULL,
		               GetTrkEndPtCnt(trk)>0 ? _("Turnouts") : _("Structures") );
		return FALSE;
	}
	FormatCompoundTitle( listLabels, xx->title );
	ParamLoadMessage( &refreshSpecialPG, REFRESH_S, message );
	while (1) {
		wListSetIndex( (wList_p)refreshSpecialPLs[REFRESH_L].control, -1 );
		wShow( refreshSpecialPG.win );
		if ( refreshSpecialInx < 0 ) {
			if ( refreshReturnVal ) {
				DYNARR_APPEND( refreshSpecial_t, refreshSpecial_da, 10 );
				refreshSpecial(refreshSpecial_da.cnt-1).to = NULL;
				refreshSpecial(refreshSpecial_da.cnt-1).name = MyStrdup( xx->title );
			}
			return refreshReturnVal;
		}
		to = (turnoutInfo_t*)wListGetItemContext( (wList_p)
		                refreshSpecialPLs[REFRESH_L].control, refreshSpecialInx );
		if ( to != NULL &&
		     RefreshCompound1( trk, to ) ) {
			DYNARR_APPEND( refreshSpecial_t, refreshSpecial_da, 10 );
			refreshSpecial(refreshSpecial_da.cnt-1).to = to;
			refreshSpecial(refreshSpecial_da.cnt-1).name = MyStrdup( xx->title );
			if ( strcasecmp( xx->title, to->title ) != 0 ) {
				MyFree( xx->title );
				xx->title = MyStrdup( to->title );
			}
			return TRUE;
		}
		ParamLoadMessage( &refreshSpecialPG, REFRESH_M1, message );
		ParamLoadMessage( &refreshSpecialPG, REFRESH_M2,
		                  _("Choose another Turnout/Structure to replace:") );
	}
}

/*****************************************************************************
 *
 * Custom Management Support
 *
 */

static char renameManuf[STR_SIZE];
static char renameDesc[STR_SIZE];
static char renamePartno[STR_SIZE];
static turnoutInfo_t * renameTo;

static paramData_t renamePLs[] = {
	/*0*/ { PD_STRING, renameManuf, "manuf", PDO_NOPREF | PDO_NOTBLANK, I2VP(350), N_("Manufacturer"), 0, 0, sizeof(renameManuf)},
	/*1*/ { PD_STRING, renameDesc, "desc", PDO_NOPREF | PDO_NOTBLANK, I2VP(230), N_("Description"), 0, 0, sizeof(renameDesc)},
	/*2*/ { PD_STRING, renamePartno, "partno", PDO_NOPREF|PDO_DLGHORZ|PDO_DLGIGNORELABELWIDTH | PDO_NOTBLANK, I2VP(100), N_("#"), 0, 0, sizeof(renamePartno)}
};
static paramGroup_t renamePG = { "rename", 0, renamePLs, COUNT( renamePLs ) };


EXPORT BOOL_T CompoundCustomSave(
        FILE * f )
{
	int inx;
	turnoutInfo_t * to;
	BOOL_T rc = TRUE;

	for ( inx=0; inx<turnoutInfo_da.cnt; inx++ ) {
		to = turnoutInfo(inx);
		if (to->paramFileIndex == PARAM_CUSTOM && to->segCnt > 0) {
			rc &= fprintf( f, "TURNOUT %s \"%s\"\n", GetScaleName(to->scaleInx),
			               PutTitle(to->title) )>0;
			if ( to->customInfo ) {
				rc &= fprintf( f, "\tU %s\n",to->customInfo )>0;
			}
			rc &= WriteCompoundPathsEndPtsSegs( f, GetParamPaths( to ), to->segCnt,
			                                    to->segs,
			                                    to->endCnt, to->endPt );
		}
	}
	for ( inx=0; inx<structureInfo_da.cnt; inx++ ) {
		to = structureInfo(inx);
		if (to->paramFileIndex == PARAM_CUSTOM && to->segCnt > 0) {
			rc &= fprintf( f, "STRUCTURE %s \"%s\"\n", GetScaleName(to->scaleInx),
			               PutTitle(to->title) )>0;
			if ( to->customInfo ) {
				rc &= fprintf( f, "\tU %s\n",to->customInfo )>0;
			}
			rc &= WriteSegs( f, to->segCnt, to->segs );
		}
	}
	return rc;
}


static void RenameOk( void * junk )
{
	sprintf( message, "%s\t%s\t%s", renameManuf, renameDesc, renamePartno );
	if ( renameTo->title ) {
		MyFree( renameTo->title );
	}
	renameTo->title = MyStrdup( message );
	wHide( renamePG.win );
	DoChangeNotification( CHANGE_PARAMS );
}


static int CompoundCustMgmProc(
        int cmd,
        void * data )
{
	turnoutInfo_t * to = (turnoutInfo_t*)data;
	turnoutInfo_t * to2=NULL;
	int inx;
	char * mP, *pP, *nP;
	int mL, pL, nL;
	BOOL_T rc = TRUE;

	switch ( cmd ) {
	case CUSTMGM_DO_COPYTO:
		if ( to->segCnt <= 0 ) {
			return TRUE;
		}
		if ( to->endCnt ) {
			rc &= fprintf( customMgmF, "TURNOUT %s \"%s\"\n", GetScaleName(to->scaleInx),
			               PutTitle(to->title) )>0;
			if ( to->customInfo ) {
				rc &= fprintf( customMgmF, "\tU %s\n",to->customInfo )>0;
			}
			rc &= WriteCompoundPathsEndPtsSegs( customMgmF,
			                                    GetParamPaths( to ), to->segCnt, to->segs,
			                                    to->endCnt, to->endPt );
		} else {
			rc &= fprintf( customMgmF, "STRUCTURE %s \"%s\"\n", GetScaleName(to->scaleInx),
			               PutTitle(to->title) )>0;
			if ( to->customInfo ) {
				rc &= fprintf( customMgmF, "\tU %s\n",to->customInfo )>0;
			}
			rc &= WriteSegs( customMgmF, to->segCnt, to->segs );
		}
		return rc;
	case CUSTMGM_CAN_EDIT:
		return (to->endCnt != 0 && to->customInfo != NULL);
	case CUSTMGM_DO_EDIT:
		if ( to->endCnt == 0 || to->customInfo==NULL )	{
			renameTo = to;
			ParseCompoundTitle( to->title, &mP, &mL, &pP, &pL, &nP, &nL );
			strncpy( renameManuf, mP, mL ); renameManuf[mL] = 0;
			strncpy( renameDesc, pP, pL ); renameDesc[pL] = 0;
			strncpy( renamePartno, nP, nL ); renamePartno[nL] = 0;
			if ( !renamePG.win ) {
				ParamRegister( &renamePG );
				ParamCreateDialog( &renamePG, MakeWindowTitle(_("Rename Object")), _("Ok"),
				                   RenameOk, wHide, TRUE, NULL, F_BLOCK, NULL );
			}
			ParamLoadControls( &renamePG );
			wShow( renamePG.win );
		} else {
			for (inx=0; inx<turnoutInfo_da.cnt && to!=turnoutInfo(inx); inx++);
			if ( inx > 0 &&
			     turnoutInfo(inx-1)->customInfo &&
			     strcmp( to->customInfo, turnoutInfo(inx-1)->customInfo ) == 0 ) {
				to2 = to;
				to = turnoutInfo(inx-1);
			} else if ( inx < turnoutInfo_da.cnt-1 &&
			            turnoutInfo(inx+1)->customInfo &&
			            strcmp( to->customInfo, turnoutInfo(inx+1)->customInfo ) == 0 ) {
				to2 = turnoutInfo(inx+1);
			}
			EditCustomTurnout( to, to2 );
		}
		return TRUE;
	case CUSTMGM_CAN_DELETE:
		return TRUE;
	case CUSTMGM_DO_DELETE:
		to->segCnt = 0;
		return TRUE;
	case CUSTMGM_GET_TITLE:
		ParseCompoundTitle( to->title, &mP, &mL, &pP, &pL, &nP, &nL );
		sprintf( message, "\t%.*s\t%s\t%.*s\t%.*s", mL, mP, GetScaleName(to->scaleInx),
		         nL, nP, pL, pP );
		return TRUE;
	}
	return FALSE;
}


#include "bitmaps/turnout.xpm3"
#include "bitmaps/building.xpm3"

EXPORT void CompoundCustMgmLoad( void )
{
	int inx;
	turnoutInfo_t * to;
	static wIcon_p turnoutI = NULL;
	static wIcon_p structI = NULL;

	if ( turnoutI == NULL ) {
		turnoutI = wIconCreatePixMap( turnout_xpm3[0] );
	}
	if ( structI == NULL ) {
		structI = wIconCreatePixMap( building_xpm3[0] );
	}

	for ( inx=0; inx<turnoutInfo_da.cnt; inx++ ) {
		to = turnoutInfo(inx);
		if (to->paramFileIndex == PARAM_CUSTOM && to->segCnt > 0) {
			CustMgmLoad( turnoutI, CompoundCustMgmProc, to );
		}
	}
	for ( inx=0; inx<structureInfo_da.cnt; inx++ ) {
		to = structureInfo(inx);
		if (to->paramFileIndex == PARAM_CUSTOM && to->segCnt > 0) {
			CustMgmLoad( structI, CompoundCustMgmProc, to );
		}
	}
}

/*****************************************************************************
 *
 * Utitlies
 *
 */

wIndex_t FindListItemByContext(
        wList_p listP,
        void * context )
{
	if ( listP == NULL ) {
		return -1;
	}
	if ( context == NULL ) {
		return -1;
	}
	for ( wIndex_t inx = 0; inx < wListGetCount( listP ); ++inx ) {
		void * itemContext = wListGetItemContext( listP, inx );
		if ( itemContext != NULL ) {
			if ( itemContext == context ) {
				return inx;
			}
		}
	}
	return -1;
}