summaryrefslogtreecommitdiff
path: root/app/bin/dcontmgm.c
blob: 45fec890c709930065dc49bcfe3a0c687fcca470 (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
/** \file dcontmgm.c
 * Manage layout control elements
 */

/* -*- C -*- ****************************************************************
 *
 *  System        : 
 *  Module        : 
 *  Created By    : Robert Heller
 *  Created       : Thu Jan 5 10:52:12 2017
 *  Last Modified : <170411.1447>
 *
 *  Description
 * 
 *     Control Element Mangment.  Control Elements are elements related to 
 *     layout control: Blocks (occupency detection), Switchmotors (actuators
 *     to "throw" turnouts), and (eventually) signals.  These elements don't
 *     relate to "physical" items on the layout, but instead refer to the
 *     elements used by the layout control software.  These elements contain
 *     "scripts", which are really just textual items that provide information
 *     for the layout control software and provide a bridge between physical 
 *     layout elements (like tracks or turnouts) and the layout control 
 *     software.  These textual items could be actual software code or could
 *     be LCC Events (for I/O device elements on a LCC network) or DCC 
 *     addresses for stationary decoders, etc.  XTrkCAD does not impose any
 *     sort of syntax or format -- that is left up to other software that might
 *     load and parse the XTrkCAD layout file.  All the XTrkCAD does is provide
 *     a unified place for this information to be stored and to provide a
 *     mapping (association) between this control information and the layout
 *     itself.
 * 
 *
 *  Notes
 *
 *  History
 *	
 ****************************************************************************
 *
 *    Copyright (C) 2017  Robert Heller D/B/A Deepwoods Software
 *			51 Locke Hill Road
 *			Wendell, MA 01379-9728
 *
 *    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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * 
 *
 ****************************************************************************/

static const char rcsid[] = "@(#) : $Id$";



#include "track.h"
#include <errno.h>
#include "i18n.h"

#ifdef WINDOWS
#include <io.h>
#define F_OK	(0)
#define W_OK	(2)
#define access	_access
#endif

/*****************************************************************************
 *
 * Control List Management
 *
 */

static void ControlEdit( void * action );
static void ControlDelete( void * action );
static void ControlDone( void * action );
static wPos_t controlListWidths[] = { 18, 100, 150 };
static const char * controlListTitles[] = { "", N_("Name"),
	N_("Tracks") };
static paramListData_t controlListData = { 10, 400, 3, controlListWidths, controlListTitles };
static paramData_t controlPLs[] = {
#define I_CONTROLLIST	(0)
#define controlSelL		((wList_p)controlPLs[I_CONTROLLIST].control)
	{	PD_LIST, NULL, "inx", PDO_DLGRESETMARGIN|PDO_DLGRESIZE, &controlListData, NULL, BL_MANY },
#define I_CONTROLEDIT	(1)
	{	PD_BUTTON, (void*)ControlEdit, "edit", PDO_DLGCMDBUTTON, NULL, N_("Edit") },
#define I_CONTROLDEL		(2)
    {	PD_BUTTON, (void*)ControlDelete, "delete", 0, NULL, N_("Delete") },
  } ;
static paramGroup_t controlPG = { "contmgm", 0, controlPLs, sizeof controlPLs/sizeof controlPLs[0] };


typedef struct {
		contMgmCallBack_p proc;
		void * data;
		wIcon_p icon;
		} contMgmContext_t, *contMgmContext_p;

static BOOL_T AnyHILIGHT = FALSE;

static void ControlDlgUpdate(
		paramGroup_p pg,
		int inx,
		void *valueP )
{
    contMgmContext_p context = NULL;
    wIndex_t selcnt = wListGetSelectedCount( (wList_p)controlPLs[0].control );
    wIndex_t linx, lcnt;

    if ( inx != I_CONTROLLIST ) return;
    lcnt = wListGetCount( (wList_p)controlPLs[0].control );
    AnyHILIGHT = FALSE;
    for (linx=0;
         linx < lcnt;
         linx++ ) {
        if (wListGetItemSelected( (wList_p)controlPLs[0].control, linx ) == TRUE) {
            context = (contMgmContext_p)wListGetItemContext( controlSelL, linx );
            context->proc( CONTMGM_DO_HILIGHT, context->data );
            AnyHILIGHT = TRUE;
        } else {
            context = (contMgmContext_p)wListGetItemContext( controlSelL, linx );
            context->proc( CONTMGM_UN_HILIGHT, context->data );
        }
    }
    ParamControlActive( &controlPG, I_CONTROLEDIT, selcnt>0 );
    ParamControlActive( &controlPG, I_CONTROLDEL, selcnt>0 );
}

static void ControlEdit( void * action )
{
	contMgmContext_p context = NULL;
	wIndex_t selcnt = wListGetSelectedCount( (wList_p)controlPLs[0].control );
	wIndex_t inx, cnt;

	if ( selcnt != 1 )
		return;
	cnt = wListGetCount( (wList_p)controlPLs[0].control );
	for ( inx=0;
		  inx<cnt && wListGetItemSelected( (wList_p)controlPLs[0].control, inx ) != TRUE;
		  inx++ );
	if ( inx >= cnt )
		return;
	context = (contMgmContext_p)wListGetItemContext( controlSelL, inx );
	if ( context == NULL )
		return;
	context->proc( CONTMGM_DO_EDIT, context->data );
	context->proc( CONTMGM_GET_TITLE, context->data );
	wListSetValues( controlSelL, inx, message, context->icon, context );
}


static void ControlDelete( void * action )
{
	wIndex_t selcnt = wListGetSelectedCount( (wList_p)controlPLs[0].control );
	wIndex_t inx, cnt;
	contMgmContext_p context = NULL;

	if ( selcnt <= 0 )
		return;
	if ( (!NoticeMessage2( 1, _("Are you sure you want to delete the %d control element(s)"), _("Yes"), _("No"), selcnt ) ) )
		return;
        cnt = wListGetCount( (wList_p)controlPLs[0].control );
        UndoStart( _("Control Elements"), "delete" );
	for ( inx=0; inx<cnt; inx++ ) {
		if ( !wListGetItemSelected( (wList_p)controlPLs[0].control, inx ) )
			continue;
		context = (contMgmContext_p)wListGetItemContext( controlSelL, inx );
		context->proc( CONTMGM_DO_DELETE, context->data );
		MyFree( context );
		wListDelete( controlSelL, inx );
		inx--;
		cnt--;
	}
        UndoEnd();
	DoChangeNotification( CHANGE_PARAMS );
}

static void ControlDone( void * action )
{
    contMgmContext_p context = NULL;
    wIndex_t linx, lcnt;
    
    if (AnyHILIGHT) {
        lcnt = wListGetCount( (wList_p)controlPLs[0].control );
        for (linx=0;
             linx < lcnt;
             linx++ ) {
            context = (contMgmContext_p)wListGetItemContext( controlSelL, linx );
            context->proc( CONTMGM_UN_HILIGHT, context->data );
        }
    }
    wHide( controlPG.win );
}


EXPORT void ContMgmLoad(
		wIcon_p icon,
		contMgmCallBack_p proc,
		void * data )
{
	contMgmContext_p context;
	context = MyMalloc( sizeof *context );
	context->proc = proc;
	context->data = data;
	context->icon = icon;
	context->proc( CONTMGM_GET_TITLE, context->data );
	wListAddValue( controlSelL, message, icon, context );
}


static void LoadControlMgmList( void )
{
	wIndex_t curInx, cnt=0;
	long tempL;
	contMgmContext_p context;
	contMgmContext_t curContext;

	curInx = wListGetIndex( controlSelL );
	curContext.proc = NULL;
	curContext.data = NULL;
	curContext.icon = NULL;
	if ( curInx >= 0 ) {
		context = (contMgmContext_p)wListGetItemContext( controlSelL, curInx );
		if ( context != NULL )
			curContext = *context;
	}
	cnt = wListGetCount( controlSelL );
	for ( curInx=0; curInx<cnt; curInx++ ) {
		context = (contMgmContext_p)wListGetItemContext( controlSelL, curInx );
		if ( context )
			MyFree( context );
	}
	curInx = wListGetIndex( controlSelL );
	wControlShow( (wControl_p)controlSelL, FALSE );
	wListClear( controlSelL );

	BlockMgmLoad();
        SwitchmotorMgmLoad();
        SignalMgmLoad();
        ControlMgmLoad();
        SensorMgmLoad();

#ifdef LATER
	curInx = 0;
	cnt = wListGetCount( controlSelL );
	if ( curContext.proc != NULL ) {
		for ( curInx=0; curInx<cnt; curInx++ ) {
			context = (contMgmContext_p)wListGetItemContext( controlSelL, curInx );
			if ( context &&
				 context->proc == curContext.proc &&
				 context->data == curContext.data )
				break;
		}
	}
	if ( curInx >= cnt )
		curInx = (cnt>0?0:-1);

	wListSetIndex( controlSelL, curInx );
	tempL = curInx;
#endif
	tempL = -1;
	ControlDlgUpdate( &controlPG, I_CONTROLLIST, &tempL );
	wControlShow( (wControl_p)controlSelL, TRUE );
}


static void ContMgmChange( long changes )
{
	if (changes) {
		if (changed) {
			changed = 1;
			checkPtMark = 1;
		}
	}
	if ((changes&CHANGE_PARAMS) == 0 ||
		controlPG.win == NULL || !wWinIsVisible(controlPG.win) )
		return;

	LoadControlMgmList();
}



static void DoControlMgr( void * junk )
{
    if (controlPG.win == NULL) {
        ParamCreateDialog( &controlPG, MakeWindowTitle(_("Manage Layout Control Elements")), _("Done"), ControlDone, NULL, TRUE, NULL, F_RESIZE|F_RECALLSIZE|F_BLOCK, ControlDlgUpdate );
    } else {
        wListClear( controlSelL );
    }
    /*ParamLoadControls( &controlPG );*/
    /*ParamGroupRecord( &controlPG );*/
    LoadControlMgmList();
    wShow( controlPG.win );
}


EXPORT addButtonCallBack_t ControlMgrInit( void )
{
    ParamRegister( &controlPG );
    /*ParamRegister( &contMgmContentsPG );*/
    RegisterChangeNotification( ContMgmChange );
    return &DoControlMgr;
}