File size: 110,559 Bytes
b6a38d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
local  warn_mark = Untranslated("<color 255 140 0>!</color>")
local   err_mark = Untranslated("<color 240 0 0>!!</color>")
local dirty_mark = Untranslated("<color 240 0 0>*</color>")
local match_mark_text = "<color 0 196 0>•</color>"
local match_mark = Untranslated(match_mark_text)

local function get_warning_msg(obj_addr)
	return (Platform.ged and g_GedApp.connection:Obj("root|warnings_cache") or rawget(_G, "DiagnosticMessagesForGed") or empty_table)[obj_addr]
end

function TFormat.ged_marks(context_obj, obj_addr)
	local marks = ""
	
	-- Ged warning/error ! mark
	local msg = get_warning_msg(obj_addr)
	if msg then
		marks = marks .. (msg[#msg] == "warning" and warn_mark or err_mark)
	end
	
	-- Ged dirty * mark
	local dirty = Platform.ged and g_GedApp.connection:Obj("root|dirty_objects") or empty_table
	if dirty[obj_addr] then
		marks = marks .. dirty_mark
	end
	
	-- Mark for a match from "search values"
	for _, panel in pairs(Platform.ged and g_GedApp.interactive_panels) do
		local results = panel.search_value_results
		if results and type(results[obj_addr]) == "table" then
			marks = marks .. match_mark
			break
		end
	end
	return marks
end

local read_only_color = Untranslated("<color 150 150 150>")
local read_only_color_close = Untranslated("</color>")

-- <read_only(panel.context)>
-- The read_only tag is used to gray out all items in a ged panel whose context is read-only.
-- Currently this is useful to gray out a Container's sub-items (like Preset sub-items).
-- If you want to gray out only specific items that read-only in a panel whose context is NOT read-only 
-- use the Format property with a construct like <if(IsReadOnly)>...</if>
function TFormat.read_only(context_obj, context_name)
	local result = ""

	local read_only = (Platform.ged and context_name) and g_GedApp.connection:Obj(context_name .. "|read_only")
	if read_only then
		result = result .. read_only_color
	end
	
	return result
end

-- </read_only(panel.context)>
-- Closes a read_only tag.
TFormat["/read_only"] = function(context_obj, context_name)
	local result = ""
	
	local read_only = (Platform.ged and context_name) and g_GedApp.connection:Obj(context_name .. "|read_only")
	if read_only then
		result = result .. read_only_color_close
	end
	
	return result
end


----- GedPanelBase, used by all panels and GedPropNestedList

local reCommaList = "([%w_]+)%s*,%s*"

DefineClass.GedPanelBase = {
	__parents = { "XControl", "XContextWindow" },
	properties = {
		{ category = "Interaction", id = "Collapsible", editor = "bool", default = false, help = "Allows the panel to be collapsed and expanded." },
		{ category = "Interaction", id = "StartsExpanded", editor = "bool", default = false, help = "Controls if the panel is initially collapsed or expanded." },
		{ category = "Interaction", id = "ExpandedMessage", editor = "text", default = "", help = "Dimmed help message to display right-aligned in the panel's title when it is expanded." },
		{ category = "Interaction", id = "EmptyMessage", editor = "text", default = "", help = "Dimmed help message to display right-aligned in the panel's title when it is empty." },
	},
	Embedded = false,
	Interactive = false, -- interactive panels have selection and store/restore state (see GedApp)
	MatchMark = match_mark_text,
	focus_column = 1,
	connection = false,
	app = false,
}

function GedPanelBase:Init(parent, context)
	assert(not context or type(context) == "string") -- context is the bind name of the object displayed
	self.app = GetParentOfKind(self.parent, "GedApp")
	self.connection = self.app and self.app.connection
	self.app:AddPanel(self.context, self)
end

function GedPanelBase:Done()
	if self.app.window_state ~= "destroying" then
		self:UnbindViews()
		self.app:RemovePanel(self)
	end
end

function GedPanelBase:SetPanelFocused()
end

function GedPanelBase:GetSelection()
	return false
end

function GedPanelBase:GetMultiSelection()
	return false
end

function GedPanelBase:SetSelection(...)
end

function GedPanelBase:OnSelection(selection)
end

function GedPanelBase:TryHighlightSearchMatch()
	-- no search support in GetPanelBase
end

function GedPanelBase:CancelSearch(dont_select)
	-- no search support in GetPanelBase
end

function GedPanelBase:GetState()
	return { selection = table.pack(self:GetSelection()) }
end

function GedPanelBase:Obj(name)
	return self.connection.bound_objects[name]
end

function GedPanelBase:BindView(suffix, func_name, ...)
	local name = self.context
	self.connection:BindObj(name .. "|" .. suffix, name, func_name, ...)
end

function GedPanelBase:UnbindView(suffix)
	local name = self.context
	self.connection:UnbindObj(name .. "|" .. suffix)
end

function GedPanelBase:BindViews()
end

function GedPanelBase:UnbindViews()
	local name = self.context
	if name then
		self.connection:UnbindObj(name)
		self.connection:UnbindObj(name .. "|", "") -- unbind all views
		self:SetContext(false)
	end
end

function GedPanelBase:OnContextUpdate(context, view)
	self:SetVisible(true)
	if view == nil then
		self:BindViews()
	end
	self.app:CheckUpdateItemTexts(view)
end

function GedPanelBase:OnSetFocus()
	if self.app:SetLastFocusedPanel(self) then
		self:BindSelectedObject(self:GetSelection())
	end
end

function GedPanelBase:BindSelectedObject(selected_item)
end

function GedPanelBase:Op(op_name, obj, ...)
	self.app:Op(op_name, obj, ...)
end

function GedPanelBase:Send(rfunc_name, ...)
	self.app:Send(rfunc_name, ...)
end

function GedPanelBase:UpdateItemTexts()
	-- used to refresh all texts so that their warning statuses get updated (see TFormat.diagnmsg)
end

local function get_warning_nodes(self)
	-- see Preset's Warning property
	local warning_data = self:Obj(self.context .. "|warning")
	if type(warning_data) == "table" then
		local warning_idxs = {}
		for i = 3, #warning_data do
			table.insert(warning_idxs, warning_data[i])
		end
		return warning_idxs
	end
	return empty_table
end


----- Ops for common actions per panel type and class

local common_action_ops = {
	GedListPanel = {
		None = {},
		PropertyObject = {
			MoveUp    = "GedOpListMoveUp",
			MoveDown  = "GedOpListMoveDown",
			Delete    = "GedOpListDeleteItem",
			Cut       = "GedOpListCut",
			Copy      = "GedOpListCopy",
			Paste     = "GedOpListPaste",
			Duplicate = "GedOpListDuplicate",
		},
		Object = {
			Delete    = "GedOpListDeleteItem",
			Cut       = "GedOpObjectCut",
			Copy      = "GedOpObjectCopy",
			Paste     = "GedOpObjectPaste",
			Duplicate = "GedOpObjectDuplicate",
		},
	},
	GedTreePanel = {
		None = {},
		PropertyObject = {
			MoveUp    = "GedOpTreeMoveItemUp",
			MoveDown  = "GedOpTreeMoveItemDown",
			MoveOut   = "GedOpTreeMoveItemOutwards",
			MoveIn    = "GedOpTreeMoveItemInwards",
			Delete    = "GedOpTreeDeleteItem",
			Cut       = "GedOpTreeCut",
			Copy      = "GedOpTreeCopy",
			Paste     = "GedOpTreePaste",
			Duplicate = "GedOpTreeDuplicate",
		},
		Preset = {
			Delete    = "GedOpPresetDelete",
			Cut       = "GedOpPresetCut",
			Copy      = "GedOpPresetCopy",
			Paste     = "GedOpPresetPaste",
			Duplicate = "GedOpPresetDuplicate",
		},
	},
	GedPropPanel = {
		None = {},
		PropertyObject = {
			Copy      = "GedOpPropertyCopy",
			Paste     = "GedOpPropertyPaste",
		},
	},
}


----- GedPanel

local function op_readonly(self, prop_meta) return self:GetProperty(prop_meta.id) == GedDisabledOp end
local function op_noedit(self, prop_meta) return not common_action_ops[rawget(self, "__class") or self.class] end
local op_edit_button = {{
	name = "Edit", func = "OpEdit",
	is_hidden = function(obj, prop_meta) return obj:GetProperty(prop_meta.id) ~= GedDisabledOp end
}}

DefineClass.GedPanel = {
	__parents = { "GedPanelBase" },
	properties = {
		{ category = "General", id = "Title", editor = "text", default = "<class>", },
		{ category = "General", id = "TitleFormatFunc", editor = "text", default = "GedFormatObject", },
		{ category = "General", id = "EnableSearch", editor = "bool", default = false, },
		{ category = "General", id = "SearchHistory", editor = "number", default = 0 },
		{ category = "General", id = "SearchValuesAvailable", editor = "bool", default = false },
		{ category = "General", id = "PersistentSearch", editor = "bool", default = false },
		{ category = "General", id = "Predicate", editor = "text", default = "", help = "This object member function controls whether the panel is visible" },
		{ category = "General", id = "DisplayWarnings", editor = "bool", default = true, },
		
		{ category = "Common Actions", id = "ActionsClass", editor = "choice", default = "None", no_edit = op_noedit,
		  items = function(self) return table.keys2(common_action_ops[rawget(self, "__class") or self.class] or { None = true }, "sorted") end },
		{ category = "Common Actions", id = "MoveUp",    editor = "text", default = GedDisabledOp, read_only = op_readonly, no_edit = op_noedit, buttons = op_edit_button },
		{ category = "Common Actions", id = "MoveDown",  editor = "text", default = GedDisabledOp, read_only = op_readonly, no_edit = op_noedit, buttons = op_edit_button },
		{ category = "Common Actions", id = "MoveOut",   editor = "text", default = GedDisabledOp, read_only = op_readonly, no_edit = op_noedit, buttons = op_edit_button },
		{ category = "Common Actions", id = "MoveIn",    editor = "text", default = GedDisabledOp, read_only = op_readonly, no_edit = op_noedit, buttons = op_edit_button },
		{ category = "Common Actions", id = "Delete",    editor = "text", default = GedDisabledOp, read_only = op_readonly, no_edit = op_noedit, buttons = op_edit_button },
		{ category = "Common Actions", id = "Cut",       editor = "text", default = GedDisabledOp, read_only = op_readonly, no_edit = op_noedit, buttons = op_edit_button },
		{ category = "Common Actions", id = "Copy",      editor = "text", default = GedDisabledOp, read_only = op_readonly, no_edit = op_noedit, buttons = op_edit_button },
		{ category = "Common Actions", id = "Paste",     editor = "text", default = GedDisabledOp, read_only = op_readonly, no_edit = op_noedit, buttons = op_edit_button },
		{ category = "Common Actions", id = "Duplicate", editor = "text", default = GedDisabledOp, read_only = op_readonly, no_edit = op_noedit, buttons = op_edit_button },
		
		{ category = "Context Menu", id = "ActionContext", editor = "text", default = "" },
		{ category = "Context Menu", id = "SearchActionContexts", editor = "string_list", default = false,},
	},
	IdNode = true,
	HAlign = "stretch",
	VAlign = "stretch",
	Padding = box(2, 2, 2, 2),
	Background = RGB(255, 255, 255),
	FocusedBackground = RGB(255, 255, 255),
	MinWidth = 300,
	MaxWidth = 100000,
	
	ContainerControlClass = "XScrollArea",
	HorizontalScroll = false,
	Translate = false,
	documentation_btn = false,
	test_btn = false,
	expanded = true, -- panels with Collapsible == true can be expanded/collapsed
	search_popup = false,
	search_values = false,
	search_value_results = false,
	read_only = false,
}

function GedPanel:OnXTemplateSetProperty(prop_id, old_value, ged)
	if prop_id == "__class" or prop_id == "ActionsClass" then
		local data = common_action_ops[self.__class]
		if prop_id == "__class" then -- reset to default ops for that panel class
			self.ActionsClass = data and "PropertyObject" or "None"
		end
		
		local ops = data and data[self.ActionsClass] or empty_table
		for _, op in ipairs(GedCommonOps) do
			self:SetProperty(op.Id, ops[op.Id] or GedDisabledOp)
		end
	end
end

function GedPanel:OpEdit(root, prop_id, ged, param)
	self:SetProperty(prop_id, "")
	ObjModified(self)
end

function GedPanel:ToggleSearch()
	if self.idSearchContainer:GetVisible() then
		self:CloseSearch()
	else
		self:OpenSearch()
	end
end

function GedPanel:SaveSearchToggled(value)
	local settings = self.app.settings or {}
	local opened_panels = settings.opened_panels or {}
	if value ~= nil then
		opened_panels[self.Id] = value
	end
	settings.opened_panels = opened_panels
	self.app.settings = settings
	return opened_panels[self.Id]
end

function GedPanel:OpenSearch()
	if not self:IsSearchAvailable() then return end
	
	local search = self.idSearchContainer
	if not search.visible then
		search:SetDock("top")
		search:SetVisible(true)
	end
	self.idSearchEdit:SetFocus()
	self.idSearchEdit:SelectAll()
	self:SaveSearchToggled(true)
end

function GedPanel:CloseSearch()
	local search = self.idSearchContainer
	if not search.visible then return end
	search:SetDock("ignore")
	search:SetVisible(false)
	if self.idSearchEdit:GetText() ~= "" then
		self:UpdateFilter()
	end
	self:SaveSearchToggled(false)
	return true
end

function GedPanel:CancelSearch(dont_select)
	if not self.idSearchContainer:GetVisible() then
		return false
	end
	if self.PersistentSearch then
		if self.idSearchEdit:GetText() ~= "" then
			self.idSearchEdit:SetText("")
			self.app.search_value_filter_text = ""
			self.app:TryHighlightSearchMatchInChildPanels(self) -- clear highlights when search is canceled
			self:UpdateFilter()
		elseif not self.idContainer:IsFocused(true) then
			if not dont_select then
				self:FocusFirstEntry()
			end
		else
			self.idSearchEdit:SetFocus()
			self.idSearchEdit:SelectAll()
		end
		return true
	else
		return self:CloseSearch()
	end
end

function GedPanel:UpdateFilter()
end

function GedPanel:FocusFirstEntry()
end

function GedPanel:GetFilterText()
	local search = self.idSearchContainer
	if not search or not search.visible then
		return ""
	end
	return string.lower(self.idSearchEdit:GetText())
end

function GedPanel:GetHighlightText()
	local app = self.app
	return app.search_value_results and app.search_value_filter_text
end

function GedPanel:OnShortcut(shortcut, source, ...)
	if shortcut == "Escape" and self:CancelSearch() then
		return "break"
	end
	
	local app = self.app
	local search_panel = app.search_value_panel
	if app.search_value_results and search_panel then
		if shortcut == "F4" then
			search_panel:NextMatch(1)
			return "break"
		elseif shortcut == "Shift-F4" then
			search_panel:NextMatch(-1)
			return "break"
		elseif shortcut == "F5" then
			search_panel:StartUpdateFilterThread()
			return "break"
		end
	end
end

function GedPanel:IsSearchAvailable()
	return self.EnableSearch and not self.Embedded
end

function GedPanel:UpdateSearchVisiblity()
	local enabled = self.EnableSearch and not self.Embedded
	if not enabled then
		self:CloseSearch()
	end
	self.idToggleSearch:SetVisible(enabled)
	self.idToggleSearch:SetDock(enabled and "right" or "ignore")

	self:UpdateSearchContextMenu(self:GetSearchActionContexts()) -- updates "Search" action
end

function GedPanel:SetSearchActionContexts(search_contexts)
	if not search_contexts or type(search_contexts) ~= "table" then return end
	
	self:UpdateSearchContextMenu(search_contexts)
end

function GedPanel:UpdateSearchContextMenu(new_contexts)
	-- Attach search to the context menu
	if not new_contexts or type(new_contexts) ~= "table" or #new_contexts == 0 then return end
	
	local host = GetActionsHost(self)
	if not host then return end
	local search_action = host:ActionById("idSearch")
	if not search_action then return end
	local contexts = search_action.ActionContexts
	if not contexts then return end
	
	local old_contexts = self:GetSearchActionContexts()
	if old_contexts and #old_contexts ~= 0 then
		for _, old in ipairs(old_contexts) do 
			table.remove_entry(contexts, old)
		end
	end
	
	local search_enabled = self.EnableSearch and not self.Embedded
	if search_enabled then
		for _, new in ipairs(new_contexts) do 
			table.insert(contexts, new)
		end
	end
	
	self.SearchActionContexts = new_contexts
end

function GedPanel:AddToSearchHistory(text)
	local settings = self.app.settings or {}
	settings.search_history = settings.search_history or {}
	local history_list = settings.search_history[self.Id] or {}
	if text and #text:trim_spaces() > 0 then
		table.remove_value(history_list, text)
		table.insert(history_list, 1, text)
		if #history_list > self.SearchHistory then
			table.remove(history_list, #history_list)
		end
	end
	settings.search_history[self.Id] = history_list
	self.app.settings = settings
	return history_list
end

function GedPanel:OpenSearchHistory(keyboard)
	local popup = XPopupList:new({
		LayoutMethod = "VList",
	}, self.desktop)
	
	local history = self:AddToSearchHistory(nil)
	if #history > 0 then
		for idx, item in ipairs(history) do
			local entry = XTemplateSpawn("XComboListItem", popup.idContainer, self.context)
			entry:SetFocusOrder(point(1, idx))
			entry:SetFontProps(XCombo)
			entry:SetText(item)
			entry:SetMinHeight(entry:GetFontHeight())
			entry.OnPress = function(entry)
				self.idSearchEdit:SetText(item)
				self:AddToSearchHistory(item)
				if popup.window_state ~= "destroying" then
					popup:Close()
				end
			end
		end
	else
		XText:new({}, popup.idContainer):SetText("No history.")
	end
	popup:SetAnchor(self.idSearchContainer.box)
	popup:SetAnchorType("drop")
	popup:Open()
	if keyboard and #history > 0 then 
		popup.idContainer[1]:SetFocus()
	end
	self.search_popup = popup
	self.app:UpdateChildrenDarkMode(popup)
end

function GedPanel:InitControls()
	self.expanded = self.StartsExpanded
	
	XWindow:new({
		Id = "idTitleContainer",
		Dock = "ignore", -- see OnContextUpdate
		Background = RGB(196, 196, 196),
		HandleMouse = true,
	}, self):SetVisible(false)
	
	local search_container
	if self.SearchValuesAvailable then
		assert(self.PersistentSearch)
		XWindow:new({
			Id = "idSearchContainer",
			Dock = "ignore",
		}, self):SetVisible(false)
		search_container = XWindow:new({
			BorderWidth = 1,
		}, self.idSearchContainer)
		
		self:CreateSearchResultsPanel()
		
		local search_toggle = XTemplateSpawn("GedToolbarToggleButtonSmall", self.idSearchContainer)
		search_toggle:SetId("idSearchValuesButton")
		search_toggle:SetIcon("CommonAssets/UI/Ged/log-dataset.tga")
		search_toggle:SetRolloverText(Untranslated("Toggle search in properties and sub-objects"))
		search_toggle:SetMargins(box(2, 0, 0, 0))
		search_toggle.OnPress = function() return self:ToggleSearchValues() end
	else
		search_container = XWindow:new({
			Id = "idSearchContainer",
			Dock = "ignore",
			BorderWidth = 1,
			Background = RGB(255, 255, 255),
		}, self)
		search_container:SetVisible(false)
	end
	
	local button = XTemplateSpawn("XComboButton", search_container, self.context)
	button:SetId("idSearchHistory")
	button:SetMargins(empty_box)
	button:SetVisible(self.SearchHistory > 0)
	button.OnPress = function(button)
		if not self.search_popup or self.search_popup.window_state ~= "open" then
			self:PopulateSearchValuesCache()
			self:OpenSearchHistory()
		else
			self.search_popup:Close()
		end
	end
	button.FoldWhenHidden = true
	XTextButton:new({
		Id = "idCancelSearch",
		Dock = "right",
		VAlign = "center",
		Text = "x",
		MaxWidth = 20,
		MaxHeight = 16,
		LayoutHSpacing = 0,
		BorderWidth = 0,
		Background = RGBA(0, 0, 0, 0),
		RolloverBackground = RGB(204, 232, 255),
		PressedBackground = RGB(121, 189, 241),
		OnPress = function() self:CancelSearch() end,
		FoldWhenHidden = true,
	}, search_container)
	self.idCancelSearch:SetVisible(false)
	XEdit:new({
		Id = "idSearchEdit",
		Dock = "box",
		Hint = "Search...",
		BorderWidth = 0,
		Background = RGBA(0, 0, 0, 0),
		AllowEscape = false,
		OnTextChanged = function(edit)
			XEdit.OnTextChanged(edit)
			self:StartUpdateFilterThread()
			if edit:GetText() ~= "" then
				self:SetSelection(false)
			end
			self.idCancelSearch:SetVisible(edit:GetText() ~= "")
		end,
		OnShortcut = function(edit, shortcut, source, ...)
			local result = XEdit.OnShortcut(edit, shortcut, source, ...)
			if result == "break" then return result end
			if shortcut == "Down" or shortcut == "Enter" then
				if shortcut == "Down" and self.SearchHistory > 0 and edit:GetText() == "" then
					self:OpenSearchHistory("keyboard")
					return "break"
				else
					self:FocusFirstEntry()
					return shortcut == "Down" and "break" -- allow Enter to open console, e.g. when typing in Inspector's search
				end
			end
		end,
		OnSetFocus = function(edit)
			self:PopulateSearchValuesCache()
			return XEdit.OnSetFocus(edit)
		end,
		OnKillFocus = function(edit, new_focus)
			self:AddToSearchHistory(edit:GetText())
			return XEdit.OnKillFocus(edit, new_focus)
		end,
	}, search_container)
	
	local search_button = XTemplateSpawn("GedToolbarButtonSmall", self.idTitleContainer)
	search_button:SetId("idToggleSearch")
	search_button:SetIcon("CommonAssets/UI/Ged/view.tga")
	search_button:SetRolloverText("Search (Ctrl-F)")
	search_button:SetBackground(self.idTitleContainer:GetBackground())
	search_button.OnPress = function() self:ToggleSearch() end
	--- End search controls

	XText:new({
		Id = "idTitle",
		Dock = "box",
		ZOrder = 1000,
		Margins = box(2, 1, 2, 1),
		TextStyle = self.Collapsible and "GedDefault" or "GedTitleSmall",
		HandleMouse = self.Collapsible,
		Translate = true,
		OnMouseButtonDown = function(button, pt, button)
			if self.Collapsible and button == "L" and
			   not terminal.IsKeyPressed(const.vkShift) and
			   not terminal.IsKeyPressed(const.vkControl) then
				self:Expand(not self.expanded)
			end
		end,
	}, self.idTitleContainer)
	
	XText:new({
		Id = "idWarningText",
		Dock = "top",
		VAlign = "center",
		TextHAlign = "center",
		BorderWidth = 1,
		BorderColor = RGB(255, 0 , 0),
		Background = RGB(255, 196, 196),
		FoldWhenHidden = true,
	}, self)
	self.idWarningText:SetVisible(false)
	
	if self.HorizontalScroll then
		XSleekScroll:new({
			Id = "idHScroll",
			Target = "idContainer",
			Dock = "bottom",
			Margins = box(0, 2, 7, 0),
			Horizontal = true,
			AutoHide = true,
			FoldWhenHidden = true,
		}, self)
	end
	local vertical_scroll = _G[self.ContainerControlClass]:IsKindOf("XScrollArea")
	if vertical_scroll then
		XSleekScroll:new({
			Id = "idScroll",
			Target = "idContainer",
			Dock = "right",
			Margins = box(2, 0, 0, 0),
			Horizontal = false,
			AutoHide = true,
			FoldWhenHidden = true,
		}, self)
	end
	_G[self.ContainerControlClass]:new({
		Id = "idContainer",
		HAlign = "stretch",
		VAlign = "stretch",
		MinHSize = false,
		LayoutMethod = "VList",
		Padding = box(2, 2, 2, 2),
		BorderWidth = 0,
		HScroll = self.HorizontalScroll and "idHScroll" or "",
		VScroll = vertical_scroll and "idScroll" or "",
		FoldWhenHidden = true,
		RolloverTemplate = "GedPropRollover",
		Translate = self.Translate,
	}, self)
	self.idContainer.OnSetFocus = function() self:OnSetFocus() end -- fix for edge case with one panel docked in another one
	
	self:UpdateSearchVisiblity()
end

function GedPanel:GetTitleView()
	return self.TitleFormatFunc .. "." .. self.Title -- generate unique view id, to make sure different panels won't clash
end

function GedPanel:Open(...)
	self:InitControls()

	XWindow.Open(self, ...)
	
	if self.context then
		self:BindViews()
		if self.Title ~= "" then
			self:BindView(self:GetTitleView(), self.TitleFormatFunc, self.Title)
		end
		if self.Predicate ~= "" then
			self:BindView("predicate", "GedExecMemberFunc", self.Predicate)
		end
		self:BindView("read_only", "GedGetReadOnly")
	end
	
	if self.PersistentSearch then
		if self:IsSearchAvailable() and not self.idSearchContainer:GetVisible() and self:SaveSearchToggled(nil) ~= false then
			self:OpenSearch()
		end
	end
end

function GedPanel:Expand(expand)
	self.expanded = expand
	self.idContainer:SetVisible(expand)
	self.idScroll:SetAutoHide(expand)
	self.idScroll:SetVisible(expand)
	if self.HorizontalScroll then
		self.idHScroll:SetAutoHide(expand)
		self.idHScroll:SetVisible(expand)
	end
	self:UpdateTitle(self.context)
end

function GedPanel:UpdateTitle(context)
	local title = self.Title ~= "" and self.Title ~= "<empty>" and self:Obj(context .. "|" .. self:GetTitleView()) or ""
	if self.Collapsible then
		if title == "" and self.Title ~= "<empty>" then title = "(no description)" end
		
		local match
		local obj_id = self:Obj(context)
		for _, panel in pairs(self.app.interactive_panels) do
			local res = panel.search_value_results
			if res and res[obj_id] then
				title = GedPanelBase.MatchMark .. title
				match = true
				break
			end
		end	
		
		local is_empty = self:IsEmpty()
		local corner_message = self.EmptyMessage
		if not is_empty then
			corner_message = (self.expanded and self.ExpandedMessage or "(click to expand)")
		end
		
		if not match then
			title = (not is_empty and (self.expanded and "- " or "+ ") or "") .. title
			if not self.Embedded then
				title = title .. "<right><color 128 128 128>" .. corner_message
			end
		end
	end
	self.idTitle:SetText(Untranslated(title))
	self.idTitleContainer:SetVisible(title ~= "")
	self.idTitleContainer:SetDock(title ~= "" and "top" or "ignore")
end

function GedPanel:OnContextUpdate(context, view)
	if not view then -- obj changed
		if self.Title ~= "" then
			self:BindView(self:GetTitleView(), self.TitleFormatFunc, self.Title)
		end
		if self.Predicate ~= "" then
			self:BindView("predicate", "GedExecMemberFunc", self.Predicate)
		end
		if self.DisplayWarnings and not self.app.actions_toggled["ToggleDisplayWarnings"] then
			self:BindView("warning", "GedGetWarning")
		end
		if not self.Embedded then
			self:BindView("read_only", "GedGetReadOnly")
		end
		self:BindView("documentationLink", "GedGetDocumentationLink")
		self:BindView("documentation", "GedGetDocumentation")
		
		if self.Collapsible then
			local obj_id = self:Obj(self.context)
			for _, panel in pairs(self.app.interactive_panels) do
				local results = panel.search_value_results
				if results and results[obj_id] then
					self.expanded = true
				end
			end
			self:Expand(self.expanded)
		end
	end
	if view == self:GetTitleView() then
		self:UpdateTitle(context)
	end
	if view == "predicate" then
		local predicate = self:Obj(context .. "|predicate")
		self:SetVisible(predicate)
		self:SetDock(not predicate and "ignore")
	end
	if view == "warning" and self.DisplayWarnings then
		local warning = self:Obj(context .. "|warning")
		self.idWarningText:SetVisible(warning)
		self.idWarningText:SetTextColor(RGB(0, 0, 0))
		self.idWarningText:SetRolloverTextColor(RGB(0, 0, 0))
		if type(warning) == "table" then
			self.idWarningText:SetText(warning[1])
			local color = warning[2]
			if color == "warning" then color = RGB(255, 140, 0) end
			if color == "error" then color = RGB(255, 196, 196) end
			
			if color then
				local r, g, b = GetRGB(color)
				local max = Max(Max(r, g), b)
				self.idWarningText:SetBackground(color)
				self.idWarningText:SetBorderColor(RGB(r == max and r or r / 4, g == max and g or g / 4, b == max and b or b / 4))
			else
				self.idWarningText:SetBackground(RGB(255, 196, 196))
				self.idWarningText:SetBorderColor(RGB(255, 0 , 0))
			end
		else
			self.idWarningText:SetText(warning)
			self.idWarningText:SetBackground(RGB(255, 196, 196))
			self.idWarningText:SetBorderColor(RGB(255, 0 , 0))
		end
	end
	if (view or ""):starts_with("documentation") then
		local documentation = self:Obj(context .. "|documentation")
		local doc_link = self:Obj(context .. "|documentationLink")
		if (documentation or doc_link or "") ~= "" then
			if not self.documentation_btn then
				self.documentation_btn = XTemplateSpawn("GedToolbarButtonSmall", self.idTitleContainer)
				self.documentation_btn:SetIcon("CommonAssets/UI/Ged/help.tga")
				self.documentation_btn:SetZOrder(-1)
			end
			self.documentation_btn.OnPress = function(button)
				button:SetFocus()
				if (doc_link or "") ~= "" then
					local link = doc_link:starts_with("http") and doc_link or ("file:///" .. doc_link)
					OpenUrl(link, "force external browser")
				end
				button:SetFocus(false)
			end
			local rollover_text = ""
			if doc_link then
				rollover_text = string.format("Open  %s \n\n", doc_link)
			end
			rollover_text = rollover_text .. (documentation or "")
			self.documentation_btn:SetRolloverText(rollover_text)
			self.documentation_btn:SetVisible(true)
			
			if self.Embedded then
				if not self.test_btn then
					self.test_btn = XTemplateSpawn("GedToolbarButtonSmall", self.idTitleContainer)
					self.test_btn:SetIcon("CommonAssets/UI/Ged/play.tga")
					self.test_btn:SetZOrder(-1)
					self.test_btn:SetRolloverText("Run now!")
				end
				self.test_btn.OnPress = function(button)
					button:SetFocus()
					self:Send("GedTestFunctionObject", self.context)
					button:SetFocus(false)
				end
			end
		elseif self.documentation_btn then
			self.documentation_btn:SetVisible(false)
		end
	end
	if view == "read_only" then
		self.read_only = self:Obj(self.context .. "|read_only")
		self.app:ActionsUpdated()
	end
	GedPanelBase.OnContextUpdate(self, context, view)
end

function GedPanel:SetPanelFocused()
	return self.idContainer:SetFocus()
end

function GedPanel:ToggleSearchValues(no_settings_update)
	self.search_values = not self.search_values
	local button = self.idSearchValuesButton
	button:SetToggled(not button:GetToggled())
	self.idSearchEdit:SetFocus(false)
	self.idSearchEdit:SetFocus()
	self:StartUpdateFilterThread()
	
	if not no_settings_update then
		local settings = self.app.settings
		settings.search_in_props = settings.search_in_props or {}
		settings.search_in_props[self.context] = self.search_values
		self.app:SaveSettings()
	end
end

function GedPanel:StartUpdateFilterThread(not_user_initiated)
	if self.search_values and self.idSearchEdit:GetText() ~= "" then
		self.app:SetUiStatus("value_search_in_progress", "Searching...")
	end
	
	self:DeleteThread("UpdateFilterThread")
	self:CreateThread("UpdateFilterThread", function()
		Sleep(75)
		
		if self.search_values then
			local filter = self.idSearchEdit:GetText()
			self.search_value_results = filter ~= "" and self.connection:Call("rfnSearchValues", self.context, self:GetFilterText())
			if self.search_value_results == "timeout" then
				self.search_value_results = false
			end
			
			if self.PersistentSearch and not not_user_initiated then
				self:ShowSearchResultsPanel(self:GetFilterText(), self.search_value_results)
			end
		else
			self.search_value_results = nil
		end
		
		if self.window_state ~= "destroying" then
			self:UpdateFilter()
			for _, panel in pairs(self.app.interactive_panels) do
				if panel ~= self then
					panel:UpdateItemTexts()
				end
			end
			self.app:SetUiStatus("value_search_in_progress", false)
		end
	end)
end

function GedPanel:PopulateSearchValuesCache()
	if self.search_values then
		CreateRealTimeThread(function() self.connection:Call("rfnPopulateSearchValuesCache", self.context) end)
	end
end

function GedPanel:CreateSearchResultsPanel()
	XWindow:new({
		Id = "idSearchResultsPanel",
		Dock = "bottom",
		FoldWhenHidden = true,
		Padding = box(0, 1, 0, 1)
	}, self.idSearchContainer):SetVisible(false)

	local button = XTemplateSpawn("GedToolbarButtonSmall", self.idSearchResultsPanel)
	button:SetIcon("CommonAssets/UI/Ged/undo.tga")
	button:SetRolloverText("Refresh search results (F5)")
	button:SetDock("right")
	button.OnPress = function() self:StartUpdateFilterThread() end

	local button = XTemplateSpawn("GedToolbarButtonSmall", self.idSearchResultsPanel)
	button:SetIcon("CommonAssets/UI/Ged/up.tga")
	button:SetRolloverText("Previous match (Shift-F4)")
	button:SetDock("right")
	button.OnPress = function() self:NextMatch(-1) end
	
	local button = XTemplateSpawn("GedToolbarButtonSmall", self.idSearchResultsPanel)
	button:SetIcon("CommonAssets/UI/Ged/down.tga")
	button:SetRolloverText("Next match (F4)")
	button:SetDock("right")
	button.OnPress = function() self:NextMatch(1) end
	
	XText:new({
		Id = "idSearchResultsText",
		TextStyle = "GedDefault",
	}, self.idSearchResultsPanel)
end

function GedPanel:ShowSearchResultsPanel(filter, search_value_results)
	local app = self.app
	app.search_value_filter_text = filter
	app.search_value_results = search_value_results
	app.search_value_panel = self
	self.idSearchResultsPanel:SetVisible(search_value_results)
	if search_value_results then
		app.search_result_idx = 1
		self:NextMatch(0, "dont_unfocus_search_edit")
	end
end

function GedPanel:NextMatch(direction, dont_unfocus_search_edit)
	local app = self.app
	local count = #app.search_value_results
	app.search_result_idx = Clamp(app.search_result_idx + direction, 1, count)
	app.display_search_result = true
	self.idSearchResultsText:SetText(string.format("Match %d/%d", app.search_result_idx, count))
	
	local focus = self.desktop:GetKeyboardFocus()
	if not dont_unfocus_search_edit and focus.Id == "idSearchEdit" then
		self.desktop:RemoveKeyboardFocus(focus)
	end
	self:TryHighlightSearchMatch()
end

function GedPanel:FilterItem(text, item_id, filter_text)
	if filter_text == "" then return end
	if self.search_values then
		return not self.search_value_results or self.search_value_results.hidden[item_id or false]
	else
		text = IsT(text) and TDevModeGetEnglishText(text) or tostring(text):gsub("<[^>]+>", "")
		text = string.lower(text)
		return not text:find(filter_text, 1, true)
	end
end

function GedPanel:UpdateItemTexts()
	self:UpdateTitle(self.context)
end

function GedPanel:IsEmpty()
	-- override in child panels
end


----- GedPropPanel

DefineClass.GedPropPanel = {
	__parents = { "GedPanel" },
	properties = {
		-- internal use
		{ category = "General", id = "CollapseDefault", editor = "bool", default = false, no_edit = true, },
		{ category = "General", id = "ShowInternalNames", editor = "bool", default = false, no_edit = true, },
		{ category = "General", id = "EnableUndo", editor = "bool", default = true, },
		{ category = "General", id = "EnableCollapseDefault", editor = "bool", default = true, },
		{ category = "General", id = "EnableShowInternalNames", editor = "bool", default = true, },
		{ category = "General", id = "EnableCollapseCategories", editor = "bool", default = true, },
		{ category = "General", id = "HideFirstCategory", editor = "bool", default = false, },
		{ category = "General", id = "RootObjectBindName", editor = "text", default = false, },
		{ category = "General", id = "SuppressProps", editor = "prop_table", default = false, help = "Set of properties to skip in format { id1 = true, id2 = true, ... }." },
		
		{ category = "Context Menu" , id = "PropActionContext", editor = "text", default = "" },
	},
	MinWidth = 200,
	Interactive = true,
	EnableSearch = true,
	ShowUnusedPropertyWarnings = false,
	
	update_request = false,
	rebuild_props = true,
	prop_update_in_progress = false,
	parent_obj_id = false,
	parent_changed = false,
	parent_changed_notified = false,
	collapsed_categories = false,
	collapse_default_button = false,
	active_tab = "All",
	
	-- property selection
	selected_properties = false,
	last_selected_container_indx = false, 
	last_selected_property_indx = false, 
}

function GedPropPanel:InitControls()
	GedPanel.InitControls(self)

	self.idContainer:SetPadding(box(0, 3, 0, 0))
	self.idContainer:SetLayoutVSpacing(5)
	self.collapsed_categories = {}
	self.selected_properties = {}
	
	GedPanel.SetSearchActionContexts(self, self.SearchActionContexts) 
	
	local host = GetActionsHost(self)
	if not host:ActionById("EditCode") then
		XAction:new({
			ActionId = "EditCode",
			ActionContexts = { self.PropActionContext } ,
			ActionName = "Edit Code",
			ActionTranslate = false,
			OnAction = function(action, host)
				local panel = host:GetLastFocusedPanel()
				if IsKindOf(panel, "GedPropPanel") then
					self:Send("GedEditFunction", panel.context, panel:GetSelectedProperties())
				end
			end,
			ActionState = function(action, host)
				local panel = host:GetLastFocusedPanel()
				if not IsKindOf(panel, "GedPropPanel") then
					return "hidden"
				end
				local selected = panel.selected_properties
				if not selected or #selected ~= 1 then return "hidden" end
				local prop_meta = selected[1].prop_meta
				if prop_meta.editor ~= "func" and prop_meta.editor ~= "expression" and prop_meta.editor ~= "script" then
					return "hidden"
				end
			end,
		}, self)
	end
	
	local show_collapse_action = self.EnableCollapseDefault and not self.Embedded
	if show_collapse_action and not self.collapse_default_button then
		self.collapse_default_button = XTemplateSpawn("GedToolbarToggleButtonSmall", self.idTitleContainer)
		self.collapse_default_button:SetId("idCollapseDefaultBtn")
		self.collapse_default_button:SetIcon("CommonAssets/UI/Ged/collapse.tga")
		self.collapse_default_button:SetRolloverText(T(912785185075, "Hide/show all properties with default values"))
		self.collapse_default_button:SetBackground(self.idTitleContainer:GetBackground())
		self.collapse_default_button:SetToggled(self.CollapseDefault)
		self.collapse_default_button.OnPress = function(button)
			self:SetFocus()
			self:SetCollapseDefault(not self.CollapseDefault)
			button:SetToggled(not button:GetToggled())
		end
	end
	
	local show_internal_names_action = self.EnableShowInternalNames and not self.Embedded
	if show_internal_names_action then
		local show_internal_names_button = XTemplateSpawn("GedToolbarToggleButtonSmall", self.idTitleContainer)
		show_internal_names_button:SetId("idShowInternalNamesBtn")
		show_internal_names_button:SetIcon("CommonAssets/UI/Ged/log-focused.tga")
		show_internal_names_button:SetRolloverText(T(496361185046, "Hide/show internal names of properties"))
		show_internal_names_button:SetBackground(self.idTitleContainer:GetBackground())
		show_internal_names_button:SetToggled(self.ShowInternalNames)
		show_internal_names_button.OnPress = function(button) 
			self:ShowInternalPropertyNames(not self.ShowInternalNames)
			button:SetToggled(not button:GetToggled())
		end
	end
	
	if not self.Embedded then
		if self.EnableCollapseCategories then
			local button = XTemplateSpawn("GedToolbarButtonSmall", self.idTitleContainer)
			button:SetIcon("CommonAssets/UI/Ged/collapse_tree.tga")
			button:SetRolloverText("Expand/collapse categories (Shift-C)")
			button:SetBackground(self.idTitleContainer:GetBackground())
			button.OnPress = function() self:ExpandCollapseCategories() end
		end
	
		if self.app.PresetClass and self.DisplayWarnings then
			self.ShowUnusedPropertyWarnings = self.app.ShowUnusedPropertyWarnings
			
			local button = XTemplateSpawn("GedToolbarToggleButtonSmall", self.idTitleContainer)
			button:SetIcon("CommonAssets/UI/Ged/warning_button.tga")
			button:SetRolloverText("Show/hide unused property warnings")
			button:SetBackground(self.idTitleContainer:GetBackground())
			button:SetToggled(self.ShowUnusedPropertyWarnings)
			button.OnPress = function(button)
				self.ShowUnusedPropertyWarnings = not self.ShowUnusedPropertyWarnings
				button:SetToggled(not button:GetToggled())
				self:UpdatePropertyNames(self.ShowInternalNames)
			end
		end
	end
end

function GedPropPanel:Open(...)
	GedPanel.Open(self, ...)
	
	self:CreateThread("update", self.UpdateThread, self)
	GetActionsHost(self, true):ActionById("EditCode").ActionContexts = { self.PropActionContext }
end

function GedPropPanel:ShowInternalPropertyNames(value)
	if self.ShowInternalNames ~= value then
		self.ShowInternalNames = value
		self:UpdatePropertyNames(value)
	end
end

function GedPropPanel:SetSelection(properties)
	if not properties then return end
	self:ClearSelectedProperties()
	for con_indx, win in ipairs(self.idContainer) do
		for cat_indx, item in ipairs(win.idCategory) do
			for _, id in ipairs(properties) do
				if item.prop_meta.id == id then 
					self:AddToSelected(item, con_indx, cat_indx)
				end
			end
		end
	end
end

function GedPropPanel:OnMouseButtonDown(pos, button)
	local prop, container_indx, category_indx = self:GetGedPropAt(pos)
	local selected_props = self.selected_properties
	
	if button == "L" then
		self:SetFocus()
		self.app:SetLastFocusedPanel(self)
		if prop then 
			if #selected_props == 0 then
				self:AddToSelected(prop, container_indx, category_indx)
			else	
				if terminal.IsKeyPressed(const.vkControl) then -- Ctrl pressed
					if prop.selected then
						self:RemoveFromSelected(prop, container_indx, category_indx)
					else
						self:AddToSelected(prop, container_indx, category_indx)
					end
				elseif terminal.IsKeyPressed(const.vkShift) then -- Shift pressed
					self:ShiftSelectMultiple(prop, container_indx, category_indx)
				else
					local current_prop_selected = prop.selected
					self:ClearSelectedProperties()
					if not current_prop_selected then 
						self:AddToSelected(prop, container_indx, category_indx)
					end
				end
			end
		else
			self:ClearSelectedProperties()
		end
		return "break"
	elseif button == "R" then 
		self:SetFocus()
		local action_context = self:GetActionContext()
		if prop then 
			if #selected_props < 2 then
				self:ClearSelectedProperties()
				self:AddToSelected(prop, container_indx, category_indx)
			end
			action_context = self.PropActionContext
		end
		
		local host = GetActionsHost(self, true)
		if host then
			host:OpenContextMenu(action_context, pos)
		end
		return "break"
	end
end

function GedPropPanel:GetSelectedProperties()
	local selected_ids = {}
	for _, prop in ipairs(self.selected_properties) do
		table.insert(selected_ids, prop.prop_meta.id)
	end
	return selected_ids
end

function GedPropPanel:ShiftSelectMultiple(prop, container_indx, category_indx)
	self:ClearSelectedProperties("keep_last_selected_container_indx")

	--- default, when selection is in same container ---
	local con_indx = container_indx
	local max_con_indx = container_indx
	local cat_indx = category_indx
	local cat_max_indx = self.last_selected_property_indx
	if category_indx > self.last_selected_property_indx then
		cat_indx = self.last_selected_property_indx 
		cat_max_indx = category_indx
	end
	--- when selection is in different container ---
	if container_indx ~= self.last_selected_container_indx then
		if container_indx > self.last_selected_container_indx then
			con_indx = self.last_selected_container_indx 
			max_con_indx = container_indx 
			cat_indx = self.last_selected_property_indx 
			cat_max_indx = category_indx 
		else
			con_indx = container_indx
			max_con_indx = self.last_selected_container_indx 
			cat_indx = category_indx
			cat_max_indx = self.last_selected_property_indx
		end
	end
	
	local container = false
	local category_cnt = false
	local shift_select = true
	while shift_select do
		container = self.idContainer[con_indx]
		category_cnt = con_indx == max_con_indx and cat_max_indx or #container.idCategory
		if container.idCategory.visible then
			for i = cat_indx, category_cnt do
				self:AddToSelected(container.idCategory[i], self.last_selected_container_indx, self.last_selected_property_indx)
			end
		end
		con_indx = con_indx + 1
		cat_indx = 1
		if con_indx > max_con_indx then
			shift_select = false
		end		
	end
end
				
function GedPropPanel:OnShortcut(shortcut, source, ...)
	local res = GedPanel.OnShortcut(self, shortcut, source, ...)
	if res == "break" then return res end
	if shortcut == "Escape" or shortcut == "ButtonB" then
		self:ClearSelectedProperties()
	end
end

function GedPropPanel:ClearSelectedProperties(keep_last_selected)
	local selected_props = self.selected_properties
	for i = 1, #selected_props do
		selected_props[i]:SetSelected(false)
		selected_props[i] = nil
	end 
	if not keep_last_selected then
		self.last_selected_container_indx = false
		self.last_selected_property_indx = false
	end
end

function GedPropPanel:AddToSelected(prop, con_indx, prop_indx)
	prop:SetSelected(true)
	local selected_props = self.selected_properties
	assert(not table.find(selected_props, prop))
	selected_props[#selected_props + 1] = prop
	self.last_selected_container_indx = con_indx
	self.last_selected_property_indx = prop_indx
end

function GedPropPanel:RemoveFromSelected(prop, con_indx, prop_indx)
	prop:SetSelected(false)
	local selected_props = self.selected_properties
	local indx = table.find(selected_props, prop)
	assert(indx)
	selected_props[indx] = nil
	self.last_selected_container_indx = con_indx
	self.last_selected_property_indx = prop_indx
end

function GedPropPanel:GetGedPropAt(pt)
	for container_indx, container in ipairs(self.idContainer) do
		if container:HasMember("idCategory") and container.idCategory.visible then -- window which holds the GedPropEditors
			for gedprop_index, gedprop in ipairs(container.idCategory) do
				if gedprop:MouseInWindow(pt) then 
					return gedprop, container_indx, gedprop_index
				end 
			end
		end
	end
end

function GedPropPanel:SetCollapseDefault(value)
	if self.CollapseDefault ~= value then
		self.CollapseDefault = value
		self.rebuild_props = true
		self:RequestUpdate()
	end
end

function GedPropPanel:BindViews()
	if not self.context then return end
	
	-- ensures all views will be resent on rebind
	self:UnbindView("props")
	self:UnbindView("values")
	
	self:BindView("props", "GedGetProperties", self.SuppressProps)
	self:BindView("values", "GedGetValues")
end

function GedPropPanel:OnContextUpdate(context, view)
	GedPanel.OnContextUpdate(self, context, view)
	if view == nil then
		self.parent_obj_id = self.connection.bound_objects[context]
		self.parent_changed = true
		self.parent_changed_notified = true
		self.connection.bound_objects[context .. "|values"] = nil
		self:RequestUpdate()
	end
	if view == "values" then
		self.rebuild_props = self.rebuild_props or self.CollapseDefault
		self:RequestUpdate()
	end
	if view == "props" then
		self.rebuild_props = true
		self:RequestUpdate()
		self:RebuildTabs()
	end
end

function GedPropPanel:GetTabsData()
	local data = self:Obj(self.context .. "|props")
	local tabs = data and data.tabs
	if not tabs then return end
	
	-- list all categories not it any tabs in an Other tab; remove empty tabs
	if tabs[#tabs].TabName ~= "Other" then
		local categories = {}
		for _, prop in ipairs(data) do
			categories[prop.category or "Misc"] = true
		end
		local allcats = table.copy(categories)
		for i = #tabs, 1, -1 do
			local tab = tabs[i]
			local has_content
			for category in pairs(tab.Categories) do
				has_content = has_content or allcats[category]
				categories[category] = nil
			end
			if not has_content then
				table.remove(tabs, i)
			end
		end
		if self.app.PresetClass == "XTemplate" then
			categories.Template = nil -- skip Template as a special case for XTemplate Editor (too hard not to make this hack)
		end
		tabs[#tabs + 1] = { TabName = #table.keys(categories) == 1 and next(categories) or "Other", Categories = categories }
	end
	return tabs
end

function GedPropPanel:RebuildTabs()
	local container = rawget(self, "idTabContainer")
	if not container then
		container = XWindow:new({
			Id = "idTabContainer",
			LayoutMethod = "HList",
			Dock = "bottom",
			ZOrder = -1,
		}, self.idTitleContainer)
	end
	container:DeleteChildren()
	
	local tabs = self:GetTabsData()
	self.idContainer:SetLayoutVSpacing(tabs and 0 or 5)
	if not tabs then return end
	
	tabs = table.copy(tabs)
	table.insert(tabs, 1, { TabName = "All" })
	
	for _, tab in ipairs(tabs) do
		if tab.TabName == "All" or next(tab.Categories) then
			XToggleButton:new({
				Text = tab.TabName,
				Toggled = self.active_tab == tab.TabName,
				OnChange = function(button)
					self.active_tab = tab.TabName
					self:RebuildTabs()
					self:UpdateVisibility()
				end,
				Padding = box(2, 1, 2, 1),
				BorderWidth = 1,
			}, container)
		end
	end
	Msg("XWindowRecreated", container)
end

function GedPropPanel:RequestUpdate()
	self.update_request = true
	Wakeup(self:GetThread("update"))
end

local function is_slider_dragged()
	return IsKindOfClasses(terminal.desktop:GetMouseCapture(), "XSleekScroll", "XCurveEditor", "XCoordAdjuster")
end

function GedPropPanel:UpdateThread()
	while true do
		if not self.update_request then
			WaitWakeup()
			if self.app.window_state == "closing" or self.window_state == "destroying" or not self.context then return end
		end
		self.update_request = false
		self:ClearSelectedProperties()
		-- don't recreate property editors while the user is dragging a slider
		while is_slider_dragged() do
			if self.app.window_state == "closing" or self.window_state == "destroying" or not self.context then return end
			self:Update(false)
			Sleep(50)
		end
		if self.app.window_state == "closing" or self.window_state == "destroying" or not self.context then return end
		self:Update(self.rebuild_props)
	end
end

function GedPropPanel:RemoteSetProperty(obj, prop_id, value, parent_obj_id, slider_drag_id)
	local rebuild_pending = self.rebuild_props and not slider_drag_id -- see UpdateThread above
	if not rebuild_pending and not self.parent_changed and (parent_obj_id or false) == self.parent_obj_id then
		return self.app:Op("GedSetProperty", obj, prop_id, value, not self.EnableUndo, slider_drag_id or is_slider_dragged())
	end
end

function GedPropPanel:Update(rebuild_props)
	local has_values = self:Obj(self.context .. "|values")
	if has_values and rebuild_props then -- rebuild controls
		local scroll_pos = self.idScroll.Scroll
		self:RebuildControls()
		self.idScroll:ScrollTo(scroll_pos)
		self.rebuild_props = false
	else -- only update the values
		self.prop_update_in_progress = true
		for _, category_window in ipairs(self.idContainer) do
			if category_window:HasMember("idCategory") then
				for _, win in ipairs(category_window.idCategory) do
					if has_values then
						win:UpdateValue(self.parent_changed_notified)
					end
					win.parent_obj_id = self.parent_obj_id
				end
			end
		end
		self.prop_update_in_progress = false
	end
	if has_values then
		self.parent_changed_notified = false
	end
	self.parent_changed = false
	self:QueueReassignFocusOrders()
end

function GedPropPanel:ShouldShowButtonForFunc(func_name)
	if rawget(self.app, "suppress_property_buttons") then
		if table.find(self.app.suppress_property_buttons, func_name) then
			return
		end
	end
	return true
end

function GedPropPanel:QueueReassignFocusOrders(x, y)
	local obj = self
	while obj and obj.Embedded do
		obj = GetParentOfKind(obj.parent, "GedPropPanel")
	end
	
	if obj and not obj:IsThreadRunning("ReasignFocusOrders") then
		obj:CreateThread("ReasignFocusOrders", function() obj:ReassignFocusOrders(x, y) end)
	end
end

function GedPropPanel:UpdateFilter()
	self:UpdateVisibility()
end

local function find_parent_prop_panel(xcontrol)
	return (xcontrol:HasMember("panel") and xcontrol.panel) or 
			(xcontrol.parent and find_parent_prop_panel(xcontrol.parent))
end

function GedPropPanel:RebuildControls()
	-- gather the old prop editors for potential reuse
	local editors_by_hash = {}
	for _, category_window in ipairs(self.idContainer) do
		if category_window:HasMember("idCategory") then
			for _, editor in ipairs(category_window.idCategory) do
				local hash = xxhash(editor:GetContext(), table.hash(editor.prop_meta))
				assert(not editors_by_hash[hash])
				editors_by_hash[hash] = editor
				editor:DetachForReuse()
			end
		end
	end
	self.idContainer:Clear()
	
	-- find the results from a value search to highlight the matching properties
	local matching_props = {}
	local obj_id = self:Obj(self.context)
	for _, panel in pairs(self.app.interactive_panels) do
		local res = panel.search_value_results
		if res and type(res[obj_id]) == "table" then
			for idx, prop in ipairs(res[obj_id]) do
				matching_props[prop] = true
			end
			break
		end
	end	
	
	local categories = {}
	local sort_priority = 1
	local context = self.context
	local values = self:Obj(context .. "|values") or empty_table
	local props = self:Obj(context .. "|props") or empty_table
	if self.read_only then
		for idx, prop_meta in ipairs(props) do
			props[idx] = table.copy(prop_meta)
			props[idx].read_only = true
		end
	end
	table.stable_sort(props, function(a, b) return (a.sort_order or 0) < (b.sort_order or 0) end)
	
	local filter_text = self:GetFilterText()
	local focus = self.desktop.keyboard_focus
	local order = focus and focus:IsWithin(self) and focus:GetFocusOrder()
	local category_data = self:Obj("root|categories") or empty_table
	local parent_panel = find_parent_prop_panel(self) 
	local collapse_default = filter_text == "" and (self.CollapseDefault or (self.Embedded and parent_panel and parent_panel.CollapseDefault) )
	for _, prop_meta in ipairs(props) do
		if not collapse_default or prop_meta.editor == "buttons" or
			values[prop_meta.id] ~= nil and values[prop_meta.id] ~= prop_meta.default
		then
			local group
			local category = prop_meta.category or "Misc"
			local idx = table.find(categories, "category", category)
			if idx then
				group = categories[idx]
			else
				group = { prop_metas = {}, category = category, category_name = category, priority = sort_priority}
				sort_priority = sort_priority + 1
				local property_category = category_data and category_data[category]
				if property_category then
					group.category_name = _InternalTranslate(property_category.display_name, property_category)
					group.priority = group.priority + property_category.SortKey * 1000
				end
				table.insert(categories, group)
			end
			table.insert(group.prop_metas, prop_meta)
		end
	end
	
	if #categories == 0 then
		local text = XText:new({
			Id = "idNoPropsToShow",
			MaxWidth = 300,
			TextHAlign = "center",
		}, self.idContainer)
		text:SetText(collapse_default and "No properties with non-default value were found." or "There are no properties to show.")
		text:Open()
		goto cleanup
	end
	
	table.stable_sort(categories, function(a, b) return a.priority < b.priority end)
	for i, group in ipairs(categories) do
		local category_window = XWindow:new({
			IdNode = true,
			FoldWhenHidden = true,
		}, self.idContainer)
		local container = XWindow:new({
			Id = "idCategory",
			LayoutMethod = "VList",
			LayoutVSpacing = 0,
			FoldWhenHidden = true,
		}, category_window)
		local button = XTextButton:new({
			Id = "idCategoryButton",
			Dock = "top",
			FoldWhenHidden = true,
			LayoutMethod = "VList",
			Padding = box(2, 2, 2, 2),
			Background = RGBA(38, 146, 227, 255),
			FocusedBackground = RGBA(24, 123, 197, 255),
			DisabledBackground = RGBA(128, 128, 128, 255),
			OnPress = function(button)
				self:ExpandCategory(container, group.category, not container:GetVisible(), not self.Embedded and "save_settings")
			end,
			RolloverBackground = RGBA(24, 123, 197, 255),
			PressedBackground = RGBA(13, 113, 187, 255),
			Image = "CommonAssets/UI/round-frame-20.tga",
			ImageScale = point(500, 500),
			FrameBox = box(9, 9, 9, 9),
		}, category_window)
		button:SetTextStyle("GedButton")
		button:SetText(group.category_name)
		button:SetVisible(not self.HideFirstCategory or i ~= 1)
		if self.collapsed_categories[group.category] then
			container:SetVisible(false)
		end
		rawset(category_window, "category", group.category)
		category_window:Open()
		
		self.prop_update_in_progress = true
		
		local values_name = context .. "|values"
		for p, prop_meta in ipairs(group.prop_metas) do
			local editor_name = prop_meta.editor or prop_meta.type
			local editor_class = GedPropEditors[editor_name or false]
			if not editor_class then
				assert(editor_class ~= "objects", "Unknown prop editor name: " .. tostring(editor_name))
			elseif not g_Classes[editor_class] then
				assert(false, "Unknown prop editor class: " .. tostring(editor_class))
			else
				local context = self.context .. "." .. prop_meta.id
				local editor = editors_by_hash[xxhash(context, table.hash(prop_meta))]
				if editor then
					editor:SetParent(container)
					editor:SetContext(context)
				else
					editor = g_Classes[editor_class]:new({
						panel = self,
						obj = values_name,
					}, container, context, prop_meta)
					editor:Open()
				end
				editor:SetHighlightSearchMatch(matching_props[prop_meta.id])
				editor:UpdatePropertyNames(self.ShowInternalNames)
				editor:UpdateValue(true)
				editor.parent_obj_id = self.parent_obj_id
			end
		end
		
		self.prop_update_in_progress = false
	end
	
	self:UpdateVisibility()
	self:ReassignFocusOrders()
	focus = self:GetRelativeFocus(order, "nearest")
	if focus then
		focus:SetFocus()
	end
	
::cleanup::
	self:TryHighlightSearchMatch("skip_visibility_update")
	
	Msg("XWindowRecreated", self)
	for _, editor in pairs(editors_by_hash) do
		if not editor.parent then
			editor:delete()
		end
	end
end

function GedPropPanel:ReassignFocusOrders(x, y)
	x = x or self.focus_column
	y = y or 0
	
	if self.window_state == "destroying" then
		return y
	end
	
	for _, category_window in ipairs(self.idContainer) do
		if category_window:GetVisible() and category_window:HasMember("idCategory") then
			for _, prop_editor in ipairs(category_window.idCategory) do
				y = prop_editor:ReassignFocusOrders(x, y)
				assert(y)
			end
		end
	end
	return y
end

function GedPropPanel:UpdatePropertyNames(internal)
	for _, category_window in ipairs(self.idContainer) do
		for _, prop_editor in ipairs(rawget(category_window, "idCategory") or empty_table) do
			prop_editor:UpdatePropertyNames(internal)
		end
	end
end

function GedPropPanel:IsPropEditorVisible(prop_editor, filter_text, highlight_text)
	local tab = self.active_tab
	local tabs = self:GetTabsData()
	if tab ~= "All" and tabs then
		local prop = prop_editor.prop_meta
		local tab_data = table.find_value(tabs, "TabName", tab)
		if tab_data and not tab_data.Categories[prop.category or "Misc"] then
			return false
		end
	end
	return prop_editor:FindText(filter_text, highlight_text)
end

function GedPropPanel:UpdateVisibility()
	local values = self.context and self:Obj(self.context .. "|values")
	if not values then return end
	
	local filter_text = self:GetFilterText()
	local highlight_text = self:GetHighlightText()
	for _, category_window in ipairs(self.idContainer) do
		local hidden = 0
		local prop_editors = rawget(category_window, "idCategory") or empty_table
		for _, prop_editor in ipairs(prop_editors) do
			local visible = self:IsPropEditorVisible(prop_editor, filter_text, highlight_text)
			prop_editor:SetVisible(visible)
			if not visible then
				hidden = hidden + 1
			end
		end
		category_window:SetVisible(hidden ~= #prop_editors)
	end
end

function GedPropPanel:LocateEditorById(id)
	if self.window_state == "destroying" then return end
	for _, category_window in ipairs(self.idContainer) do
		if category_window:HasMember("idCategory") then
			for _, prop_editor in ipairs(category_window.idCategory) do
				if prop_editor.prop_meta.id == id then
					return prop_editor
				end
			end
		end
	end
end

function GedPropPanel:TryHighlightSearchMatch(skip_visibility_update)
	local match_data = self.app:GetDisplayedSearchResultData()
	local obj_id = self:Obj(self.context)
	if match_data and match_data.path[#match_data.path] == obj_id then
		if self.desktop:GetKeyboardFocus().Id ~= "idSearchEdit" then
			self:FocusPropEditor(match_data.prop)
		end
		self.app.display_search_result = false
	end
	if not skip_visibility_update then
		self:UpdateVisibility()
	end
end

function GedPropPanel:FocusPropEditor(prop_id)
	local old_focused = self.search_value_focused_prop_editor
	if old_focused and old_focused.parent and old_focused.window_state ~= "destroying" then
		old_focused:HighlightAndSelect(false)
	end
	
	local highlight_text = self:GetHighlightText()
	local prop_editor = self:LocateEditorById(prop_id)
	local focus = highlight_text and prop_editor and prop_editor:HighlightAndSelect(highlight_text)
	if focus then
		-- scroll the prop editor into the view
		local scrollarea = self.idContainer
		local parent = GetParentOfKind(scrollarea.parent, "XScrollArea")
		while parent do
			scrollarea = parent
			parent = GetParentOfKind(scrollarea.parent, "XScrollArea")
		end
		scrollarea:ScrollIntoView(focus)
	end
	self.search_value_focused_prop_editor = prop_editor
end

function GedPropPanel:ExpandCategory(container, category, expand, save_settings)
	self.collapsed_categories[category] = not expand
	if save_settings then
		self:SaveCollapsedCategories(self.context)
	end
	container:SetVisible(expand)
end

function GedPropPanel:SaveCollapsedCategories(key)
	local app = self.app
	local settings = app.settings.collapsed_categories or {}
	settings[key] = self.collapsed_categories
	app.settings.collapsed_categories = settings
	app:SaveSettings()
end

function GedPropPanel:ExpandCollapseCategories()
	if self.window_state == "destroying" then return end
	
	local has_expanded
	for _, category_window in ipairs(self.idContainer) do
		if category_window:HasMember("idCategory") and category_window.idCategory:GetVisible() then
			has_expanded = true
			break
		end
	end
	
	for _, category_window in ipairs(self.idContainer) do
		if category_window:HasMember("idCategory") then
			self:ExpandCategory(category_window.idCategory, category_window.category, not has_expanded, not "save_settings")
		end
	end
end


----- GedListPanel

DefineClass.GedListPanel = {
	__parents = { "GedPanel" },
	properties = {
		{ category = "General", id = "FormatFunc", editor = "text", default = "GedListObjects", },
		{ category = "General", id = "Format", editor = "text", default = "<name>", },
		{ category = "General", id = "AllowObjectsOnly", editor = "bool", default = false, },
		{ category = "General", id = "FilterName", editor = "text", default = "", },
		{ category = "General", id = "FilterClass", editor = "text", default = "", },
		{ category = "General", id = "SelectionBind", editor = "text", default = "", },
		{ category = "General", id = "OnDoubleClick", editor = "func", params = "self, item_idx", default = function(self, item_idx) end, },
		{ category = "General", id = "MultipleSelection", editor = "bool", default = false },
		
		{ category = "General", id = "EmptyText", editor = "text", default = "" },
		{ category = "General", id = "DragAndDrop", editor = "bool", default = false },
		
		{ category = "Common Actions", id = "ItemClass", editor = "expression", params = "gedapp", default = function(gedapp) return "" end, },
		{ category = "Context Menu", id = "ItemActionContext", editor = "text", default = "" },
	},
	ContainerControlClass = "XList",
	Interactive = true,
	EnableSearch = true,
	Translate = true,
	
	pending_update = false,
	pending_selection = false,
	restoring_state = false,
}

function GedListPanel:InitControls()
	GedPanel.InitControls(self)
	
	local list = self.idContainer
	list.OnSelection   = function(list, ...)          self:OnSelection(...)          end
	list.OnDoubleClick = function(list, item_idx)     self:OnDoubleClick(item_idx)   end
	list.CreateTextItem = function(list, ...) return  self:CreateTextItem(...)       end
	list:SetMultipleSelection(true)
	list:SetActionContext(self.ActionContext)
	list:SetItemActionContext(self.ItemActionContext)

	GedPanel.SetSearchActionContexts(self, self.SearchActionContexts)
end

function GedListPanel:Done()
	for bind in string.gmatch(self.SelectionBind .. ",", reCommaList) do
		self.connection:UnbindObj(bind)
	end
end

function GedListPanel:CreateTextItem(text, props, context)
	props = props or {}
	local item = XListItem:new({ selectable = props.selectable }, self.idContainer)
	props.selectable = nil
	local label = XText:new(props, item, context)
	label:SetText(text)
	item.idLabel = label
	if self.DragAndDrop then
		item.idDragAndDrop = GedListDragAndDrop:new({
			Dock = "box",
			List = self,
			Item = item,
		}, item)
	end
	return item
end

function GedListPanel:BindViews()
	if self.FilterName ~= "" and self.FilterClass then
		self.connection:BindFilterObj(self.context .. "|list", self.FilterName, self.FilterClass)
	end
	self:BindView("list", self.FormatFunc, self.Format, self.AllowObjectsOnly)
end

function GedListPanel:GetSelection()
	local selection = self.pending_selection or self.idContainer:GetSelection()
	if not selection or not next(selection) then return end
	return selection[1], selection
end

function GedListPanel:GetMultiSelection()
	return self.idContainer:GetSelection()
end

function GedListPanel:SetSelection(selection, multiple_selection, notify, restoring_state)
	if restoring_state or self.pending_update then
		self.pending_selection = multiple_selection or selection
		self.restoring_state = restoring_state
		return
	end
	self.idContainer:SetSelection(multiple_selection or selection, notify)
end

function GedListPanel:OnSelection(selected_item, selected_items)
	self:BindSelectedObject(selected_item, selected_items)
end

function GedListPanel:SetSelectionAndFocus(idx)
	local list = self.idContainer
	if list:GetSelection() == idx then
		self.app:TryHighlightSearchMatchInChildPanels(self)
	else
		-- focusing prevents issues with the focus restored to the leftmost panel (and selecting the wrong object) when cycling through search results
		local focus = self.desktop:GetKeyboardFocus()
		if not focus or focus.Id ~= "idSearchEdit" then
			self:SetFocus()
		end
		list:SetSelection(idx)
	end
end

function GedListPanel:TryHighlightSearchMatch()
	local obj_id = self:Obj(self.context)
	local match_data = self.app:GetDisplayedSearchResultData()
	if match_data then
		local match_path = match_data.path
		local match_idx = table.find(match_path, obj_id)
		if match_idx and match_idx < #match_path then
			local match_id = match_path[match_idx + 1]
			local list = self:Obj(self.context .. "|list")
			local ids = list.ids or empty_table
			for idx in ipairs(list) do
				if ids[idx] == match_id then
					self:SetSelectionAndFocus(idx)
					break
				end
			end
		end
	end
end

function GedListPanel:OnContextUpdate(context, view)
	GedPanel.OnContextUpdate(self, context, view)
	if view == nil then
		self.idContainer:SetSelection(false)
		self.pending_update = true
	end
	if view == "list" then
		self:UpdateContent()
		if self.search_values then
			self:StartUpdateFilterThread("not_user_initiated")
		end
		self:TryHighlightSearchMatch()
	end
	if view == "warning" and self.DisplayWarnings then
		self:UpdateContent() -- if the displayed items need to be underlined
	end
end

function GedListPanel:BindSelectedObject(selected_item, selected_indexes)
	if not selected_item then return end
	self.app:StoreAppState()
	for bind in string.gmatch(self.SelectionBind .. ",", reCommaList) do
		if self.MultipleSelection and selected_indexes and #selected_indexes > 1 then
			self.app:SelectAndBindMultiObj(bind, self.context, selected_indexes)
		else
			self.app:SelectAndBindObj(bind, { self.context, selected_item })
		end
	end
end

function GedListPanel:UpdateContent()
	if not self.context then return end
	local list = self:Obj(self.context .. "|list")
	if not list then
		self.idContainer:Clear()
		return
	end
	
	local sel = self.pending_selection or self.idContainer:GetSelection()
	local scroll_pos = self.idScroll.Scroll
	local filtered, ids = list.filtered, list.ids
	local filter_text = self:GetFilterText()
	if filter_text == "" then
		filter_text = false
	end
	local warning_idxs = self.DisplayWarnings and get_warning_nodes(self) or empty_table
	if #warning_idxs == 0 then
		warning_idxs = false
	end
	local container = self.idContainer
	container:Clear()
	local string_format = string.format
	for i, item_text in ipairs(list) do
		local str = "<literal(text,true)>"
		if warning_idxs and table.find(warning_idxs, i) then
			str = string_format("<underline>%s</underline>", str)
		end
		str = string.format("<read_only('%s')>%s</read_only('%s')>", self.context, str, self.context)
		local item_id = ids and ids[i]
		if item_id then
			str = string_format("<ged_marks('%s')>%s", item_id, str)
		end
		
		str = T{str, text = item_text, untranslated = true}
		local item = container:CreateTextItem(str, { Translate = true })
		if filtered and filtered[i] or filter_text and self:FilterItem(item_text, item_id, filter_text) then
			item:SetDock("ignore")
			item:SetVisible(false)
		end
	end

	if #list == 0 and self.EmptyText then
		self.idContainer:CreateTextItem(Untranslated(self.EmptyText), { Translate = true })
	end
	
	self.idScroll:ScrollTo(scroll_pos)
	self.idContainer:SetSelection(sel, self.pending_update and not self.restoring_state) -- notify only if selection is set externally
	self.pending_update = false
	self.pending_selection = false
	self.restoring_state = false
	Msg("XWindowRecreated", self)
end

function GedListPanel:FocusFirstEntry()
	for idx, value in ipairs(self.idContainer) do
		if value.Dock ~= "ignore" then
			self.idContainer:SetSelection(idx)
			break
		end
	end
	self:SetPanelFocused()
end

function GedListPanel:UpdateFilter()
	self:UpdateContent()
end

function GedListPanel:UpdateItemTexts()
	self:UpdateTitle(self.context)
	for _, item in ipairs(self.idContainer) do
		item[1]:SetText(item[1]:GetText())
	end
end

function GedListPanel:IsEmpty()
	local list = self:Obj(self.context .. "|list")
	return list and #list > 0
end


----- GedTreePanel

DefineClass.GedTreePanel = {
	__parents = { "GedPanel" },
	properties = {
		{ category = "General", id = "FormatFunc", editor = "text", default = "GedObjectTree", },
		{ category = "General", id = "Format", editor = "text", default = "<name>", },
		{ category = "General", id = "AltFormat", editor = "text", default = "", },
		{ category = "General", id = "AllowObjectsOnly", editor = "bool", default = false, },
		{ category = "General", id = "FilterName", editor = "text", default = "", },
		{ category = "General", id = "FilterClass", editor = "text", default = "", },
		{ category = "General", id = "SelectionBind", editor = "text", default = "", },
		{ category = "General", id = "OnSelectionChanged", editor = "func", params = "self, selection", default = function(self, selection) end, },
		{ category = "General", id = "OnCtrlClick", editor = "func", params = "self, selection", default = function(self, selection) end, },
		{ category = "General", id = "OnAltClick", editor = "func", params = "self, selection", default = function(self, selection) end, },
		{ category = "General", id = "OnDoubleClick", editor = "func", params = "self, selection", default = function(self, selection) end, },
		{ category = "General", id = "MultipleSelection", editor = "bool", default = false },
		{ category = "General", id = "DragAndDrop", editor = "bool", default = false },
		{ category = "General", id = "EnableRollover", editor = "bool", default = false },
		
		{ category = "General", id = "EmptyText", editor = "text", default = "" },
		
		-- Enables the actions for all nodes of a tree panel. By default actions are disabled for Group nodes but some tree panels don't have groups and can use this option.
		{ category = "Common Actions", id = "EnableForRootLevelItems", editor = "bool", default = false, },
		{ category = "Common Actions", id = "ItemClass", editor = "expression", params = "gedapp", default = function(gedapp) return "" end, },
		
		{ category = "Context Menu", id = "RootActionContext", editor = "text", default = "" },
		{ category = "Context Menu", id = "ChildActionContext", editor = "text", default = "" },

		{ category = "Layout", id = "FullWidthText", editor = "bool", default = false, },
		{ category = "Layout", id = "ShowToolbarButtons", name = "Show toolbar buttons", editor = "bool", default = true, help = "Show/hide the buttons in the TreePanel toolbar (expand/collapse tree, etc)", },
	},
	ContainerControlClass = "XTree",
	HorizontalScroll = true,
	EnableSearch = true,
	Interactive = true,
	Translate = true,
	
	expanding_node = false,
	currently_selected_path = false,
	pending_update = false,
	pending_selection = false,
	alt_format_enabled = false,
	filtered_tree = false,
	view_warnings_only = false,
	view_errors_only = false,
}

function GedTreePanel:InitControls()
	GedPanel.InitControls(self)
	
	local tree = self.idContainer
	tree:SetSortChildNodes(false)
	tree.GetNodeChildren = function(tree, ...) return self:GetNodeChildren(...) end
	tree.InitNodeControls = function(tree, ...) self:InitNodeControls(...) end
	tree.OnSelection = function(tree, selection, selected_indexes) self:OnSelection(selection, selected_indexes) end
	tree.OnCtrlClick = function(tree, ...) self:OnCtrlClick(...) end
	tree.OnAltClick = function(tree, ...) self:OnAltClick(...) end
	tree.OnUserExpandedNode = function(tree, path) self:OnUserExpandedNode(path) end
	tree.OnUserCollapsedNode = function(tree, path) self:OnUserCollapsedNode(path) end
	tree.OnDoubleClickedItem = function(tree, path) self:OnDoubleClick(path) end
	
	tree:SetActionContext(self.ActionContext)
	tree:SetRootActionContext(self.RootActionContext)
	tree:SetChildActionContext(self.ChildActionContext)
	tree:SetMultipleSelection(true)
	tree:SetFullWidthText(self.FullWidthText)
	GedPanel.SetSearchActionContexts(self, self.SearchActionContexts)
	
	local alt_format_button = XTemplateSpawn("GedToolbarToggleButtonSmall", self.idTitleContainer)
	alt_format_button:SetId("idAltFormatButton")
	alt_format_button:SetIcon("CommonAssets/UI/Ged/log-focused.tga")
	alt_format_button:SetRolloverText(T(185486815318, "Hide/show alternative names"))
	alt_format_button:SetBackground(self.idTitleContainer:GetBackground())
	alt_format_button:SetToggled(false)
	alt_format_button:SetFoldWhenHidden(true)
	alt_format_button:SetVisible(self.AltFormat and self.AltFormat ~= "")
	alt_format_button.OnPress = function(button)
		self.alt_format_enabled = not button:GetToggled()
		button:SetToggled(self.alt_format_enabled)
		self:BindView("tree", self.FormatFunc, self.alt_format_enabled and self.AltFormat or self.Format, self.AllowObjectsOnly)
	end

	if self.ShowToolbarButtons then
		local button = XTemplateSpawn("GedToolbarButtonSmall", self.idTitleContainer)
		button:SetIcon("CommonAssets/UI/Ged/collapse_node.tga")
		button:SetRolloverText("Expand/collapse selected node's children (Alt-C)")
		button:SetBackground(self.idTitleContainer:GetBackground())
		button.OnPress = function()
			self.idContainer:ExpandNodeByPath(self.idContainer:GetFocusedNodePath() or empty_table)
			self.idContainer:ExpandCollapseChildren(self.idContainer:GetFocusedNodePath() or empty_table, not "recursive", "user_initiated")
		end
		
		local button = XTemplateSpawn("GedToolbarButtonSmall", self.idTitleContainer)
		button:SetIcon("CommonAssets/UI/Ged/collapse_tree.tga")
		button:SetRolloverText("Expand/collapse tree (Shift-C)")
		button:SetBackground(self.idTitleContainer:GetBackground())
		button.OnPress = function() self.idContainer:ExpandCollapseChildren({}, "recursive", "user_initiated") end
	end
end

function GedTreePanel:Done()
	for bind in string.gmatch(self.SelectionBind .. ",", reCommaList) do
		self.connection:UnbindObj(bind)
	end
end

function GedTreePanel:BindViews()
	if self.FilterName ~= "" and self.FilterClass then
		self.connection:BindFilterObj(self.context .. "|tree", self.FilterName, self.FilterClass)
	end
	self:BindView("tree", self.FormatFunc, self.alt_format_enabled and self.AltFormat or self.Format, self.AllowObjectsOnly, self.EnableRollover)
	if not terminal.desktop:GetKeyboardFocus() then
		self:SetPanelFocused()
	end
end

local function try_index(root, key, ...)
	if key and root then
		return try_index(root[key], ...)
	end
	return root
end

function GedTreePanel:GetNodeChildren(...)
	if not self.filtered_tree then return end

	local texts, is_leaf, auto_expand, rollovers, user_datas = {}, {}, {}, {}, {}
	local node_data = try_index(self.filtered_tree, ...)
	local warning_idxs = self.filtered_tree == node_data and get_warning_nodes(self) or empty_table
	if type(node_data) == "table" then
		for i, subnode in ipairs(node_data) do
			if subnode then
				local read_only_wrapped_text = string.format("<read_only('%s')><literal(text,true)></read_only('%s')>", self.context, self.context)
				local format = subnode.id and ("<ged_marks('%s')>" .. read_only_wrapped_text) or read_only_wrapped_text
				if table.find(warning_idxs, i) then
					format = string.format("<underline>%s</underline>", format)
				end
				
				local str = subnode.id and string.format(format, subnode.id) or format
				texts[i] = T{str, text = subnode.name, untranslated = true}
				is_leaf[i] = #subnode == 0
				auto_expand[i] = not subnode.collapsed
				rollovers[i] = subnode.rollover
				user_datas[i] = subnode
			end
		end
	end
	return texts, is_leaf, auto_expand, rollovers, user_datas
end

GedTreePanel.OnShortcut = GedPanel.OnShortcut

local function new_item(self, child, class, path, button)
	local method = child and "GedGetSubItemClassList" or "GedGetSiblingClassList"
	local items = self.app:Call(method, self.context, path, self.app.ScriptDomain)
	local title = string.format("New %s", class)
	GedOpenCreateItemPopup(self, title, items, button, function(class)
		if self.window_state == "destroying" then return end
		self.app:Op("GedOpTreeNewItem", self.context, path, class, child and "child")
	end)
end

function GedTreePanel:InitNodeControls(node, data)
	if self.DragAndDrop then
		local label = node.idLabel
		if label then
			local l, u, r, b = label:GetPadding():xyxy()
			node.idDragAndDrop = GedTreeDragAndDrop:new({
				Dock = "box",
				Margins = box(-l, -u, -r, -b),
				NodeParent = node,
			}, label)
		end
	end

	local mode = data and data.child_button_mode
	if not mode then return end
	
	if data.child_class and (mode == "docked" or mode == "docked_if_empty" and #self.idContainer:GetChildNodes(node) == 0) then
		local button = XTemplateSpawn("GedPropertyButton", node)
		button:SetText("Add new...")
		button:SetDock(IsKindOf(node, "XTreeNode") and "bottom")
		button:SetZOrder(IsKindOf(node, "XTreeNode") and 0 or 1)
		button:SetMargins(IsKindOf(node, "XTreeNode") and box(40, 2, 0, 2) or box(20, 2, 0, 2))
		button:SetHAlign("left")
		button.OnPress = function() CreateRealTimeThread(new_item, self, "child", data.child_name or data.child_class, node.path, button) end
		Msg("XWindowRecreated", button)
		return
	end
	
	-- create floating button that appears on rollover at the right side of items
	if node == self.idContainer then return end
	if (mode == "floating" or mode == "children") and not data.child_class then return end
	if mode == "floating_combined" and not (data.child_class or data.sibling_class) then return end
	
	local new_parent = XWindow:new({ HandleMouse = true, HAlign = "left" }, node)
	node.idLabel:SetParent(new_parent)
	
	local create_button = XTemplateSpawn("GedToolbarButtonSmall", new_parent)
	create_button:SetId("idNew")
	create_button:SetDock("right")
	create_button:SetIcon("CommonAssets/UI/Ged/new.tga")
	create_button:SetVisible(not mode:starts_with("floating"))
	create_button:SetRolloverText("New " .. (data.child_name or data.child_class))
	create_button.OnPress = function(button)
		if not self:IsThreadRunning("CreateSubItem") then
			self:CreateThread("CreateSubItem", function(self, data)
				local mode = data.child_button_mode
				if mode == "floating_combined" and data.child_class and data.sibling_class then
					local host = XActionsHost:new({}, self)
					XAction:new({
						ActionId = "AddChild",
						ActionMenubar = "menu",
						ActionTranslate = false,
						ActionName = "Add child item...",
						OnAction = function() CreateRealTimeThread(new_item, self, "child", data.child_name or data.child_class, node.path, button) end,
					}, host)
					XAction:new({
						ActionId = "AddSibling",
						ActionMenubar = "menu",
						ActionTranslate = false,
						ActionName = "Add new item below...",
						OnAction = function() CreateRealTimeThread(new_item, self, not "child", data.sibling_name or data.sibling_class, node.path, button) end,
					}, host)
					host:OpenPopupMenu("menu", terminal.GetMousePos())
				elseif (mode == "floating_combined" or mode == "floating" or mode == "children") and data.child_class then
					new_item(self, "child", data.child_name or data.child_class, node.path, button)
				elseif mode == "floating_combined" and data.sibling_class then
					new_item(self, not "child", data.sibling_name or data.sibling_class, node.path, button)
				end
			end, self, data)
		end
	end
	create_button.button_mode = mode
	
	-- A thread to make the hovered button visible; OnSetRollover doesn't work as multiple nested items get rollovered at once
	if not self:IsThreadRunning("TreeItemRollover") then
		self:CreateThread("TreeItemRollover", function(self)
			local last
			while true do
				local focus = terminal.desktop.keyboard_focus
				if not focus or not GetParentOfKind(focus, "XPopup") then
					local pt = terminal.GetMousePos()
					local rollover = GetParentOfKind(terminal.desktop.modal_window:GetMouseTarget(pt), "XTreeNode")
					if rollover ~= last then
						if last and last.idNew and last.idNew.button_mode:starts_with("floating") then last.idNew:SetVisible(false) end
						last = rollover
						if last and last.idNew and last.idNew.button_mode:starts_with("floating") then last.idNew:SetVisible(true) end
					end
				end
				Sleep(50)
			end
		end, self)
	end	
end

function GedTreePanel:OnUserExpandedNode(path)
	self.connection:Send("rfnTreePanelNodeCollapsed", self.context, path, false)
	local entry = try_index(self.filtered_tree, table.unpack(path))
	if entry then entry.collapsed = false end
	local orig_entry = try_index(self:Obj(self.context.."|tree"), table.unpack(path))
	if orig_entry then orig_entry.collapsed = false end
end

function GedTreePanel:OnUserCollapsedNode(path)
	self.connection:Send("rfnTreePanelNodeCollapsed", self.context, path, true)
	local entry = try_index(self.filtered_tree, table.unpack(path))
	if entry then entry.collapsed = true end
	local orig_entry = try_index(self:Obj(self.context.."|tree"), table.unpack(path))
	if orig_entry then orig_entry.collapsed = true end
end

function GedTreePanel:GetSelection()
	return self.idContainer:GetSelection()
end

function GedTreePanel:GetMultiSelection()
	return table.pack(self:GetSelection())
end

function GedTreePanel:SetSelection(selection, selected_keys, notify, restoring_state)
	if type(selection) == "table" and type(selection[1]) == "table" then
		selected_keys = selection[2]
		selection = selection[1]
	end
	if restoring_state or self.pending_update then
		self.pending_selection = { selection, selected_keys }
		self.restoring_state = restoring_state
		return
	end
	self.idContainer:SetSelection(selection, selected_keys, notify)
end

function GedTreePanel:BindSelectedObject(selection, selected_keys)
	if not selection then return end
	self.app:StoreAppState()
	for bind in string.gmatch(self.SelectionBind .. ",", reCommaList) do
		if self.MultipleSelection and selected_keys and #selected_keys > 1 then
			self.app:SelectAndBindMultiObj(bind, { self.context, unpack_params(selection, 1, #selection - 1) }, selected_keys)
		else
			self.app:SelectAndBindObj(bind, { self.context, table.unpack(selection) })
		end
	end
end

function GedTreePanel:OnSelection(selection, selected_keys)
	self:BindSelectedObject(selection, selected_keys)
	local old_selection = self.currently_selected_path or empty_table
	if not table.iequal(old_selection, selection) then
		self:OnSelectionChanged(selection)
		self.currently_selected_path = selection
		self.app:ActionsUpdated()
	end
end

function GedTreePanel:SetSelectionAndFocus(path)
	local tree = self.idContainer
	if not tree then return end -- window destroying
	if table.iequal(tree:GetSelection() or empty_table, path) then
		self.app:TryHighlightSearchMatchInChildPanels(self)
	else
		-- focusing prevents issues with the focus restored to the leftmost panel (and selecting the wrong object) when cycling through search results
		local focus = self.desktop:GetKeyboardFocus()
		if not focus or focus.Id ~= "idSearchEdit" then
			self:SetFocus()
		end
		tree:SetSelection(path)
	end
end

function GedTreePanel:TryHighlightSearchMatch()
	local match_data = self.app:GetDisplayedSearchResultData()
	if match_data then
		local match_path = match_data.path
		local max_idx, path = 0
		self.idContainer:ForEachNode(function(node)
			local text = TDevModeGetEnglishText(node:GetText())
			for idx, obj_id in ipairs(match_path) do
				if string.find(text, obj_id, 1, true) then
					max_idx = Max(max_idx, idx)
					path = node.path
				end
			end
		end)
		
		if max_idx > 0 then
			self:SetSelectionAndFocus(path)
		end
	end
end

function GedTreePanel:OnContextUpdate(context, view)
	GedPanel.OnContextUpdate(self, context, view)
	if view == nil then
		self.idContainer:ClearSelection(false)
		self.pending_update = true
	end
	if view == "tree" then
		self:RebuildTree()
		local tree_empty = self:IsEmpty()
		if context == "bookmarks" then
			if tree_empty then
				self:Expand(false) -- collapse bookmarks panel when empty
			elseif not self.expanded then
				self:Expand(true) -- expand when adding a bookmark and panel is collapsed
			end
		end
		local first_update = self.app.first_update
		local sel = self.pending_selection or                         -- selection set before the tree data arrived
		   first_update and (tree_empty and {{}, {}} or {{1},{1}}) or -- default selection upon first editor open
		   table.pack(self.idContainer:GetSelection())                -- previous selection
		local scroll_pos = self.idScroll.Scroll
		self.idContainer:Clear() -- fills the tree control with content
		if tree_empty and self.context ~= "bookmarks" then
			self:OnSelection(empty_table) -- will bind "nothing" to child panels to empty them
			local text = XText:new({ ZOrder = 1, Margins = box(20, 2, 0, 2) }, self.idContainer)
			text:SetText(self.EmptyText)
			Msg("XWindowRecreated", text)
		else
			self.idContainer:SetSelection(sel[1], sel[2], first_update or self.pending_update and not self.restoring_state)
		end
		self.idScroll:ScrollTo(scroll_pos)
		self.pending_update = false
		self.pending_selection = false
		self.app.first_update = false
		self.restoring_state = false
		self:InitNodeControls(self.idContainer, self.filtered_tree)
		Msg("XWindowRecreated", self)
		
		if self.search_values then
			self:StartUpdateFilterThread("not_user_initiated")
		end
		self:TryHighlightSearchMatch()
	end
end

function GedTreePanel:SetViewWarningsOnly(mode)
	if self.view_warnings_only ~= mode then
		self.view_warnings_only = mode
		if self.idSearchEdit:GetText() ~= "" then
			self:CancelSearch()
		else
			self:UpdateFilter()
		end
	end
end

function GedTreePanel:SetViewErrorsOnly(mode)
	if self.view_errors_only ~= mode then
		self.view_errors_only = mode
		if self.idSearchEdit:GetText() ~= "" then
			self:CancelSearch()
		else
			self:UpdateFilter()
		end
	end
end

-- If it returns true - hide the item
function GedTreePanel:FilterItem(text, item_id, filter_text, has_child_nodes)
	local msg = get_warning_msg(item_id)
	local msg_type = msg and msg[#msg]
	
	local filter = GedPanel.FilterItem(self, text, item_id, filter_text)
	local children = not has_child_nodes
	
	-- Show only warnings/errors - 4 states
	if self.view_warnings_only and not self.view_errors_only then
		return filter or children and msg_type ~= "warning" -- only warnings
	elseif not self.view_warnings_only and self.view_errors_only then
		return filter or children and msg_type ~= "error" -- only errors
	elseif self.view_warnings_only and self.view_errors_only then
		return filter or children and not msg_type -- warning or error
	else
		return filter -- everything
	end
end

function GedTreePanel:BuildTree(root, filter_text)
	if not root or root.filtered then
		return false
	end
	local has_child_nodes = type(root) ~= "string" and #root > 0
	if (self.search_value_results or type(root) == "string") and self:FilterItem(root, root.id, filter_text, has_child_nodes) then
		return false
	end
	if type(root) == "string" then
		return { id = root.id, name = root }
	end
	
	local node = {
		id = root.id,
		name = root.name,
		class = root.class,
		child_button_mode = root.child_button_mode,
		child_class = root.child_class,
		child_name = root.child_name,
		sibling_class = root.sibling_class,
		sibling_name = root.sibling_name,
		rollover = root.rollover,
		collapsed = filter_text == "" and not self.view_warnings_only and not self.view_errors_only and root.collapsed
	}
	
	local visible = false
	for key, subnode in ipairs(root) do
		local child = self:BuildTree(subnode, filter_text) or false
		table.insert(node, child)
		if child then visible = true end
	end
	if visible or root.name and (self.search_value_results or not self:FilterItem(root.name, root.id, filter_text)) then
		return node
	end
end

function GedTreePanel:RebuildTree()
	local data = self:Obj(self.context .. "|tree")
	if not data then return end
	self.filtered_tree = self:BuildTree(data, self:GetFilterText()) or empty_table
end

function GedTreePanel:FocusFirstEntry()
	if self:GetFilterText() == "" then
		if not self:GetSelection() then
			self:SetSelection({1})
		end
		self:SetPanelFocused()
		return
	end
	
	local path = {}
	local root = self.filtered_tree
	while type(root) == "table" do
		local to_iterate = root
		root = nil
		for key, node in ipairs(to_iterate) do
			if node then
				root = node
				table.insert(path, key)
				break
			end
		end
	end
	self:SetSelection(path)
	self:SetPanelFocused()
end

function GedTreePanel:UpdateFilter()
	self:RebuildTree()
	local sel = table.pack(self.idContainer:GetSelection())
	local scroll_pos = self.idScroll.Scroll
	self.idContainer:Clear() -- fills the tree control with content
	self.idContainer:SetSelection(sel[1], sel[2], not "notify") -- restore selection as if nothing has changed (don't notify)
	self.idScroll:ScrollTo(scroll_pos)
	self:InitNodeControls(self.idContainer, self.filtered_tree)
	Msg("XWindowRecreated", self)
end

function GedTreePanel:UpdateItemTexts()
	self:UpdateTitle(self.context)
	self.idContainer:ForEachNode(function(node) node.idLabel:SetText(node.idLabel:GetText()) end)
end

function GedTreePanel:IsEmpty()
	return type(self.filtered_tree) ~= "table"
end

if FirstLoad then
	ged_drag_target = false
	ged_drag_initatior = false
	ged_drop_target = false
	ged_drop_type = false
end
DefineClass.GedDragAndDrop = {
	__parents = { "XDragAndDropControl", },
	ForEachSelectedDragTargetChild = empty_func,
	ForEachDragTargetChild = empty_func,
	GetGedDragTarget = empty_func,
	drop_target_container_class = "XTree",
	line_thickness = 2,
}

function GedDragAndDrop:GetDragAndDropError()
	if not ged_drag_target or not ged_drop_target then
		return true
	end
	local selected_children = {}
	self:ForEachSelectedDragTargetChild(function(selected_child, selected_children)
		selected_children[selected_child] = true
	end, selected_children)
	if not next(selected_children) then
		return true
	end
	local win = ged_drop_target
	while win and not IsKindOf(win, self.drop_target_container_class) do
		if selected_children[win] then
			return true
		end
		win = win.parent
	end
end

function GedDragAndDrop:OnMouseButtonDown(pt, button)
	if button ~= "L" then return end
	local res = XDragAndDropControl.OnMouseButtonDown(self, pt, button)
	if not self.drag_win and self.pt_pressed then
		self:SetModal()
	end
	return res
end

function GedDragAndDrop:OnMouseButtonUp(pt, button)
	if self.desktop.modal_window == self then
		self:SetModal(false)
	end
	return XDragAndDropControl.OnMouseButtonUp(self, pt, button)
end

function GedDragAndDrop:GetMouseCursor()
	if not self.enabled then return end
	if self.drag_win then
		if self:GetDragAndDropError() then
			return "CommonAssets/UI/ErrorCursor.tga"
		else
			return "CommonAssets/UI/HandCursor.tga"
		end
	end
end

function GedDragAndDrop:OnDragStart(pt, button)
	local drag_target = self:GetGedDragTarget()
	if not drag_target then return end
	ged_drag_target = drag_target
	ged_drag_initatior = self

	local drag_parent_win = XWindow:new({
		LayoutMethod = "VList",
		ZOrder = 10,
		Clip = false,
		HideOnEmpty = true,
		UseClipBox = false,
	}, self.desktop)

	self:ForEachSelectedDragTargetChild(function(child, drag_parent_win, self)
		return self:AddDragWindowText(child, drag_parent_win)
	end, drag_parent_win, self)
	
	if #drag_parent_win == 0 then
		drag_parent_win:delete()
		return
	end

	drag_parent_win:UpdateMeasure(drag_parent_win.MaxWidth, drag_parent_win.MaxHeight)
	local cursor_width = UIL.MeasureImage(GetMouseCursor())
	drag_parent_win:SetBox(pt:x() + cursor_width - 10, pt:y(), drag_parent_win.measure_width, drag_parent_win.measure_height, true)

	return drag_parent_win
end

function GedDragAndDrop:GetDragWindowTextControl(control)
	return control.idLabel
end

function GedDragAndDrop:AddDragWindowText(control, drag_parent_win)
	local label_win = self:GetDragWindowTextControl(control)
	if not label_win then return end
	XText:new({
		HAlign = "left",
		VAlign = "top",
		ZOrder = 10,
		Clip = false,
		TextStyle = GetDarkModeSetting() and "GedDefaultDarkMode" or "GedDefault",
		Translate = label_win.Translate,
		HideOnEmpty = true,
		UseClipBox = false,
		Background = RGBA(89, 126, 141, 100),
	}, drag_parent_win):SetText(label_win:GetText())
end

function GedDragAndDrop:UpdateDrag(drag_win, pt)
	XDragAndDropControl.UpdateDrag(self, drag_win, pt)
	if not ged_drag_target or ged_drag_target ~= self:GetGedDragTarget() then return end
	local drop_target = false
	if ged_drag_target.box:minx() < pt:x() and ged_drag_target.box:maxx() > pt:x() then
		self:ForEachDragTargetChild(function(child, self, py)
			local drop_target_box = self:GetDropTargetBox(child)
			if drop_target_box and drop_target_box:miny() <= py and drop_target_box:maxy() > py then
				drop_target = child
				return "break"
			end
		end, self, pt:y())
	end
	if drop_target ~= ged_drop_target then
		ged_drop_target = drop_target
		UIL.Invalidate()
	end
	if ged_drop_target then
		local drop_target_box = self:GetDropTargetBox()
		if not drop_target_box then return end
		local miny, maxy = drop_target_box:miny(), drop_target_box:maxy()
		local sizey = maxy - miny
		local dy = pt:y() - miny
		local pct = dy * 100 / sizey
		local new_drop_type = self:GetDropType(pct)
		if new_drop_type ~= ged_drop_type then
			ged_drop_type = new_drop_type
			UIL.Invalidate()
		end
	end
end

function GedDragAndDrop:GetDropTargetBox(drop_target)
	drop_target = drop_target or ged_drop_target
	return drop_target and drop_target.box
end

function GedDragAndDrop:GetDropType(pct)
	return pct < 50 and "Up" or "Down"
end

local function DrawHorizontalLine(width, pt1, pt2, color)
	local x1, y1 = pt1:xy()
	local x2, y2 = pt2:xy()
	for i = -width/2, width - width/2 - 1 do
		UIL.DrawLine(point(x1, y1 + i), point(x2, y2 + i), color)
	end
end

local highlight_color = RGB(62, 165, 165)
local highlight_color_error = RGB(197, 128, 128)
function GedDragAndDropHighlight()
	if not ged_drag_initatior or not ged_drop_target then return end
	local color = ged_drag_initatior:GetDragAndDropError() and highlight_color_error or highlight_color
	local drop_target_box = ged_drag_initatior:GetDropTargetBox()
	local x_padding = ged_drag_initatior.line_thickness/2
	if ged_drop_type == "Up" then
		DrawHorizontalLine(ged_drag_initatior.line_thickness, point(drop_target_box:minx() - x_padding, drop_target_box:miny() + 1), point(drop_target_box:maxx() + x_padding, drop_target_box:miny() + 1), color)
	elseif ged_drop_type == "Down" then
		DrawHorizontalLine(ged_drag_initatior.line_thickness, point(drop_target_box:minx() - x_padding, drop_target_box:maxy() + 1), point(drop_target_box:maxx() + x_padding, drop_target_box:maxy() + 1), color)
	else
		UIL.DrawBorderRect(box(drop_target_box:minx() - x_padding, drop_target_box:miny() - 1 + ged_drag_initatior.line_thickness%2, drop_target_box:maxx() + x_padding, drop_target_box:maxy() + 1), ged_drag_initatior.line_thickness, ged_drag_initatior.line_thickness, color, RGBA(0, 0, 0, 0))
	end
end

function OnMsg.Start()
	if Platform.desktop then
		UIL.Register("GedDragAndDropHighlight", XDesktop.terminal_target_priority + 1)
	end
end

function GedDragAndDrop:OnDragEnded(drag_win, last_target, drag_res)
	drag_win:delete()
	ged_drag_target = false
	ged_drag_initatior = false
	ged_drop_target = false
	ged_drop_type = false
	UIL.Invalidate()
end

function GedDragAndDrop:GetDropTarget(...)
	if ged_drop_target then
		return ged_drop_target.idDragAndDrop
	end
	return XDragAndDropControl.GetDropTarget(self, ...)
end

DefineClass.GedTreeDragAndDrop = {
	__parents = { "GedDragAndDrop", },
	NodeParent = false,
}

function GedTreeDragAndDrop:ForEachSelectedDragTargetChild(func, ...)
	local panel = GetParentOfKind(ged_drag_target, "GedTreePanel")
	if not panel then return end
	local first_selected, selected_idxs = panel:GetSelection()
	if not first_selected then return end
	local parent = ged_drag_target:NodeByPath(first_selected, #first_selected - 1, "allow_root")
	local children = ged_drag_target:GetChildNodes(parent)
	for _, idx in ipairs(selected_idxs) do
		local selected_child = children[idx]
		if func(selected_child, ...) == "break" then
			return "break"
		end
	end
end

function GedTreeDragAndDrop:GetGedDragTarget()
	return self.NodeParent.tree
end

function GedTreeDragAndDrop:ForEachDragTargetChild(func, ...)
	if not IsKindOf(ged_drag_target, self.drop_target_container_class) then return end
	return ged_drag_target:ForEachNode(func, ...)
end

function GedTreeDragAndDrop:GetDropTargetBox(drop_target)
	drop_target = drop_target or ged_drop_target
	return drop_target and drop_target.idLabel and drop_target.idLabel.box
end

function GedTreeDragAndDrop:GetDropType(pct)
	if pct < 20 then
		return "Up"
	elseif pct > 80 and (not ged_drop_target or #ged_drop_target.idSubtree == 0) then
		return "Down"
	else
		return "In"
	end
end

gedTreeDragAndDropOps = {
	Up   = "GedOpTreeDropItemUp",
	Down = "GedOpTreeDropItemDown",
	In   = "GedOpTreeDropItemInwards",
}
function GedTreeDragAndDrop:OnDrop(drag_win, pt, drag_source_win)
	if self:GetDragAndDropError() then return end
	local panel = GetParentOfKind(ged_drop_target, "GedTreePanel")
	local op = gedTreeDragAndDropOps[ged_drop_type]
	panel.app:Op(op, panel.context, panel:GetMultiSelection(), table.copy(ged_drop_target.path))
end

DefineClass.GedListDragAndDrop = {
	__parents = { "GedDragAndDrop", },
	drop_target_container_class = "XList",
	List = false,
	Item = false,
}

function GedListDragAndDrop:ForEachSelectedDragTargetChild(func, ...)
	local list = GetParentOfKind(ged_drag_target, "GedListPanel")
	if not list then return end
	local selection = ged_drag_target:GetSelection()
	for _, idx in ipairs(selection) do
		local selected_child = ged_drag_target[idx]
		if func(selected_child, ...) == "break" then
			return "break"
		end
	end
end

function GedListDragAndDrop:GetGedDragTarget()
	return self.List.idContainer
end

function GedListDragAndDrop:ForEachDragTargetChild(func, ...)
	if not IsKindOf(ged_drag_target, self.drop_target_container_class) then return end
	for _, child in ipairs(ged_drag_target) do
		if func(child, ...) == "break" then
			return "break"
		end
	end
end

gedListDragAndDropOps = {
	Up   = "GedOpListDropUp",
	Down = "GedOpListDropDown",
}
function GedListDragAndDrop:OnDrop(drag_win, pt, drag_source_win)
	if self:GetDragAndDropError() then return end
	local panel = GetParentOfKind(ged_drop_target, "GedListPanel")
	local op = gedListDragAndDropOps[ged_drop_type]
	panel.app:Op(op, panel.context, panel:GetMultiSelection(), table.find(panel.idContainer, ged_drop_target))
end

----- GedBreadcrumbPanel

DefineClass.GedBreadcrumbPanel = {
	__parents = { "GedPanel" },
	properties = {
		{ category = "General", id = "FormatFunc", editor = "text", default = "GedFormatObject", },
		{ category = "General", id = "TreePanelId", editor = "text", default = "", },
	},
	ContainerControlClass = "XWindow",
	MaxWidth = 1000000,
}

function GedBreadcrumbPanel:InitControls()
	GedPanel.InitControls(self)
	self.idContainer.LayoutMethod = "HWrap"
end

function GedBreadcrumbPanel:BindViews()
	self:BindView("path", self.FormatFunc)
end

function GedBreadcrumbPanel:OnContextUpdate(context, view)
	GedPanel.OnContextUpdate(self, context, view)
	if view == "path" then
		local pathdata = self:Obj(self.context .. "|path")
		self.idContainer:DeleteChildren()
		for k, entry in ipairs(pathdata) do
			local button = XButton:new({
				OnPress = function(button)
					self.app[self.TreePanelId]:SetSelection(entry.path)
				end,
				Background = RGBA(0, 0, 0, 0),
				RolloverBackground = RGBA(72, 72, 72, 255),
			}, self.idContainer)
			XText:new({}, button):SetText(entry.text)
			if k < #pathdata then
				XText:new({}, self.idContainer):SetText("<color 32 128 32> >> </color>")
			end
		end
		for _, win in ipairs(self.idContainer) do
			win:Open()
		end
		Msg("XWindowRecreated", self)
	end
end


----- GedTextPanel

DefineClass.GedTextPanel = {
	__parents = { "GedPanel", "XFontControl" },
	properties = {
		{ category = "General", id = "FormatFunc", editor = "text", default = "GedFormatObject", },
		{ category = "General", id = "Format", editor = "text", default = "", },
		{ category = "General", id = "AutoHide", editor = "bool", default = true, },
	},
	ContainerControlClass = "XText",
	MaxWidth = 1000000,
	TextStyle = "GedTextPanel",
}

LinkFontPropertiesToChild(GedTextPanel, "idContainer")

function GedTextPanel:InitControls()
	GedPanel.InitControls(self)

	local text = self.idContainer
	text:SetEnabled(false)
	text:SetBorderWidth(0)
	text:SetFontProps(self)
	text:SetWordWrap(false)
end

function GedTextPanel:GetView()
	return self.FormatFunc .. "." .. self.Format -- generate unique view id, to make sure different GedTextPanels won't clash
end

function GedTextPanel:BindViews()
	self:BindView(self:GetView(), self.FormatFunc, self.Format)
end

function GedTextPanel:GetTextToDisplay()
	return self:Obj(self.context .. "|" .. self:GetView()) or ""
end

function GedTextPanel:OnContextUpdate(context, view)
	GedPanel.OnContextUpdate(self, context, view)
	
	if self.window_state == "open" then
		local text = self:GetTextToDisplay()
		self.idContainer:SetText(text)
		self:SetVisible(not self.AutoHide or text ~= "")
	end
end

DefineClass.GedMultiLinePanel = {
	__parents = { "GedTextPanel" },
	ContainerControlClass = "XMultiLineEdit",
	TextStyle = "GedMultiLine",
}

function GedMultiLinePanel:InitControls()
	GedTextPanel.InitControls(self)
	self.idContainer:SetPlugins({ "XCodeEditorPlugin" })
end


----- GedObjectPanel

DefineClass.GedObjectPanel = {
	__parents = { "GedPanel" },
	properties = {
		{ category = "General", id = "FormatFunc", editor = "text", default = "GedInspectorFormatObject", },
	},
	ContainerControlClass = "XScrollArea",
	MaxWidth = 1000000,
	HorizontalScroll = true,
	Interactive = true,
}

function GedObjectPanel:BindViews()
	self:BindView("objectview", self.FormatFunc)
end

function GedObjectPanel:OnContextUpdate(context, view)
	GedPanel.OnContextUpdate(self, context, view)
	if view == "objectview" then
		self.idContainer:DeleteChildren()
		
		local objectview = self:Obj(self.context .. "|objectview")
		if not objectview then return end
		
		local metatable_id = objectview.metatable_id
		local text = objectview.name .. (objectview.metatable_id and (" [ <color 128 128 216> <h OpenKey 1>" .. objectview.metatable_name .. "</h></color> ]") or "")
		local child = XText:new({
			TextStyle = self.app.dark_mode and "GedTitleDarkMode" or "GedTitle",
		}, self.idContainer)
		child:SetText(text)
		child.OnHyperLink = function(this, id, ...)
			if id == "OpenKey" then
				self.app:Op("GedOpBindObjByRefId", "root", objectview.metatable_id, terminal.IsKeyPressed(const.vkControl)) 
			end
		end
		
		local search_text = self.idSearchEdit:GetText()
		for _, v in ipairs(objectview.members) do
			local key = v.key
			local val = v.value
			local count = v.count
			local value_id = v.value_id
			local key_id = v.key_id
			
			local text = (key_id and "<color 128 128 216><h OpenKey q>" .. key .. "</h></color>" or key) .. " = " .. (value_id and "<color 128 128 216><h OpenValue 2>" .. val .. "</h></color>"  or val) .. (count and "<color 100 200 220> (#" .. count .. ")</color>" or "")
			local child = XText:new({
				TextStyle = self.app.dark_mode and "GedDefaultDarkMode" or "GedDefault",
			}, self.idContainer)
			child:SetText(text)
			child.OnHyperLink = function(this, id, ...)
				if id == "OpenKey" then
					self.app:Op("GedOpBindObjByRefId", "root", key_id, terminal.IsKeyPressed(const.vkControl)) 
				elseif id == "OpenValue" then
					self.app:Op("GedOpBindObjByRefId", "root", value_id, terminal.IsKeyPressed(const.vkControl))
				end	
			end
			
			self:UpdateChildVisible(child, search_text)
		end
		self.idContainer:ScrollTo(0, 0)
	end	
end

function GedObjectPanel:UpdateChildVisible(child, search_text)
	if search_text ~= "" and not string.find_lower(string.strip_tags(child.Text), search_text) then 
		child:SetDock("ignore")
		child:SetVisible(false)
	else
		child:SetDock(false)
		child:SetVisible(true)
	end
end

-- Called when there's a change in the search bar
function GedObjectPanel:UpdateFilter()
	local search_text = self.idSearchEdit:GetText()
	for _, container in ipairs(self) do
		if container.Id == "idContainer" then
			for idx, child in ipairs(container) do
				if idx ~= 1 then
					self:UpdateChildVisible(child, search_text) -- hide the items that don't contain the search string and show the rest
				end
			end
		end
	end
end


----- GedGraphEditorPanel

DefineClass.GedGraphEditorPanel = {
	__parents = { "GedPanel" },
	properties = {
		{ category = "General", id = "SelectionBind", editor = "text", default = "", },
	},
	
	ContainerControlClass = "XGraphEditor",
}

function GedGraphEditorPanel:InitControls()
	GedPanel.InitControls(self)
	
	local graph = self.idContainer
	graph:SetReadOnly(true)
	graph.OnGraphEdited = function()
		self:Op("GedSetGraphData", "SelectedPreset", self.idContainer:GetGraphData())
	end
	graph.OnNodeSelected = function(graph, node)
		for bind_name in string.gmatch(self.SelectionBind .. ",", reCommaList) do
			self:Send("GedBindGraphNode", "SelectedPreset", node.handle, bind_name)
		end
	end
	graph.NodeClassItems = g_GedApp.ContainerGraphItems
end

function GedGraphEditorPanel:BindViews()
	if not self.context then return end
	self:BindView("graph", "GedGetGraphData")
end

function GedGraphEditorPanel:OnContextUpdate(context, view)
	if view == "graph" then
		local data = self:Obj(context .. "|graph") -- will be nil if a non-preset is selected
		self.idContainer:SetGraphData(data)
		self.idContainer:SetReadOnly(not data)
	end
	GedPanelBase.OnContextUpdate(self, context, view)
end


----- XPanelSizer

DefineClass.XPanelSizer = 
{
	__parents = { "XControl" },
	
	properties = {
		{ category = "Visual", id = "Cursor", editor = "ui_image", force_extension = ".tga", default = "CommonAssets/UI/Controls/resize03.tga" },
		{ category = "Visual", id = "BorderSize", editor = "number", default = 3, },
	},
	is_horizontal = true,
	drag_start_mouse_pos = false, 
	drag_start_panel1_max_sizes = false,
	drag_start_panel2_max_sizes = false,
	
	panel1 = false, 
	panel2 = false,
	valid = true,
}

function XPanelSizer:Open(...)
	local layout_method = self.parent.LayoutMethod
	assert(layout_method == "HPanel" or layout_method == "VPanel")
	self.is_horizontal = layout_method == "HPanel"

	if self.is_horizontal then
		self:SetMaxWidth(self.BorderSize)
		self:SetMinWidth(self.BorderSize)
	else
		self:SetMaxHeight(self.BorderSize)
		self:SetMinHeight(self.BorderSize)
	end
	
	if not self.panel1 or not self.panel2 then
		local current_index = table.find(self.parent, self)
		assert(current_index)
		if current_index then 
			for i = current_index - 1, 1, -1 do
				if not self.parent[i].Dock then
					assert(not IsKindOf(self.parent[i], "XPanelSizer"))
					self.panel1 = self.parent[i]
					break
				end
			end
			for i = current_index + 1, #self.parent do
				if not self.parent[i].Dock then
					assert(not IsKindOf(self.parent[i], "XPanelSizer"))
					self.panel2 = self.parent[i]
					break
				end
			end
		end
		if not self.panel1 or not self.panel2 then 
			self.valid = false 
		end
	end
end

function XPanelSizer:OnMouseButtonDown(pos, button)
	if not self.valid then return "break" end
	if button == "L" then
		self:SetFocus()
		self.desktop:SetMouseCapture(self)		
		self.drag_start_mouse_pos = pos
		self.drag_start_panel1_max_size = point(self.panel1.MaxWidth, self.panel1.MaxHeight)
		self.drag_start_panel2_max_size = point(self.panel2.MaxWidth, self.panel2.MaxHeight)
	end
	return "break"
end

function XPanelSizer:OnMousePos(new_pos)
	if self.valid and self.desktop:GetMouseCapture() == self then
		self:MovePanel(new_pos)
	end
	return "break"
end

local MulDivRoundPoint = MulDivRoundPoint

local function ElementwiseMax(min_value, point2)
	return point(Max(min_value, point2:x()), Max(min_value, point2:y()))
end

function XPanelSizer:MovePanel(new_pos)
	local old_pos = self.drag_start_mouse_pos
	local diff = new_pos - old_pos
	
	-- code is analogous to HPanel/VPanel's measure and layout methods
	local total_size = point(0, 0)
	local min_sizes = point(0, 0)
	local total_items = 0
	local panel_pixel_sizes = point(0, 0)
	for _, win in ipairs(self.parent) do
		if not win.Dock and not IsKindOf(win, "XPanelSizer") then
			local min_width, min_height, max_width, max_height = ScaleXY(win.scale, win.MinWidth, win.MinHeight, win.MaxWidth, win.MaxHeight)
			total_size = total_size + point(max_width, max_height)
			min_sizes = min_sizes + point(min_width, min_height)
			total_items = total_items + 1
			panel_pixel_sizes = panel_pixel_sizes + win.box:size()
		end
	end
	if self.is_horizontal then
		assert(total_size:x() >= 100000, "MaxWidths of HPanel's children should be > 1000000")
	else
		assert(total_size:y() >= 100000, "MaxHeights of VPanel's children should be > 1000000")
	end
	
	local pixels_to_distribute = panel_pixel_sizes - min_sizes
	if pixels_to_distribute:x() == 0 or pixels_to_distribute:y() == 0 then return end
	local pixels_to_max_space_units = MulDivRoundPoint(total_size - min_sizes, 1000, pixels_to_distribute)
	
	local prop_diff = MulDivRoundPoint(diff, pixels_to_max_space_units, 1000)
	local min_diff = MulDivRoundPoint(self.panel1.scale,
						- ElementwiseMax(0, self.drag_start_panel1_max_size - point(self.panel1.MinWidth, self.panel1.MinHeight)), 1000)
	local max_diff = MulDivRoundPoint(self.panel2.scale,
						ElementwiseMax(0, self.drag_start_panel2_max_size - point(self.panel2.MinWidth, self.panel2.MinHeight)), 1000)
	prop_diff = ClampPoint(prop_diff, box(min_diff, max_diff))
	
	local panel1_new_max_size = self.drag_start_panel1_max_size + MulDivRoundPoint(prop_diff, 1000, self.panel1.scale)
	local panel2_new_max_size = self.drag_start_panel2_max_size - MulDivRoundPoint(prop_diff, 1000, self.panel2.scale)
	
	if self.is_horizontal then
		self.panel1.MaxWidth = panel1_new_max_size:x()
		self.panel2.MaxWidth = panel2_new_max_size:x()
	else
		self.panel1.MaxHeight = panel1_new_max_size:y()
		self.panel2.MaxHeight = panel2_new_max_size:y()
	end
	
	self.panel1:InvalidateMeasure()
	self.panel2:InvalidateMeasure()
	self.parent:InvalidateLayout()
	self.parent:UpdateLayout()
end

function XPanelSizer:GetMouseTarget(pos)
	return self, self.Cursor
end

function XPanelSizer:OnMouseButtonUp(pos, button)
	if self.valid and self.desktop:GetMouseCapture() == self and button == "L" then
		self:OnMousePos(pos)
		self.desktop:SetMouseCapture()
	
		self.drag_start_mouse_pos = false
	end
	return "break"
end