forked from ennorehling/csmapfx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchdlg.cpp
603 lines (477 loc) · 18 KB
/
searchdlg.cpp
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
#include "version.h"
#include "main.h"
#include "fxhelper.h"
#include "searchdlg.h"
#include "symbols.h"
#include <boost/function.hpp>
#include <boost/tuple/tuple.hpp>
// *********************************************************************************************************
// *** FXMessages implementation
FXDEFMAP(FXSearchDlg) MessageMap[]=
{
//________Message_Type_____________________ID_______________Message_Handler_______
FXMAPFUNC(SEL_COMMAND, FXSearchDlg::ID_CLOSE, FXSearchDlg::onCmdHide),
FXMAPFUNC(SEL_FOCUSIN, 0, FXSearchDlg::onFocusIn),
FXMAPFUNC(SEL_COMMAND, FXSearchDlg::ID_SEARCH, FXSearchDlg::onSearch),
FXMAPFUNC(SEL_CHANGED, FXSearchDlg::ID_SEARCH, FXSearchDlg::onChangedSearch),
FXMAPFUNC(SEL_UPDATE, FXSearchDlg::ID_SEARCH, FXSearchDlg::onUpdateSearch),
FXMAPFUNC(SEL_COMMAND, FXSearchDlg::ID_RESULTS, FXSearchDlg::onSelectResults),
};
FXIMPLEMENT(FXSearchDlg,FXDialogBox,MessageMap, ARRAYNUMBER(MessageMap))
FXSearchDlg::FXSearchDlg(FXWindow* owner, const FXString& name, FXIcon* icon, FXuint opts, FXint x,FXint y,FXint w,FXint h)
: FXDialogBox(owner, name, opts, x,y,w,h, 10,10,10,10, 10,10), modifiedText(false)
{
setIcon(icon);
// buttons on bottom
FXHorizontalFrame* buttons = new FXHorizontalFrame(this, LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X|PACK_UNIFORM_HEIGHT, 0,0,0,0, 0,0,0,0);
new FXButton(buttons, "S&chliessen", NULL, this,ID_CLOSE, BUTTON_DEFAULT|FRAME_RAISED|LAYOUT_FILL_Y|LAYOUT_RIGHT);
info_text = new FXLabel(buttons, "", NULL, LABEL_NORMAL);
new FXHorizontalSeparator(this, SEPARATOR_GROOVE|LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X);
// create layout
FXVerticalFrame* content = new FXVerticalFrame(this, LAYOUT_FILL_X|LAYOUT_FILL_Y, 0,0,0,0, 0,0,0,0);
// search field
new FXLabel(content, "&Suche nach:", NULL, LABEL_NORMAL);
FXHorizontalFrame* search_line = new FXHorizontalFrame(content, LAYOUT_FILL_X|PACK_UNIFORM_HEIGHT, 0,0,0,0, 0,0,0,0);
search = new FXTextField(search_line, 12, this,ID_SEARCH, FRAME_LINE|TEXTFIELD_ENTER_ONLY|LAYOUT_FILL_X);
search->setBorderColor(getApp()->getShadowColor());
search_button = new FXButton(search_line, "Suchen", NULL, this,ID_SEARCH, BUTTON_DEFAULT|FRAME_RAISED);
// search mode: Ignore case, regular expression matching
FXHorizontalFrame* mode_frame = new FXHorizontalFrame(content, LAYOUT_FILL_X|PACK_UNIFORM_HEIGHT, 0,0,0,0, 0,0,0,0);
options.regardcase = new FXCheckButton(mode_frame, FXString(L"&Gro\u00df-/Kleinschreibung beachten"), this,ID_SEARCH, CHECKBUTTON_NORMAL);
options.regexp = new FXCheckButton(mode_frame, FXString(L"&Regul\u00e4rer Ausdruck"), this,ID_SEARCH, CHECKBUTTON_NORMAL);
// search domain: Search for what type, search in descriptions
FXHorizontalFrame* domain_frame = new FXHorizontalFrame(content, LAYOUT_FILL_X|PACK_UNIFORM_HEIGHT, 0,0,0,0, 0,0,0,0);
options.descriptions = new FXCheckButton(domain_frame, "&Beschreibungen", this,ID_SEARCH, CHECKBUTTON_NORMAL);
new FXVerticalSeparator(domain_frame, SEPARATOR_GROOVE|LAYOUT_FILL_Y);
new FXLabel(domain_frame, "&Durchsuche", NULL, LABEL_NORMAL);
FXListBox *box = options.domain = new FXListBox(domain_frame, this,ID_SEARCH, LISTBOX_NORMAL|LAYOUT_FILL_X|FRAME_LINE);
box->setBorderColor(getApp()->getShadowColor());
box->appendItem("Alles");
box->appendItem("Regionen");
box->appendItem("Einheiten");
box->appendItem(FXString(L"Geb\u00e4ude"));
box->appendItem("Schiffe");
box->appendItem("Befehle");
box->setNumVisible(box->getNumItems());
// detail options
FXHorizontalFrame *details = new FXHorizontalFrame(content, LAYOUT_FILL_X, 0,0,0,0, 0,0,0,0, 0,0);
detailsTab = new FXToolBarTab(details, NULL,0, TOOLBARTAB_HORIZONTAL, 0,0,0,0);
detailsTab->setTipText("Weitere Optionen ein- und ausblenden");
FXVerticalFrame *details_frame = new FXVerticalFrame(details, LAYOUT_FILL_X|FRAME_LINE, 0,0,0,0);
details_frame->setBorderColor(getApp()->getShadowColor());
options.searchdirectly = new FXCheckButton(details_frame, "Bei Texteingabe suchen", NULL,0, CHECKBUTTON_NORMAL);
options.limitresults = new FXCheckButton(details_frame, FXString(L"Auf 1000 Treffer beschr\u00e4nken"), this,ID_SEARCH, CHECKBUTTON_NORMAL);
// results list
new FXHorizontalSeparator(content, SEPARATOR_GROOVE|LAYOUT_FILL_X, 0,0,0,0, 0,0,DEFAULT_SPACING,DEFAULT_SPACING);
FXHorizontalFrame *list_frame = new FXHorizontalFrame(content, LAYOUT_FILL_X|LAYOUT_FILL_Y|FRAME_LINE, 0,0,0,0, 0,0,0,0);
list_frame->setBorderColor(getApp()->getShadowColor());
results = new FXFoldingList(list_frame, this,ID_RESULTS, FOLDINGLIST_SINGLESELECT|LAYOUT_FILL_X|LAYOUT_FILL_Y);
results->getHeader()->setHeaderStyle(HEADER_RESIZE|HEADER_TRACKING);
results->appendHeader("Region");
results->appendHeader(FXString(L"Einheit/Geb\u00e4ude/Schiff"));
}
void FXSearchDlg::create()
{
FXDialogBox::create();
// resize table headers
int w = (getWidth() - getPadLeft() - getPadRight() - 20) / results->getNumHeaders();
for (int i = 0; i < results->getNumHeaders(); i++)
results->setHeaderSize(i, w);
}
FXSearchDlg::~FXSearchDlg()
{
}
void FXSearchDlg::mapfiles(std::list<datafile> *f)
{
files = f;
}
void FXSearchDlg::loadState(FXRegistry& reg)
{
// default size is 400x300.
width = getApp()->reg().readUnsignedEntry("SEARCHDLG", "WIDTH", 400);
height = getApp()->reg().readUnsignedEntry("SEARCHDLG", "HEIGHT", 300);
if (width < 0) width = 400;
if (height < 0) height = 300;
setWidth(width);
setHeight(height);
// load options
FXint regardcase = reg.readUnsignedEntry("SEARCHDLG", "REGARDCASE", 0);
FXint regexp = reg.readUnsignedEntry("SEARCHDLG", "REGEXP", 0);
FXint descriptions = reg.readUnsignedEntry("SEARCHDLG", "DESCRIPTIONS", 0);
options.regardcase->setCheck(regardcase!=0);
options.regexp->setCheck(regexp != 0);
options.descriptions->setCheck(descriptions != 0);
FXint domain = reg.readUnsignedEntry("SEARCHDLG", "DOMAIN", 0);
if (domain >= 0 && domain < options.domain->getNumItems())
options.domain->setCurrentItem(domain);
FXint hideoptions = reg.readUnsignedEntry("SEARCHDLG", "HIDEOPTIONS", 1);
detailsTab->collapse(hideoptions != 0);
FXint searchdirectly = reg.readUnsignedEntry("SEARCHDLG", "SEARCHDIRECTLY", 1);
FXint limitresults = reg.readUnsignedEntry("SEARCHDLG", "LIMITRESULTS", 1);
options.searchdirectly->setCheck(searchdirectly != 0);
options.limitresults->setCheck(limitresults != 0);
}
void FXSearchDlg::saveState(FXRegistry& reg)
{
// save window size
if (!isMinimized())
{
reg.writeUnsignedEntry("SEARCHDLG", "WIDTH", getWidth());
reg.writeUnsignedEntry("SEARCHDLG", "HEIGHT", getHeight());
}
// save options
reg.writeUnsignedEntry("SEARCHDLG", "REGARDCASE", options.regardcase->getCheck());
reg.writeUnsignedEntry("SEARCHDLG", "REGEXP", options.regexp->getCheck());
reg.writeUnsignedEntry("SEARCHDLG", "DESCRIPTIONS", options.descriptions->getCheck());
reg.writeUnsignedEntry("SEARCHDLG", "DOMAIN", options.domain->getCurrentItem());
reg.writeUnsignedEntry("SEARCHDLG", "HIDEOPTIONS", detailsTab->isCollapsed());
reg.writeUnsignedEntry("SEARCHDLG", "SEARCHDIRECTLY", options.searchdirectly->getCheck());
reg.writeUnsignedEntry("SEARCHDLG", "LIMITRESULTS", options.limitresults->getCheck());
}
long FXSearchDlg::onFocusIn(FXObject* sender, FXSelector sel, void* ptr)
{
if (!search->hasFocus())
{
search->setFocus();
search->selectAll();
}
return FXDialogBox::onFocusIn(sender, sel, ptr);;
}
// search functors
namespace
{
// compare functions
typedef boost::function<bool(const FXString&)> compare_func_t;
class compare_normal
{
public:
compare_normal() {}
compare_normal(const FXString& needle_) : needle(needle_) { needle.upper(); }
bool operator()(const FXString& str) const
{
return FXString(str).upper().find(needle) != -1;
}
protected:
FXString needle;
};
class compare_regardcase
{
public:
compare_regardcase() {}
compare_regardcase(const FXString& needle_) : needle(needle_) {}
bool operator()(const FXString& str) const
{
return str.find(needle) != -1;
}
protected:
FXString needle;
};
class compare_regexp_normal
{
public:
compare_regexp_normal() {}
compare_regexp_normal(const FXString& needle_) : compare(needle_, REX_ICASE) {}
bool operator()(const FXString& str) const
{
return compare.match(str) == TRUE;
}
protected:
FXRex compare;
};
class compare_regexp_regardcase
{
public:
compare_regexp_regardcase() {}
compare_regexp_regardcase(const FXString& needle_) : compare(needle_, REX_NORMAL) {}
bool operator()(const FXString& str) const
{
return compare.match(str) == TRUE;
}
protected:
FXRex compare;
};
// search functions
typedef const datablock::itor& itor_ref;
typedef boost::tuple<itor_ref, itor_ref, itor_ref, itor_ref, itor_ref, const compare_func_t&, const compare_func_t&, bool> block_context;
// context(search_string, region, building, ship, unit, end, compare_func, compare_func_icase, descriptions);
typedef boost::function<bool(const datablock::itor&, const block_context&)> search_func_t;
bool search_region(const datablock::itor& block, const block_context& context)
{
if (block->type() != datablock::TYPE_REGION)
return false;
const compare_func_t& compare = context.get<5>();
FXString name = block->value(datakey::TYPE_NAME);
if (name.empty())
name = block->terrainString();
if (name.empty())
name = "Unbekannt";
name += " (" + FXStringVal(block->x()) + "," + FXStringVal(block->y());
if (block->info())
name += "," + datablock::planeName(block->info());
name += ")";
if (compare(name))
return true;
if (context.get<7>()) // compare descriptions?
return compare(block->value(datakey::TYPE_DESCRIPTION));
return false;
}
bool search_unit(const datablock::itor& block, const block_context& context)
{
if (block->type() != datablock::TYPE_UNIT)
return false;
const compare_func_t& compare = context.get<5>();
const compare_func_t& compare_icase = context.get<6>();
if (compare(block->value(datakey::TYPE_NAME)) || compare_icase(block->id()))
return true;
if (context.get<7>()) // compare descriptions?
return compare(block->value(datakey::TYPE_DESCRIPTION));
return false;
}
bool search_building(const datablock::itor& block, const block_context& context)
{
if (block->type() != datablock::TYPE_BUILDING)
return false;
const compare_func_t& compare = context.get<5>();
const compare_func_t& compare_icase = context.get<6>();
if (compare(block->value(datakey::TYPE_NAME)) || compare_icase(block->id()))
return true;
if (context.get<7>()) // compare descriptions?
return compare(block->value(datakey::TYPE_DESCRIPTION));
return false;
}
bool search_ship(const datablock::itor& block, const block_context& context)
{
if (block->type() != datablock::TYPE_SHIP)
return false;
const compare_func_t& compare = context.get<5>();
const compare_func_t& compare_icase = context.get<6>();
if (compare(block->value(datakey::TYPE_NAME)) || compare_icase(block->id()))
return true;
if (context.get<7>()) // compare descriptions?
return compare(block->value(datakey::TYPE_DESCRIPTION));
return false;
}
bool search_commands(const datablock::itor& block, const block_context& context)
{
if (block->type() != datablock::TYPE_COMMANDS)
return false;
const compare_func_t& compare = context.get<5>();
// search if a command matchs
if (att_commands* cmds = dynamic_cast<att_commands*>(block->attachment()))
{
// search in the changed command list
att_commands::cmdlist_t::iterator end = cmds->commands.end();
for (att_commands::cmdlist_t::iterator itor = cmds->commands.begin(); itor != end; itor++)
{
if (compare(*itor))
return true;
}
}
else
{
// search in default command list
datakey::itor end = block->data().end();
for (datakey::itor itor = block->data().begin(); itor != end; itor++)
{
if (compare(itor->value()))
return true;
}
}
return false;
}
bool search_all(const datablock::itor& block, const block_context& context)
{
return search_region(block, context) || search_unit(block, context) || search_building(block, context)
|| search_ship(block, context) || search_commands(block, context);
}
}
long FXSearchDlg::onChangedSearch(FXObject* sender, FXSelector sel, void* ptr)
{
if (options.searchdirectly->getCheck())
modifiedText = true;
return 0;
}
long FXSearchDlg::onUpdateSearch(FXObject* sender, FXSelector sel, void* ptr)
{
if (modifiedText)
{
modifiedText = false;
return onSearch(sender, sel, ptr);
}
return 0;
}
long FXSearchDlg::onSearch(FXObject*, FXSelector sel, void*)
{
if (!files)
return 0;
// clear old results
results->clearItems();
info_text->setText("");
// get search string
FXString str = search->getText().trim();
if (str.empty())
return 1;
// search options
bool regardcase = options.regardcase->getCheck() == TRUE; // ignore case or don't ignore case
bool regexp = options.regexp->getCheck() == TRUE; // use regular expressions
bool descriptions = options.descriptions->getCheck() == TRUE; // search also in description texts
bool limitresults = options.limitresults->getCheck() == TRUE; // stop after 1000 results
compare_func_t compare_func_icase = compare_normal(str); // ignores case (for base36 numbers)
compare_func_t compare_func = compare_normal(str); // does ignores case if not other specified
if (regardcase)
compare_func = compare_regardcase(str);
if (regexp)
compare_func_icase = compare_regexp_normal(str),
compare_func = compare_regexp_normal(str);
if (regexp && regardcase)
compare_func = compare_regexp_regardcase(str);
int domain = options.domain->getCurrentItem();
search_func_t search_func;
if (domain == 0)
search_func = search_all;
else if (domain == 1)
search_func = search_region;
else if (domain == 2)
search_func = search_unit;
else if (domain == 3)
search_func = search_building;
else if (domain == 4)
search_func = search_ship;
else if (domain == 5)
search_func = search_commands;
else
{
FXMessageBox::error(this, MBOX_OK, CSMAP_APP_TITLE, "FXSearchDlg::onSearch Fehler: domain = %d.", domain);
return 0;
}
// do the search
for (datafile::itor file = files->begin(); file != files->end(); file++)
{
datablock::itor end = file->end();
datablock::itor region = end, // initialize last-block-of-typ holder
building = end,
ship = end,
unit = end;
block_context context(region, building, ship, unit, end, compare_func, compare_func_icase, descriptions);
for (datablock::itor block = file->blocks().begin(); block != end; block++)
{
// pass blocks to search functors
if (block->type() == datablock::TYPE_REGION)
{
region = block;
building = ship = unit = end;
}
else if (block->type() == datablock::TYPE_BUILDING)
{
building = block;
ship = unit = end;
}
else if (block->type() == datablock::TYPE_SHIP)
{
ship = block;
building = unit = end;
}
else if (block->type() == datablock::TYPE_UNIT)
{
unit = block;
building = ship = end;
}
if (search_func(block, context))
{
FXString region_str, object_str;
// add region to results-list in first column
if (region != end)
{
FXString terrain = region->terrainString();
FXString name = region->value(datakey::TYPE_NAME);
if (name.empty())
name = terrain;
if (name.empty())
name = "Unbekannt";
if (region->info())
region_str.format("%s (%d,%d,%s)", name.text(), region->x(), region->y(), datablock::planeName(region->info()).text());
else
region_str.format("%s (%d,%d)", name.text(), region->x(), region->y());
}
// add block to results-list in second column
if (building != end)
{
FXString name = building->value(datakey::TYPE_NAME);
FXString id = building->id();
if (name.empty())
name = FXString(L"Geb\u00e4ude ") + id;
object_str = name + " (" + id + ")";
}
else if (ship != end)
{
FXString name = ship->value(datakey::TYPE_NAME);
FXString id = ship->id();
if (name.empty())
name = "Schiff " + id;
object_str = name + " (" + id + ")";
}
else if (unit != end)
{
FXString name = unit->value(datakey::TYPE_NAME);
FXString id = unit->id();
//FXString number = block->value(datakey::TYPE_NUMBER);
//FXString type = block->value(datakey::TYPE_TYPE);
if (name.empty())
name = "Einheit " + id;
object_str = name + " (" + id + ")";
}
// add to list
datablock::itor link = region;
if (unit != end)
link = unit;
else if (building != end)
link = building;
else if (ship != end)
link = ship;
results->appendItem(NULL, region_str + "\t" + object_str, NULL,NULL, (void*)&*link);
if (limitresults && results->getNumItems() >= 1000)
break; // list only 1000 results
}
}
}
if (limitresults && results->getNumItems() >= 1000)
info_text->setText("Nur die ersten " + FXStringVal(results->getNumItems()) + " Treffer werden angezeigt.");
else
info_text->setText(FXStringVal(results->getNumItems()) + " Treffer");
return 1;
}
long FXSearchDlg::onSelectResults(FXObject*, FXSelector, void*)
{
if (!files)
return 0;
FXFoldingItem *item = results->getCurrentItem();
if (!item)
return 0;
datablock* select = (datablock*)item->getData();
for (datafile::itor file = files->begin(); file != files->end(); file++)
{
datablock::itor end = file->blocks().end();
datablock::itor region = end;
for (datablock::itor block = file->blocks().begin(); block != end; block++)
{
if (block->type() == datablock::TYPE_REGION)
region = block;
if (select != &*block)
continue;
// propagate selection
datafile::SelectionState state;
state.selected = 0;
if (region != end)
state.region = region, state.selected |= state.REGION;
if (block->type() == datablock::TYPE_UNIT)
state.unit = block, state.selected |= state.UNIT;
else if (block->type() == datablock::TYPE_BUILDING)
state.building = block, state.selected |= state.BUILDING;
else if (block->type() == datablock::TYPE_SHIP)
state.ship = block, state.selected |= state.SHIP;
getOwner()->handle(this, FXSEL(SEL_COMMAND, ID_UPDATE), &state);
return 1;
}
}
return 1;
}