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
|
/** \file paramfilesearch_ui.c
* Parameter File Search Dialog
*/
/* XTrackCAD - Model Railroad CAD
* Copyright (C) 2019 Martin Fischer
*
* 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 "custom.h"
#include "dynstring.h"
#include "param.h"
#include "include/partcatalog.h"
#include "paths.h"
#include "include/paramfilelist.h"
#include "include/paramfile.h"
#include "fileio.h"
#include "directory.h"
#include "misc2.h"
#include "layout.h"
static ParameterLib *trackLibrary; /**< Track Library */
static Catalog currentCat; /**< catalog being shown */
/* define the search / browse dialog */
static void SearchUiDefault(void);
static void SearchUiApply(wWin_p junk);
static void SearchUiSelectAll(void *junk);
static void SearchUiDoSearch(void *junk);
static void SearchUiClearFilter(void *ptr);
static long searchUiMode = 0;
static long searchFitMode = 0;
static paramListData_t searchUiListData = { 10, 370, 0 };
#define MAXQUERYLENGTH 250
static char searchUiQuery[MAXQUERYLENGTH];
static char * searchUiLabels[] = { N_("Show File Names"), NULL };
// Note these are defined in the same order as FIT_ANY, FIT_COMPATIBLE, FIT_EXACT
static char * searchFitLabels[] = { N_("Fit Any"), N_("Fit Compatible"), N_("Fit Exact"), NULL};
#define QUERYPROMPTSTRING "Enter at least one search word"
static paramData_t searchUiPLs[] = {
#define I_QUERYSTRING (0)
{ PD_STRING, searchUiQuery, "query", PDO_ENTER | PDO_NOPREF | PDO_STRINGLIMITLENGTH | PDO_DLGRESIZE, I2VP(340), "", 0, 0, MAXQUERYLENGTH-1 },
#define I_SEARCHBUTTON (1)
{ PD_BUTTON, SearchUiDoSearch, "find", PDO_DLGHORZ, 0, NULL, BO_ICON, NULL },
#define I_CLEARBUTTON (2)
{ PD_BUTTON, SearchUiClearFilter, "clearfilter", PDO_DLGHORZ, 0, NULL, BO_ICON, NULL },
#define I_FITRADIO (3)
{ PD_RADIO, &searchFitMode, "fit", PDO_NOPREF | PDO_DLGBOXEND, searchFitLabels, NULL, BC_HORZ|BC_NOBORDER },
#define I_MESSAGE (4)
{ PD_MESSAGE, N_(QUERYPROMPTSTRING), NULL, 0, I2VP(370) },
#define I_STATISTICS (5)
{ PD_MESSAGE, "", NULL, PDO_DLGBOXEND, I2VP(370) },
#define I_RESULTLIST (6)
{ PD_LIST, NULL, "inx", PDO_NOPREF | PDO_DLGRESIZE, &searchUiListData, NULL, BL_DUP|BL_SETSTAY|BL_MANY },
#define I_MODETOGGLE (7)
{ PD_TOGGLE, &searchUiMode, "mode", PDO_DLGBOXEND, searchUiLabels, NULL, BC_HORZ|BC_NOBORDER },
#define I_APPLYBUTTON (8)
{ PD_BUTTON, SearchUiApply, "apply", PDO_DLGCMDBUTTON, NULL, N_("Add") },
#define I_SELECTALLBUTTON (9)
{ PD_BUTTON, SearchUiSelectAll, "selectall", PDO_DLGCMDBUTTON, NULL, N_("Select all") },
};
#define SEARCHBUTTON ((wButton_p)searchUiPLs[I_SEARCHBUTTON].control)
#define CLEARBUTTON ((wButton_p)searchUiPLs[I_CLEARBUTTON].control)
#define RESULTLIST ((wList_p)searchUiPLs[I_RESULTLIST].control)
#define APPLYBUTTON ((wButton_p)searchUiPLs[I_APPLYBUTTON].control)
#define SELECTALLBUTTON ((wButton_p)searchUiPLs[I_SELECTALLBUTTON].control)
#define MESSAGETEXT ((wMessage_p)searchUiPLs[I_MESSAGE].control)
#define QUERYSTRING ((wString_p)searchUiPLs[I_QUERYSTRING].control)
#define SEARCHSTAT ((wMessage_p)searchUiPLs[I_STATISTICS].control)
#define FITRADIO ((wChoice_p)searchUiPLs[I_FITRADIO].control)
static paramGroup_t searchUiPG = { "searchgui", 0, searchUiPLs, COUNT( searchUiPLs ) };
static wWin_p searchUiW;
#define FILESECTION "file"
#define PARAMDIRECTORY "paramdir"
/**
* Clears the current catalog
*/
void
ClearCurrentCatalog(void)
{
if (currentCat.head) {
DestroyCatalog(¤tCat);
currentCat.head = NULL;
}
}
/**
* Reload the listbox showing the current catalog. The catalog is either the system
* default library catalog or a search result
*
* \param [in] catalog the current catalog.
*/
static
int SearchFileListLoad(Catalog *catalog)
{
CatalogEntry *head = catalog->head;
CatalogEntry *catalogEntry;
DynString description;
DynStringMalloc(&description, STR_SHORT_SIZE);
wControlShow((wControl_p)RESULTLIST, FALSE);
wListClear(RESULTLIST);
DL_FOREACH(head, catalogEntry) {
for (unsigned int i=0; i<catalogEntry->files; i++) {
if (catalogEntry->tag && searchFitMode != 0) {
char * type_copy = MyStrdup(catalogEntry->tag);
char * cp = type_copy;
char * type = strtok(cp, " \t");
SCALE_FIT_TYPE_T fit_type;
if (strcmp(type,TURNOUTCOMMAND) == 0)
fit_type = FIT_TURNOUT;
else if (strcmp(type,STRUCTURECOMMAND)==0)
fit_type = FIT_STRUCTURE;
else if ((strcmp(type,CARCOMMAND)==0) || (strcmp(type,CARPROTOCOMMAND)==0))
fit_type = FIT_CAR;
char * scale = strtok(NULL, " \t\n");
if (scale) {
SCALEINX_T scale1 = LookupScale(scale);
SCALEINX_T scale2 = GetLayoutCurScale();
if (searchFitMode == FIT_COMPATIBLE) {
if (CompatibleScale(fit_type,scale1,scale2)<FIT_COMPATIBLE) continue;
} else {
if (CompatibleScale(fit_type,scale1,scale2)<FIT_EXACT) continue;
}
}
MyFree(type_copy);
}
DynStringClear(&description);
DynStringCatCStr(&description,
((!searchUiMode) && catalogEntry->contents) ?
catalogEntry->contents :
catalogEntry->fullFileName[i]);
wListAddValue(RESULTLIST,
DynStringToCStr(&description),
NULL,
catalogEntry->fullFileName[i]);
}
}
wControlShow((wControl_p)RESULTLIST, TRUE);
wControlActive((wControl_p)SELECTALLBUTTON,
wListGetCount(RESULTLIST));
DynStringFree(&description);
return wListGetCount(RESULTLIST);
}
/**
* Reload just the system files into the searchable set
*/
static void SearchUiDefault(void)
{
DynString dsSummary;
int matches = SearchFileListLoad(trackLibrary->catalog); //Start with system files
wStringSetValue(QUERYSTRING, "");
wMessageSetValue(MESSAGETEXT, _(QUERYPROMPTSTRING));
DynStringMalloc(&dsSummary, 16);
DynStringPrintf(&dsSummary, _("%u parameter files in library. %d Fit Scale."), CountCatalogEntries(trackLibrary->catalog), matches);
wMessageSetValue(SEARCHSTAT, DynStringToCStr(&dsSummary));
DynStringFree(&dsSummary);
wControlActive((wControl_p)CLEARBUTTON, FALSE);
}
/**
* Load the selected items of search results
*/
void static
SearchUILoadResults(void)
{
int files = wListGetSelectedCount(RESULTLIST);
if (files) {
char **fileNames;
int found = 0;
fileNames = MyMalloc(sizeof(char *)*files);
if (!fileNames) {
AbortProg("Couldn't allocate memory for result list: %s (%d)", __FILE__,
__LINE__, NULL);
}
for (int inx = 0; found < files; inx++) {
if (wListGetItemSelected(RESULTLIST, inx)) {
fileNames[found++] = (char *)wListGetItemContext(RESULTLIST, inx);
}
}
LoadParamFile(files, fileNames, NULL);
MyFree(fileNames);
SearchUiOk(NULL);
}
}
/**
* Update the action buttons.
*
* If there is at least one selected file, Apply is enabled
* If there are entries in the list, Select All is enabled
*
* \return
*/
static void UpdateSearchUiButton(void)
{
wIndex_t selCnt = wListGetSelectedCount(RESULTLIST);
wIndex_t cnt = wListGetCount(RESULTLIST);
wControlActive((wControl_p)APPLYBUTTON, selCnt > 0);
wControlActive((wControl_p)SELECTALLBUTTON, cnt > 0);
}
/**
* Return a pointer to the (shifted) trimmed string
*
* \param [in,out] s If non-null, a char to process.
*
* \returns pointer to the trimmed string
*/
char * StringTrim(char *s)
{
char *original = s;
size_t len = 0;
while (isspace((unsigned char) *s)) {
s++;
}
if (*s) {
char *p = s;
while (*p) {
p++;
}
while (isspace((unsigned char) *(--p)));
p[1] = '\0';
len = (size_t)(p - s + 1);
}
return (s == original) ? s : memmove(original, s, len + 1);
}
/**
* Perform the search. If successful, the results are loaded into the list
*
* \param [in,out] ptr ignored.
*/
static void SearchUiDoSearch(void * ptr)
{
unsigned result;
SearchResult *currentResults = MyMalloc(sizeof(SearchResult));
char * search;
ClearCurrentCatalog();
strcpy(searchUiQuery, wStringGetValue((wString_p)searchUiPG.paramPtr[I_QUERYSTRING].control));
search = StringTrim(searchUiQuery);
if (search[0]) {
result = SearchLibrary(trackLibrary, search, currentResults);
if (result) {
char *statistics;
statistics = SearchStatistics(currentResults);
wMessageSetValue(SEARCHSTAT, statistics);
MyFree(statistics);
int matches = SearchFileListLoad(&(currentResults->subCatalog));
DynString hitsMessage;
DynStringMalloc(&hitsMessage, 16);
DynStringPrintf(&hitsMessage, _("%d parameter files found. %d Fit Scale"), result, matches);
wMessageSetValue(MESSAGETEXT, DynStringToCStr(&hitsMessage));
DynStringFree(&hitsMessage);
currentCat = currentResults->subCatalog;
wControlActive((wControl_p)CLEARBUTTON, TRUE);
} else {
wListClear(RESULTLIST);
wControlActive((wControl_p)SELECTALLBUTTON, FALSE);
wMessageSetValue(MESSAGETEXT, _("No matches found."));
}
} else {
SearchUiDefault();
}
MyFree(currentResults); //Because SearchFileList also caches the currentResults->subCatalog address as currentCatalog for reuse.
}
/**
* Clear the current filter
*
* \param [in,out] ptr ignored
*/
static void
SearchUiClearFilter(void *ptr)
{
ClearCurrentCatalog();
SearchUiDefault();
}
/**
* Select all files in the list
*
* \param junk IN ignored
* \return
*/
static void SearchUiSelectAll(void *junk)
{
wListSelectAll(RESULTLIST);
}
/**
* Action handler for Done button. Hides the dialog.
*
* \param [in,out] junk ignored.
*/
void SearchUiOk(void * junk)
{
if (searchUiW) {
wHide(searchUiW);
}
}
/**
* Handle the Add button: a list of selected list elements is created and
* passed to the parameter file list.
*
* \param junk IN/OUT ignored
*/
static void SearchUiApply(wWin_p junk)
{
SearchUILoadResults();
}
/**
* Event handling for the Search dialog. If the 'X' decoration is pressed the
* dialog window is closed.
*
* \param pg IN ignored
* \param inx IN ignored
* \param valueP IN ignored
*/
static void SearchUiDlgUpdate(
paramGroup_p pg,
int inx,
void * valueP)
{
switch (inx) {
case I_QUERYSTRING:
if (pg->paramPtr[inx].enter_pressed) {
strcpy( searchUiQuery, wStringGetValue((wString_p)pg->paramPtr[inx].control) );
SearchUiDoSearch(NULL);
}
break;
case I_RESULTLIST:
UpdateSearchUiButton();
break;
case I_FITRADIO:
strcpy( searchUiQuery, wStringGetValue((wString_p)pg->paramPtr[I_QUERYSTRING].control) );
SearchUiDoSearch(NULL);
break;
case I_MODETOGGLE:
if (currentCat.head) {
SearchFileListLoad(¤tCat);
} else {
SearchFileListLoad(trackLibrary->catalog);
}
break;
case -1:
SearchUiOk(valueP);
break;
}
}
void
SearchUiCancel(wWin_p window)
{
ClearCurrentCatalog();
wHide(window);
}
/**
* Get the system default directory for parameter files. First step is to
* check the configuration file for a user specific setting. If that is not
* found, the diretory is based derived from the installation directory.
* The returned string has to be free'd() when no longer needed.
*
* \return parameter file directory
*/
static char *
GetParamsPath()
{
char * params_path;
char *params_pref;
params_pref = wPrefGetString(FILESECTION, PARAMDIRECTORY);
if (!params_pref) {
MakeFullpath(¶ms_path, wGetAppLibDir(), "params", NULL);
} else {
params_path = strdup(params_pref);
}
return (params_path);
}
#include "bitmaps/funnel.xpm"
#include "bitmaps/funnelclear.xpm"
/**
* Create and open the search dialog.
*
* \param junk
*/
void DoSearchParams(void * junk)
{
if (searchUiW == NULL) {
//Make the Find menu bound to the System Library initially
char *paramsDir = GetParamsPath();
trackLibrary = CreateLibrary(paramsDir);
free(paramsDir);
searchUiPLs[I_SEARCHBUTTON].winLabel = (char *)wIconCreatePixMap(funnel_xpm);
searchUiPLs[I_CLEARBUTTON].winLabel = (char *)wIconCreatePixMap(
funnelclear_xpm);
searchFitMode = FIT_COMPATIBLE; //Default to "Any" after startup
ParamRegister(&searchUiPG);
searchUiW = ParamCreateDialog(&searchUiPG,
MakeWindowTitle(_("Choose parameter files")), _("Done"), NULL, SearchUiCancel,
TRUE, NULL, F_RESIZE | F_RECALLSIZE, SearchUiDlgUpdate);
wControlActive((wControl_p)APPLYBUTTON, FALSE);
wControlActive((wControl_p)SELECTALLBUTTON, FALSE);
}
wControlActive((wControl_p)FITRADIO, TRUE);
ParamLoadControls(&searchUiPG);
ParamGroupRecord(&searchUiPG);
if (!trackLibrary) {
wControlActive((wControl_p)SEARCHBUTTON, FALSE);
wControlActive((wControl_p)QUERYSTRING, FALSE);
wMessageSetValue(MESSAGETEXT,
_("No system parameter files found, search is disabled."));
} else {
SearchUiDefault();
}
wShow(searchUiW);
}
|