Source

boa-constructor / Companions / BaseCompanions.py

   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
#-------------------------------------------------------------------------------
# Name:        BaseCompanions.py
# Purpose:     Classes that 'shadow' controls. They implement design time
#              behaviour and interfaces
#
# Author:      Riaan Booysen
#
# Created:     1999
# RCS-ID:      $Id$
# Copyright:   (c) 1999 - 2007 Riaan Booysen
# Licence:     GPL
#-------------------------------------------------------------------------------

""" Classes that 'shadow' controls.

They implement design time behaviour and interfaces. Also used for inspectable
objects """

print 'importing Companions'

import copy

import wx

import Preferences, Utils
from Utils import _

from PropEdit.PropertyEditors import *
from Constructors import WindowConstr
import RTTI, EventCollections

import methodparse, sourceconst

""" Design time classes
        These are companion classes to wxPython classes that capture
        design-time behaviour like
          * Constructor parameters
          * Events
          * Property editors

    XXX Todo XXX

    * write/wrap many more classes
    * streaming of properties
        * xml option
        * handling of default values
    * define event and name for popup menu on component
    * overrideable method or new multi inheritance class
      for palette creation in container companions,

"""

class Companion:
    """ Default companion, entity with a name and default documentation """
    def __init__(self, name):
        self.name = name
    def getPropertyHelp(self, propName):
        return propName

class CodeCompanion(Companion):
    pass

class DesignTimeCompanion(Companion):
    """ Base class for all companions participating in the design-time process.
    """
    handledConstrParams = ()
    suppressWindowId = False
    def __init__(self, name, designer):
        Companion.__init__(self, name)
        self.parentCompanion = None
        self.designer = designer
        # Design time window id
        self.dId = wx.NewId()
        self.id = None

        # Property editors for properties whose types cannot be deduced
        self.editors = {'Class': ClassConstrPropEdit}
        # Enumerated values for the options of a property editor
        self.options = {}
        # Enumerated display values for the options of a property editor
        # XXX Change to something less generic
        self.names = {}

        # Companion methods that must be called when a property changes
        self.triggers = {'Name': self.SetName}
        # Companions for properties for which a companion can not be deduced
        self.subCompanions = {}
        # Parsers for special properties, given string value should
        # return valid wxPython object
        # The property evaluator should return a tuple of arguments
        # as customPropEvaluators are also used to initialise multi parameter
        # properties
        self.customPropEvaluators = {}
        # Properties that should be initialised thru the companion instead of
        # directly on the control. Usually this applies to 'write-only'
        # properties whose values cannot otherwise be determined
        # It's keyed on the property name and if the setter does not start
        # with Set*, it's keyed on the setter name
        self.initPropsThruCompanion = []
        # Run time dict of created collection companions
        self.collections = {}
        # Can't work, remove
        self.letClickThru = False
        # Mutualy depentent props
        # These props will all be refeshed in the inspector when any one of
        # them is updated
        self.mutualDepProps = []
        # Flag for controls which do not process mouse events correctly
        self.ctrlDisabled = False
        self.compositeCtrl = False
        #
        self.resourceImports = []

        # Parse objects for reading in and writing out source
        self.textConstr = None
        self.textPropList = []
        self.textEventList = []
        self.textCollInitList = []

    def destroy(self):
        del self.triggers

    def constructor(self):
        """ This method must be overriden by defining it in in another class
            and multiply inheriting from this new class and a
            DesignTimeCompanion derivative. This allows groups of components
            having the same constructor to be created."""

        return {}

    def extraConstrProps(self):
        return {}

#    def defaults(self):
#        return {}

    def getPropList(self):
        propList = RTTI.getPropList(self.control, self)
        pw = RTTI.PropertyWrapper('Class', 'CompnRoute', self.GetClass, self.SetClass)
        propList['constructor'].append(pw)
        return propList

    def GetClass(self, dummy=None):
        """ Used by the Inspector to display the type of the selected object """
        return self.textConstr.class_name

    def SetClass(self, value):
        """ Used by the Inspector to display the type of the selected object """
        self.textConstr.class_name = value

    def properties(self):
        """ Properties additional to those gleened thru reflection.
            Dictionary key=propname : type, val=getter, setter tuple.
            type = 'CtrlRoute' : Routed to get/setters on the control
                   'CompnRoute': Routed to get/setters on the companion
        """

        return {}

    def setConstr(self, constr):
        """ Define a new constructor for source code, called when a component is
            parsed from source
            See also: persistConstr """
        self.textConstr = constr

    def setProps(self, propList):
        # XXX Should companion initialise comp props instead of Designer?
        self.textPropList = propList

    def setCollInits(self, collInitList):
        self.textCollInitList = collInitList

    def setEvents(self, eventList):
        self.textEventList = eventList

    def getEvents(self):
        return self.textEventList

    def getWinId(self):
        return self.id

    def hideDesignTime(self):
        """ Property names of automatically picked up properties that should
            not be shown in the Inspector. """
        return []

    def dontPersistProps(self):
        """ Properties are live (i.e. read/write) at design time but whose
            changes won't be applied to source. This is for cascading type
            properties like Size vs ClientSize. Updating one will automatically
            update the other so only one of them has to be stored."""
        return ['Class']

    def onlyPersistProps(self):
        """ Properties that should not be applied at design-time and should
            only be applied to the source """
        return []

    def events(self):
        return []

    def editor(self):
        pass

    def vetoedMethods(self):
        return []

##    def links(self):
##        return []

    # Rename to links
    def dependentProps(self):
        """ These are properties that depend on other controls already
            being created. They will be initialised right at the end of
            the definition block
        """
        return []

    def applyRunTime(self):
        """ Properties whose value will be modifyable at design-time
            and whose changes will be applied to the source but will
            not be applied to the controls at design time e.g. Show/Enable.
        """
        pass

    def getPropEditor(self, prop):
        if prop in self.editors: return self.editors[prop]
        else: return None

    def getPropOptions(self, prop):
        if prop in self.options: return self.options[prop]
        else: return None

    def getPropNames(self, prop):

        if prop in self.names:
            return self.names[prop]
        else:
            return None

    def evtGetter(self, name):
        for evt in self.textEventList:
            if evt.event_name == name: return evt.trigger_meth
        return None

    def evtSetter(self, name, value):
        for evt in self.textEventList:
            if evt.event_name == name:
                evt.trigger_meth = value
                return

    def persistConstr(self, className, params):
        """ Define a new constructor for source code, called when creating a
            new component from the palette
            See also: setConstr """

        paramStrs = []
        for param in params.keys():
            paramStrs.append('%s = %s'%(param, params[param]))

        # XXX Is frame name initialised ???
        self.textConstr = methodparse.ConstructorParse('self.%s = %s(%s)' %(
              self.name, className, ', '.join(paramStrs)))

        self.designer.addCtrlToObjectCollection(self.textConstr)

    def persistCollInit(self, method, ctrlName, propName, params = {}):
        """ Define a new collection init method for source code, called when
            creating a new item in CollectionEditor
        """

        collInitParse = methodparse.CollectionInitParse(None, ctrlName, method,
          [], propName)

        self.parentCompanion.textCollInitList.append(collInitParse)

        self.designer.addCollToObjectCollection(collInitParse)

    def checkTriggers(self, name, oldValue, newValue):
        #trigger specially handled callbacks for property changes with consequences
        if name in self.triggers:
            self.triggers[name](oldValue, newValue)

    def getCompName(self):
        if id(self.control) == id(self.designer):
            return ''
        else:
            return self.name

    def persistProp(self, name, setterName, value):
        c = self.constructor()
        #constructor
        if name in c:
            self.textConstr.params[c[name]] = value
        #property
        elif name not in self.dontPersistProps():
            for prop in self.textPropList:
                if prop.prop_setter == setterName:
                    prop.params = [value]
                    return

            self.textPropList.append(methodparse.PropertyParse( \
                None, self.getCompName(), setterName, [value], name))

    def persistedPropVal(self, name, setterName):
        c = self.constructor()
        #constructor
        if name in c:
            return self.textConstr.params[c[name]]
        #property
        elif name not in self.dontPersistProps():
            for prop in self.textPropList:
                try:
                    if prop.prop_setter == setterName:
                        return prop.params
                except:
                    #print 'except in persistprop'
                    raise
        return None

    def propRevertToDefault(self, name, setterName):
        """ Removes property methods from source and revert constructor
            parameters to default values """
        c = self.constructor()
        #constructor
        if name in c:
            defVal = self.designTimeSource()[c[name]]
            self.textConstr.params[c[name]] = defVal
        #property
        elif name not in self.dontPersistProps():
            idx = 0
            while idx < len(self.textPropList):
                prop = self.textPropList[idx]
                if prop.prop_setter == setterName:
                    del self.textPropList[idx]
                else:
                    idx = idx + 1

    def propIsDefault(self, name, setterName):
        """ Returns True if no modification has been made to the property
            or constructor parameter """
        c = self.constructor()
        #constructor
        if name in c:
            try:
                dts = self.designTimeSource()
            except TypeError:
                return True
            else:
                if c[name] in dts:
                    defVal = self.designTimeSource()[c[name]]
                    return self.textConstr.params[c[name]] == defVal
                else:
                    return True
        #property
        elif name not in self.dontPersistProps():
            for prop in self.textPropList:
                if prop.prop_setter == setterName:
                    return False
        return True

    def persistEvt(self, name, value, wId = None):
        """ Add a source entry for an event or update the trigger method of
            am existing event. """
        for evt in self.textEventList:
            if evt.event_name == name:
                evt.trigger_meth = value
                return
        if self.control == self.designer or \
              not isinstance(self.control, wx.EvtHandler) or \
              isinstance(self.control, wx.Timer):
            comp_name = ''
        else:
            comp_name = self.name
        self.textEventList.append(methodparse.EventParse(None, comp_name, name,
                                                         value, wId))

    def evtName(self):
        return self.name

    def addIds(self, lst):
        if self.id is not None:
            # generate default win id for list when reserved one is used by control
            if self.id in EventCollections.reservedWxIds:
                wId = Utils.windowIdentifier(self.designer.GetName(), self.name)
            else:
                wId = self.id
            lst.append(wId)

    def renameEventListIds(self, wId):
        for evt in self.textEventList:
            if evt.windowid and evt.windowid not in EventCollections.reservedWxIds:
                evt.windowid = wId

    def setProp(self, name, value):
        """ Optional callback companions can override for extra functionality
            after updating a property e.g. refreshing """
        pass

    def SetName(self, oldValue, newValue):
        """ Triggered when the 'Name' property is changed """
        if newValue in self.designer.objects:
            wx.LogError(_('There is already an object named %s')%newValue)
        else:
            self.name = newValue
            self.designer.model.renameCtrl(oldValue, newValue)
            self.designer.renameCtrl(oldValue, newValue)

    def renameCtrl(self, oldName, newName):
        self.textConstr.comp_name = newName
        for prop in self.textPropList:
            prop.comp_name = newName
        for collInit in self.textCollInitList:
            collInit.renameCompName2(oldName, newName)
        for evt in self.textEventList:
            if evt.comp_name:
                evt.comp_name = newName

    def renameCtrlRefs(self, oldName, newName):
        """ Notification of a the rename of another control, used to fix up
            references
        """
        if self.textConstr:
            self.textConstr.renameCompName2(oldName, newName)
        for prop in self.textPropList:
            prop.renameCompName2(oldName, newName)

    def getPropNameFromSetter(self, setter):
        props = self.properties()
        for prop in props.keys():
            if props[prop][1] and props[prop][1].__name__ == setter:
                return prop
        if setter[:3] == 'Set': return setter[3:]
        else: return setter

    def eval(self, expr):
        import PaletteMapping
        try:
            return PaletteMapping.evalCtrl(expr, self.designer.model.specialAttrs)
        except Exception, err:
            print _('Illegal expression: %s')%expr
            raise

    def defaultAction(self):
        """ Invoke the default property editor for this component,
            This can be anything from a custom editor to an event.
        """
        pass

    def notification(self, compn, action):
        """ Called after components are added and before they are removed.
            Used for initialisation or finalisation hooks in other
            components.
        """
        pass

    def registerResourceModule(self, name):
        """ Resource Module name that should be added to the import list """
        if name not in self.resourceImports:
            self.resourceImports.append(name)

    def writeResourceImports(self):
        """ Return import line that will be added to module
        """
        if not self.resourceImports:
            return ''
        else:
            return '\n'.join(['import %s'%mod for mod in self.resourceImports])

    def writeImports(self):
        """ Return import line that will be added to module
        """
        return ''

#---Source writing methods------------------------------------------------------
    def addContinuedLine(self, line, output, indent):
        if Preferences.cgWrapLines:
            if len(line) > Preferences.cgLineWrapWidth:
                segs = methodparse.safesplitfields(line, ',', True, (), ())
                line = sourceconst.bodyIndent+segs[0].lstrip()
                for seg in segs[1:]:
                    newLine = line +', '+ seg
                    if len(newLine) >= Preferences.cgLineWrapWidth:
                        output.append(line+',')
                        line = indent+' '*Preferences.cgContinuedLineIndent+seg
                    else:
                        line = newLine

        output.append(line)

    def writeConstructor(self, output, collectionMethod, stripFrmId=''):
        """ Writes out constructor and parameters for control
        """
        # Add constructor
        if self.textConstr:
            self.addContinuedLine(
                sourceconst.bodyIndent+self.textConstr.asText(stripFrmId),
                output, sourceconst.bodyIndent)

    nullProps = ('None', 'wx.NullBitmap', 'wx.NullIcon')
    def writeProperties(self, output, ctrlName, definedCtrls, deps, depLinks, stripFrmId=''):
        """ Write out property setters but postpone dependent properties.
        """
        # Add properties
        for prop in self.textPropList:
            # Skip blanked out props
            if len(prop.params) == 1 and prop.params[0] in self.nullProps:
                continue
            # Postpone dependent props
            if self.designer.checkAndAddDepLink(ctrlName, prop,
                  self.dependentProps(), deps, depLinks, definedCtrls):
                continue
            self.addContinuedLine(sourceconst.bodyIndent+prop.asText(stripFrmId),
                  output, sourceconst.bodyIndent)

    def writeEvents(self, output, module=None, stripFrmId=''):
        """ Write out EVT_* calls for all events. Optionally For every event
            definition not defined in source add an empty method declaration to
            the bottom of the class """
        for evt in self.textEventList:
            if evt.trigger_meth != _('(delete)'):
                self.addContinuedLine(
                      sourceconst.bodyIndent + evt.asText(stripFrmId),
                      output, sourceconst.bodyIndent)
                model = self.designer.model
                # Either rename the event or add if a new one
                # The first streamed occurrence will do the rename or add
                if (evt.prev_trigger_meth and module
                        and (evt.prev_trigger_meth in
                            module.classes[model.main].methods)):
                    module.renameMethod(model.main, evt.prev_trigger_meth,
                          evt.trigger_meth)
                elif module and not evt.trigger_meth in module.classes[model.main].methods:
                    module.addMethod(model.main, evt.trigger_meth,
                       'self, event', [sourceconst.bodyIndent + 'event.Skip()'])

    def writeCollections(self, output, collDeps, stripFrmId=''):
        """ Write out collection initialiser methods. """
        for collInit in self.textCollInitList:
            if collInit.getPropName() in self.dependentProps():
                self.addContinuedLine(
                    sourceconst.bodyIndent + collInit.asText(stripFrmId),
                    collDeps, sourceconst.bodyIndent)
            else:
                self.addContinuedLine(
                      sourceconst.bodyIndent + collInit.asText(stripFrmId),
                      output, sourceconst.bodyIndent)

    def writeDependencies(self, output, ctrlName, depLinks, definedCtrls,
          stripFrmId=''):
        """ Write out dependent properties if all the ctrls they reference
            have been created.
        """
        if ctrlName in depLinks:
            for prop, otherRefs in depLinks[ctrlName]:
                for oRf in otherRefs:
                    if oRf not in definedCtrls:
                        # special attrs are not 'reference dependencies'
                        if not hasattr(
                              self.designer.model.specialAttrs['self'], oRf):
                            break
                else:
                    self.addContinuedLine(
                          sourceconst.bodyIndent + prop.asText(stripFrmId),
                          output, sourceconst.bodyIndent)


class NYIDTC(DesignTimeCompanion):
    """ Blank holder for companions which have not been implemented."""
    host = 'Not Implemented'
    def __init__(self, name, designer, parent, ctrlClass):
        raise Exception, _('Not Implemented')


class ControlDTC(DesignTimeCompanion):
    """ Visible controls created on a Frame and defined from
        _init_ctrls.
    """
    handledConstrParams = ('id', 'parent')
    windowIdName = 'id'
    windowParentName = 'parent'
    host = 'Designer'
    def __init__(self, name, designer, parent, ctrlClass):
        DesignTimeCompanion.__init__(self, name, designer)
        self.parent = parent
        self.ctrlClass = ctrlClass
        self.generateWindowId()
        self.container = False


    def designTimeControl(self, position, size, args = None):
        """ Create and initialise a design-time control """
        if args:
            self.control = self.ctrlClass(**args)
        else:
            self.control = self.ctrlClass(**self.designTimeDefaults(position, size))

        self.initDesignTimeControl()
        return self.control

    def designTimeDefaults(self, position = wx.DefaultPosition,
                                 size = wx.DefaultSize):
        """ Return a dictionary of parameters for the constructor of a wxPython
            control. e.g. {'name': 'Frame1', etc) """
        if not position: position = wx.DefaultPosition
        if not size: size = wx.DefaultSize

        dts = self.designTimeSource('wx.Point(%s, %s)'%(position.x, position.y),
          'wx.Size(%s, %s)'%(size.x, size.y))

        for param in dts.keys():
            dts[param] = self.eval(dts[param])

        dts[self.windowParentName] = self.parent

        if not self.suppressWindowId:
            dts[self.windowIdName] = self.dId

        return dts

    def designTimeSource(self, position = 'wx.DefaultPosition', size = 'wx.DefaultSize'):
        """ Return a dictionary of parameters for the constructor of a wxPython
            control's source. 'parent' and 'id' handled automatically
        """
        return {}

    def generateWindowId(self):
        if self.designer:
            self.id = Utils.windowIdentifier(self.designer.GetName(), self.name)
        else: self.id = `wx.NewId()`

    def SetName(self, oldValue, newValue):
        DesignTimeCompanion.SetName(self, oldValue, newValue)
        self.updateWindowIds()
        if self.compositeCtrl:
            for ctrl in self.control.GetChildren():
                ctrl.SetName(newValue)

    def extraConstrProps(self):
        return {'Class': 'class'}

##    def GetClass(self, dummy):
##        return self.control.__class__.__name__
##
##    def SetClass(self, value):
##        raise 'Cannot change'

    def updateWindowIds(self):
        self.generateWindowId()
        if not self.suppressWindowId:
            EventCollections.renameCmdIdInDict(self.textConstr.params, self.windowIdName, self.id)
            self.renameEventListIds(self.id)

    def initDesignTimeEvents(self, ctrl):
        # XXX Uncommenting this causes a crash after the first
        # XXX OnMouseOver event
        # By pushing the eventhandler, even ctrls
        # that hook to the Mouse events will still cause
        # mouse event to fire (theoretically)
        # ctrl.PushEventHandler(self.designer.ctrlEvtHandler)
        self.designer.ctrlEvtHandler.connectEvts(ctrl, self.compositeCtrl)

    def initDesignTimeControl(self):
        #try to set the name
        try:
            self.control.SetName(self.name)
            if self.compositeCtrl:
                for ctrl in self.control.GetChildren():
                    ctrl.SetName(self.name)
                    ctrl._composite_child = 1
            self.control.SetToolTipString(self.name)
            # Disabled controls do not pass thru mouse clicks to their parents on GTK :(
            if wx.Platform != '__WXGTK__' and self.ctrlDisabled:
                self.control.Enable(False)
        except:
            pass

        self.initDesignTimeEvents(self.control)

        self.popx = self.popy = 0

        self.control.Bind(wx.EVT_RIGHT_DOWN, self.designer.OnRightDown)
        # for wxMSW
#        EVT_COMMAND_RIGHT_CLICK(self.control, -1, self.designer.OnRightClick)
        # for wxGTK
        self.control.Bind(wx.EVT_RIGHT_UP, self.designer.OnRightClick)

    def beforeResize(self):
        pass
        #print 'beforeResize'

    def afterResize(self):
        pass
        #print 'afterResize'

    def updatePosAndSize(self):
        if self.textConstr and 'pos' in self.textConstr.params \
              and 'size' in self.textConstr.params:
            pos = self.control.GetPosition()
            size = self.control.GetSize()
            self.textConstr.params['pos'] = 'wx.Point(%d, %d)' % (pos.x, pos.y)
            self.textConstr.params['size'] = 'wx.Size(%d, %d)' % (size.x, size.y)

    def getDefCtrlSize(self):
        return 'wx.Size(%d, %d)'%(Preferences.dsDefaultControlSize.x,
                                 Preferences.dsDefaultControlSize.y)


    def getPositionDependentProps(self):
        return [('constr', 'Position'), ('prop', 'Position')]

    def getSizeDependentProps(self):
        return [('constr', 'Size'), ('prop', 'Size'), ('prop', 'ClientSize')]

class MultipleSelectionDTC(DesignTimeCompanion):
    """ Semi mythical class at the moment that will represent a group of
        selected objects. It's properties should represent the common subset
        of properties of the selection.

        Currently only used so the inspector has something to hold on to during
        multiple selection
    """

# sub properties (Font etc)
class HelperDTC(DesignTimeCompanion):
    """ Helpers are subobjects or enumerations of properties. """
    def __init__(self, name, designer, ownerCompanion, obj, ownerPropWrap):
        DesignTimeCompanion.__init__(self, name, designer)
        self.control = obj
#        self.obj = obj
        self.ownerCompn = ownerCompanion
        self.owner = ownerCompanion.control
        self.ownerPW = ownerPropWrap

        self.updateObjFromOwner()

    def updateObjFromOwner(self):
        """ The object to which a sub object is connected may change
            this method reconnects the property to the current object.
        """
        self.obj = self.ownerPW.getValue(self)
        self.ctrl = self.obj
        self.control = self.obj

    def updateOwnerFromObj(self):
        """ Changes to subobjects do not reflect in their owners
            automatically they have to be reassigned to their
            property
        """
        self.ownerPW.setValue(self.obj)

    def persistProp(self, name, setterName, value):
        """ When a subobject's property is told to persist, it
            should persist it's owner

           This is currently managed by the property editor
        """
        pass

# non-visual classes (Imagelists, etc)
class UtilityDTC(DesignTimeCompanion):
    """ Utility companions are 'invisible' components that
        are not owned by the Frame.

        Utilities are created before the frame and controls
        and defined in the _init_utils method.
    """

    host = 'Data'
    def __init__(self, name, designer, objClass):
        DesignTimeCompanion.__init__(self, name, designer)
        self.objClass = objClass
        self.editors['Name'] = NameConstrPropEdit

    def properties(self):
        props = DesignTimeCompanion.properties(self)
        props['Name'] = ('NoneRoute', None, None)
        return props

    def designTimeObject(self, args = None):
        if args:
            self.control = self.objClass(**args)
        else:
            self.control = self.objClass(**self.designTimeDefaults())

        return self.control

    def designTimeDefaults(self):
        """ Return a dictionary of parameters for the constructor of a wxPython
            control. e.g. {'name': 'Frame1', etc) """

        dts = self.designTimeSource()

        for param in dts.keys():
            dts[param] = self.eval(dts[param])
        return dts

    def extraConstrProps(self):
        return {'Class': 'class'}

    def updateWindowIds(self):
        pass

    def updatePosAndSize(self):
        pass

# XXX Parents, from constructor or current selected container in designer
class WindowDTC(WindowConstr, ControlDTC):
    """ Defines the wxWindow interface overloading/defining specialised
        property editors. """
    def __init__(self, name, designer, parent, ctrlClass):
        ControlDTC.__init__(self, name, designer, parent, ctrlClass)
        self.editors.update({'AutoLayout': BoolPropEdit,
                        'Shown': BoolPropEdit,
                        'Enabled': BoolPropEdit,
                        #'EvtHandlerEnabled': BoolPropEdit,
                        'Style': StyleConstrPropEdit,
                        #'Constraints': CollectionPropEdit,
                        'Name': NamePropEdit,
                        'Anchors': AnchorPropEdit,
                        'Sizer': SizerClassLinkPropEdit,
                        'SizeHints': TuplePropEdit,
                        'Cursor': CursorClassLinkPropEdit,
                        'Centered': EnumPropEdit,
                        'ThemeEnabled': BoolPropEdit,
                        'WindowVariant': EnumPropEdit,
                        'BackgroundStyle': EnumPropEdit,
                        })
        self.options['Centered'] = [None, wx.HORIZONTAL, wx.VERTICAL, wx.BOTH]
        self.names['Centered'] = {'None': None, 'wx.HORIZONTAL': wx.HORIZONTAL,
                                  'wx.VERTICAL': wx.VERTICAL, 'wx.BOTH': wx.BOTH}
        self.options['WindowVariant'] = [wx.WINDOW_VARIANT_NORMAL, 
              wx.WINDOW_VARIANT_SMALL, wx.WINDOW_VARIANT_MINI, 
              wx.WINDOW_VARIANT_LARGE]
        self.names['WindowVariant'] = {
            'wx.WINDOW_VARIANT_NORMAL': wx.WINDOW_VARIANT_NORMAL, 
            'wx.WINDOW_VARIANT_SMALL':  wx.WINDOW_VARIANT_SMALL, 
            'wx.WINDOW_VARIANT_MINI': wx.WINDOW_VARIANT_MINI, 
            'wx.WINDOW_VARIANT_LARGE': wx.WINDOW_VARIANT_LARGE}
        self.options['BackgroundStyle'] = [wx.BG_STYLE_SYSTEM, 
              wx.BG_STYLE_COLOUR, wx.BG_STYLE_CUSTOM]
        self.names['BackgroundStyle'] = {
            'wx.BG_STYLE_SYSTEM': wx.BG_STYLE_SYSTEM, 
            'wx.BG_STYLE_COLOUR': wx.BG_STYLE_COLOUR, 
            'wx.BG_STYLE_CUSTOM': wx.BG_STYLE_CUSTOM}
        self.triggers.update({'Size'     : self.SizeUpdate,
                              'Position' : self.PositionUpdate})
        self.customPropEvaluators.update({'Constraints': self.EvalConstraints,
                                          'SizeHints': self.EvalSizeHints,})

        self.windowStyles = ['wx.CAPTION', 'wx.MINIMIZE_BOX', 'wx.MAXIMIZE_BOX',
            'wx.THICK_FRAME', 'wx.SIMPLE_BORDER', 'wx.DOUBLE_BORDER',
            'wx.SUNKEN_BORDER', 'wx.RAISED_BORDER', 'wx.STATIC_BORDER', 
            'wx.TRANSPARENT_WINDOW', 'wx.wxNO_3D', 'wx.TAB_TRAVERSAL', 
            'wx.WANTS_CHARS', 'wx.NO_FULL_REPAINT_ON_RESIZE', 'wx.VSCROLL', 
            'wx.HSCROLL', 'wx.CLIP_CHILDREN', 'wx.NO_BORDER', 'wx.ALWAYS_SHOW_SB']
        
        self.mutualDepProps = ['Value', 'Title', 'Label']

        #import UtilCompanions
        #self.subCompanions['Constraints'] = UtilCompanions.IndividualLayoutConstraintOCDTC
        #self.subCompanions['SizeHints'] = UtilCompanions.SizeHintsDTC
        self.anchorSettings = []
        self._applyConstraints = False
        self.initPropsThruCompanion = ['SizeHints', 'Cursor', 'Center', 'Sizer']
        self._sizeHints = (-1, -1, -1, -1)
        self._cursor = wx.NullCursor
        self._centered = None

    def properties(self):
        return {'Shown': ('CompnRoute', self.GetShown, self.Show),
                'Enabled': ('CompnRoute', self.GetEnabled, self.Enable),
                'ToolTipString': ('CompnRoute', self.GetToolTipString, self.SetToolTipString),
                'Anchors': ('CompnRoute', self.GetAnchors, self.SetConstraints),
                'SizeHints': ('CompnRoute', self.GetSizeHints, self.SetSizeHints),
                'Cursor': ('CompnRoute', self.GetCursor, self.SetCursor),
                'Centered': ('CompnRoute', self.GetCentered, self.Center),
                'Sizer': ('CompnRoute', self.GetSizer, self.SetSizer),
                }

    def designTimeSource(self, position = 'wx.DefaultPosition', size = 'wx.DefaultSize'):
        return {'pos':  position,
                'size': self.getDefCtrlSize(),
                'name': `self.name`,
                'style': '0'}

    def dependentProps(self):
        return ['Cursor']

    def onlyPersistProps(self):
        return ['Show', 'Enable']

    def hideDesignTime(self):
        return ['NextHandler', 'PreviousHandler', 'EventHandler', 'EvtHandlerEnabled',
                'Id', 'Caret', 'WindowStyleFlag', 'ToolTip', 'Title', 'Rect',
                'DragTarget', 'DropTarget', 'Cursor', 'VirtualSize', 'Sizer',
                'ContainingSizer', 'Constraints', 'DefaultItem', 'Validator',
                'WindowStyle', 'AcceleratorTable', 'ClientRect', 'ExtraStyle',
                'LayoutDirection']

    def dontPersistProps(self):
        return ControlDTC.dontPersistProps(self) + ['ClientSize']
    def applyRunTime(self):
        return ['Shown', 'Enabled', 'EvtHandlerEnabled']
    def events(self):
        return ['MiscEvent', 'MouseEvent', 'FocusEvent', 'KeyEvent', 'HelpEvent']

    def notification(self, compn, action):
        if action == 'delete':
            if self._cursor and `self._cursor` == `compn.control`:
                self.propRevertToDefault('Cursor', 'SetCursor')
                self.SetCursor(wx.NullCursor)
        # XXX sizer

    def persistProp(self, name, setterName, value):
        if setterName == 'SetSizeHints':
            minW, minH, maxW, maxH = self.eval(value)
            newParams = [`minW`, `minH`, `maxW`, `maxH`]
            # edit if exists
            for prop in self.textPropList:
                if prop.prop_setter == setterName:
                    prop.params = newParams
                    return
            # add if not defined
            self.textPropList.append(methodparse.PropertyParse( None,
                self.getCompName(), setterName, newParams, 'SetSizeHints'))
        elif setterName == 'SetSizer':
            sizerList = self.designer.getSizerConnectList()
            if sizerList is not None:
                for prop in sizerList:
                    if prop.prop_setter == setterName and \
                          prop.comp_name == self.getCompName():
                        if value == 'None':
                            sizerList.remove(prop)
                        else:
                            prop.params = [value]
                        return

                if value != 'None':
                    sizerList.append(methodparse.PropertyParse(
                          None, self.getCompName(), setterName, [value], name))
        else:
            ControlDTC.persistProp(self, name, setterName, value)

    def propIsDefault(self, propName, setterName):
        if setterName == 'SetSizer':
            scl = self.designer.getSizerConnectList()
            if scl:
                for connProp in self.designer.getSizerConnectList():
                    if connProp.comp_name == self.getCompName():
                        return False
            return True
        else:
            return ControlDTC.propIsDefault(self, propName, setterName)

#---ToolTips--------------------------------------------------------------------
    def GetToolTipString(self, blah):
        return self.control.GetToolTip().GetTip()

    def SetToolTipString(self, value):
        self.control.SetToolTipString(value)

#---Anchors---------------------------------------------------------------------
    from wx.lib.anchors import LayoutAnchors

    def writeImports(self):
        imports = ControlDTC.writeImports(self)
        if self.anchorSettings:
            return '\n'.join( (imports,
                   'from wx.lib.anchors import LayoutAnchors') )
        else:
            return imports

    def GetAnchors(self, compn):
        if self.anchorSettings:
            return self.LayoutAnchors(*([self.control] + self.anchorSettings))
        else:
            return None

    # Named like the wxWindow method to override it when generating code
    def SetConstraints(self, value):
        curVal = self.control.GetConstraints()
        if curVal != value:
            self.control.SetConstraints(value)
        if self.designer.selection:
            self.designer.selection.updateAnchors()
        elif self.designer.multiSelection:
            for selection in self.designer.multiSelection:
                selection.updateAnchors()
        self.designer.inspector.propertyUpdate('Anchors')

    def EvalConstraints(self, exprs, objects):
        if exprs[0].startswith('LayoutAnchors'):
            ctrl, left, top, right, bottom = \
             methodparse.safesplitfields(exprs[0][len('LayoutAnchors')+1:-1], ',')
            ctrl, left, top, right, bottom = (objects[ctrl], self.eval(left),
                  self.eval(top), self.eval(right), self.eval(bottom))
            self.anchorSettings = [left, top, right, bottom]
            return (self.LayoutAnchors(ctrl, left, top, right, bottom), )
        return (None,)

    def updateAnchors(self, flagset, value):
        if not self.anchorSettings:
            self.defaultAnchors()

        for idx in range(4):
            if flagset[idx]:
                self.anchorSettings[idx] = value

    def removeAnchors(self):
        self.anchorSettings = []
        idx = 0
        while idx < len(self.textPropList):
            prop = self.textPropList[idx]
            if prop.prop_setter == 'SetConstraints' and \
                  prop.params[0].startswith('LayoutAnchors'):
                del self.textPropList[idx]
                break
            else:
                idx = idx + 1

    def defaultAnchors(self):
        self.anchorSettings = [True, True, False, False]

    def applyConstraints(self):
        left, top, right, bottom = self.anchorSettings
        self.control.SetConstraints(
            self.LayoutAnchors(self.control, left, top, right, bottom))

    def beforeResize(self):
        lc = self.control.GetConstraints()
        self._applyConstraints = lc != None and self.anchorSettings
        if self._applyConstraints:
            self.SetConstraints(None)

    def afterResize(self):
        if self._applyConstraints and self.anchorSettings:
            self.applyConstraints()
        elif self.designer.sizersView and hasattr(self.control, '_in_sizer'):
            szr = self.control._in_sizer
            if szr:
                for si in szr.GetChildren():
                    if si.IsWindow():
                        if si.GetWindow() == self.control:
                            p = self.control.GetPosition()
                            s = self.control.GetSize()
                            #si.SetDimension(p, s)
                            si.SetInitSize(s.width, s.height)
                            #szr.Layout()
                            #self.designer.sizersView.layoutSizers()
                            parent = self.control.GetParent()
                            if parent:
                                wx.PostEvent(parent, wx.SizeEvent(
                                    parent.GetSize(), parent.GetId()))
                                wx.CallAfter(parent.Refresh)#relayoutCtrl(self.designer)
                            #self.designer.model.editor.setStatus('Sizer item update %s, %s'%(p, s))
                            break


#---Designer updaters-----------------------------------------------------------
    def SizeUpdate(self, oldValue, newValue):
        if self.designer.selection:
            self.designer.selection.selectCtrl(self.control, self)
            self.designer.selection.moveCapture(self.control, self, wx.Point(0, 0))

    def PositionUpdate(self, oldValue, newValue):
        if self.designer.selection:
            self.designer.selection.selectCtrl(self.control, self)
            self.designer.selection.moveCapture(self.control, self, wx.Point(0, 0))

#---Size hints------------------------------------------------------------------
    def GetSizeHints(self, dummy):
        return self._sizeHints

    def SetSizeHints(self, value):
        self._sizeHints = value
        self.control.SetSizeHints(value[0], value[1], value[2], value[3])

    def EvalSizeHints(self, exprs, objects):
        res = []
        for expr in exprs:
            res.append(self.eval(expr))
        return tuple(res)

#---Cursors---------------------------------------------------------------------
    def GetCursor(self, x):
        return self._cursor
    def SetCursor(self, value):
        self._cursor = value
        self.control.SetCursor(value)

#---Sizers----------------------------------------------------------------------
    def GetSizer(self, x):
        return self.control.GetSizer()
    def SetSizer(self, value):
        if value is not None:
            self.control._has_sizer = value
            value._has_control = self.control
        else:
            if hasattr(self.control, '_has_sizer'):
                szr = self.control._has_sizer
                if szr:
                    if hasattr(szr, '_has_control'):
                        del szr._has_control
                del self.control._has_sizer

        self.control.SetSizer(value)

        if value is not None:
            value.Layout()
            self.designer.relayoutCtrl(self.control)

#-------------------------------------------------------------------------------

    def GetCentered(self, dummy):
        return self._centered
    def Center(self, value):
        self._centered = value
        if value:
            self.control.Center(value)

    def GetShown(self, x):
        for prop in self.textPropList:
            if prop.prop_setter == 'Show':
                return int(prop.params[0].lower() == 'true')
        return 1

    def Show(self, value):
        pass

    def GetEnabled(self, x):
        for prop in self.textPropList:
            if prop.prop_setter == 'Enable':
                return int(prop.params[0].lower() == 'true')
        return 1

    def Enable(self, value):
        pass


class ChoicedDTC(WindowDTC):
    def __init__(self, name, designer, parent, ctrlClass):
        WindowDTC.__init__(self, name, designer, parent, ctrlClass)
        self.editors['Choices'] = ChoicesConstrPropEdit

class ContainerDTC(WindowDTC):
    """ Parent for controls that contain/own other controls """
    def __init__(self, name, designer, parent, ctrlClass):
        WindowDTC.__init__(self, name, designer, parent, ctrlClass)
        self.container = True

class CollectionDTC(DesignTimeCompanion):
    """ Companions encapsulating list maintaining behaviour into a single
        property
        Maintains an index which points to the currently active item in the
        collection
    """

    propName = 'undefined'
    insertionMethod = 'undefined'
    deletionMethod = 'undefined'
    displayProp = 'undefined'
    indexProp = 'undefined'
    sourceObjName = 'parent'

    additionalMethods = {}

    def __init__(self, name, designer, parentCompanion, ctrl):
        DesignTimeCompanion.__init__(self, name, designer)
        from Views.CollectionEdit import CollectionEditor
        self.CollEditorFrame = CollectionEditor
        self.control = ctrl
        self.setCollectionMethod()
        self.index = 0
        self.parentCompanion = parentCompanion

    def setCollectionMethod(self):
        self.collectionMethod = '_init_coll_%s_%s' %(self.name, self.propName)

    def setIndex(self, index):
        self.index = index
        self.setConstr(self.textConstrLst[index])

    def setConstrs(self, constrLst, inits, fins):
        self.initialisers = inits
        self.finalisers = fins

        self.textConstrLst = constrLst

    def renameCtrl(self, oldName, newName):
        DesignTimeCompanion.renameCtrl(self, oldName, newName)
        self.setCollectionMethod()

    def renameCtrlRefs(self, oldName, newName):
        # textConstr and textPropList not used in collections
        # DesignTimeCompanion.renameCtrlRefs(self, oldName, newName)
        for constr in self.textConstrLst:
            constr.renameCompName2(oldName, newName)

    def getCount(self):
        return len(self.textConstrLst)

    def getDisplayProp(self):
        tcl = self.textConstrLst[self.index]
        if tcl.method != self.insertionMethod:
            if tcl.method in self.additionalMethods:
                displayProp = self.additionalMethods[tcl.method][1]
            else:
                return '-'
        else:
            displayProp = self.displayProp

        if displayProp in tcl.params:
            propSrc = tcl.params[displayProp]
            if propSrc and (propSrc[0] in ("'", '"') or propSrc[:2] in ('u"', "u'")):
                return self.eval(propSrc)
            else:
                return propSrc
        else:
            return '-'

    def initialiser(self):
        """ When overriding, append this after derived initialiser """
        return ['']

    def finaliser(self):
        """ When overriding, append this before derived finaliser """
        return []

    def appendItem(self, method=None, srcParams={}):
        self.index = self.getCount()
        if method is None:
            method = self.insertionMethod
        src = self.designTimeSource(self.index, method)        
        src.update(srcParams)
        collItemInit = methodparse.CollectionItemInitParse(None,
          self.sourceObjName, method, src)

        self.textConstrLst.append(collItemInit)
        self.setConstr(collItemInit)

        self.applyDesignTimeDefaults(collItemInit.params, method)

        return collItemInit

    def deleteItem(self, idx):
        # remove from ctrl
        if self.deletionMethod != '(None)':
            getattr(self.control, self.deletionMethod)(idx)

        # renumber items following deleted one
        if self.indexProp != '(None)':
            for constr in self.textConstrLst[idx:]:
                constr.params[self.indexProp] = `int(constr.params[self.indexProp]) -1`

    def moveItem(self, idx, dir):
        tc = self.textConstrLst[idx]
        newIdx = min(max(idx + dir, 0), len(self.textConstrLst)-1)

        if newIdx != idx:
            del self.textConstrLst[idx]
            self.textConstrLst.insert(newIdx, tc)

            if self.indexProp != '(None)':
                # swap index property values
                (self.textConstrLst[idx].params[self.indexProp],
                 self.textConstrLst[newIdx].params[self.indexProp]) = \
                (self.textConstrLst[newIdx].params[self.indexProp],
                 self.textConstrLst[idx].params[self.indexProp])

        return newIdx

    def applyDesignTimeDefaults(self, params, method=None):
        if method is None:
            method = self.insertionMethod
        args = []
        kwargs = {}
        paramItems = self.designTimeDefaults(params, method).items()
        paramItems.sort()
        for k, v in paramItems:
            if type(k) is type(0):
                args.append(v)
            else:
                kwargs[k] = v
        getattr(self.control, method)(*args, **kwargs)

    def SetName(self, oldValue, newValue):
        self.name = newValue
        self.setCollectionMethod()

    def GetClass(self, dummy=None):
        return self.propName

    def SetClass(self, value):
        pass

    def updateWindowIds(self):
        pass

##    def addIds(self, lst):
##        """ Iterate over items and extend lst for collections with ids """
##        pass

    def designTimeDefaults(self, vals, method=None):
        """ Return a dictionary of parameters for adding an item to the collection """
        dtd = {}
        for param in vals.keys():
            dtd[param] = self.eval(vals[param])
        return dtd

    def initCollection(self):
        pass

    def writeCollectionInitialiser(self, output, stripFrmId=''):
        output.extend(self.initialiser())

    def writeCollectionItems(self, output, stripFrmId=''):
        for creator in self.textConstrLst:
            self.addContinuedLine(
                  sourceconst.bodyIndent + creator.asText(stripFrmId),
                  output, sourceconst.bodyIndent)

    def writeCollectionFinaliser(self, output, stripFrmId=''):
        output.extend(self.finaliser())

    def getPropList(self):
        """ Returns a dictionary of methods suported by the control

            The dict has 'properties' and 'methods' keys which contain the
            getters/setters and undefined methods.
         """
        # XXX should use sub objects if available, but properties on collection
        # XXX items aren't supported yet
        return RTTI.getPropList(None, self)

    def defaultAction(self):
        """ Called when a component is double clicked in a designer
        """
        pass
##        print 'CDTC', self.textConstrLst[self.index]

    def notification(self, compn, action):
        """ Called when other components are deleted.

            Use it to clear references to components which are being deleted.
        """
        # XXX Should use this mechanism to trap renames as well
        pass
##        print 'CollectionDTC.notification', compn, action

class CollectionIddDTC(CollectionDTC):
    """ Collections which have window ids and events """
    windowIdName = 'id'
    idProp = '(undefined)'
    idPropNameFrom = '(undefined)'

    def __init__(self, name, designer, parentCompanion, ctrl):
        CollectionDTC.__init__(self, name, designer, parentCompanion, ctrl)
        self.editors = {'ItemId': ItemIdConstrPropEdit}

    def properties(self):
        props = CollectionDTC.properties(self)
        props['ItemId'] = ('CompnRoute', self.GetItemId, self.SetItemId)
        return props

    def evtName(self):
#        return '%s%s%d' % (self.name, self.propName, self.index)
        base = self.newWinId('')
        itemId = self.GetItemId(None)[len(base):].capitalize()
        return '%s%s' % (self.name, itemId)

##    def setIndex(self, idx):
##        CollectionDCT.setIndex(idx)
##        self.setEvents(

    def getEvents(self):
        evts = []
        idxWId = self.getWinId()
        for evt in self.textEventList:
            if evt.windowid == idxWId:
                evts.append(evt)
        return evts

    def getWinId(self):
        tcl = self.textConstrLst[self.index]
        if self.idProp in tcl.params:
            return tcl.params[self.idProp]
        else:
            return -1

    def getDesignTimeWinId(self):
        return self.control.GetMenuItems()[self.index].GetId()

    def addIds(self, lst):
        for constr in self.textConstrLst:
            if self.idProp in constr.params:
                wId = constr.params[self.idProp]
                if wId in EventCollections.reservedWxIds:
                    name, wId = self.newUnusedItemNames(0)
                lst.append(wId)

    def appendItem(self, method=None):
        CollectionDTC.appendItem(self, method)

#1        self.generateWindowId(self.index)
        self.updateWindowIds()

    def deleteItemEvents(self, idx):
        constr = self.textConstrLst[idx]
        if self.idProp in constr.params:
            wIdStr = constr.params[self.idProp]
            for evt in self.textEventList[:]:
                if evt.windowid == wIdStr:
                    self.textEventList.remove(evt)

    def deleteItem(self, idx):
        self.deleteItemEvents(idx)
        CollectionDTC.deleteItem(self, idx)

        self.updateWindowIds()

    def newUnusedItemNames(self, wId):
        while 1:
            newItemName = '%s%d'%(self.propName, wId)
            winId = self.newWinId(newItemName)
            if self.isIdUsed(winId): wId = wId + 1
            else: break
        return newItemName, winId

    def isIdUsed(self, wId):
        for tc in self.textConstrLst:
            if self.idProp in tc.params and tc.params[self.idProp] == wId:
                return True
        return False


    def newWinId(self, itemName):
        return Utils.windowIdentifier(self.designer.controllerView.GetName(),
              self.name + itemName)

    def generateWindowId(self, idx):
        return

    def SetName(self, oldValue, newValue):
        CollectionDTC.SetName(self, oldValue, newValue)
        self.updateWindowIds()

    def evtGetter(self, name):
        wId = self.getWinId()
        for evt in self.textEventList:
            if evt.event_name == name and evt.windowid == wId:
                return evt.trigger_meth
        return None

    def evtSetter(self, name, value):
        wId = self.getWinId()
        for evt in self.textEventList:
            if evt.event_name == name and evt.windowid == wId:
                evt.trigger_meth = value
                return

    def persistEvt(self, name, value, wId = None):
        """ Add a source entry for an event or update the trigger method of
            am existing event. """
        if wId is None:
            wId = self.getWinId()

        for evt in self.textEventList:
            if evt.event_name == name and evt.windowid == wId:
                evt.trigger_meth = value
                return
        if self.control == self.designer or wId is not None:
            comp_name = ''
        else:
            comp_name = self.name
        self.textEventList.append(methodparse.EventParse(None, comp_name, name,
                                                         value, wId))

    def updateWindowIds(self):
        for idx in range(len(self.textConstrLst)):
            # XXX no op?
            self.generateWindowId(idx)

    def designTimeDefaults(self, vals, method=None):
        """ Return a dictionary of parameters for the constructor of a wxPython
            control. e.g. {'name': 'button1', etc)

            Derived classes should only call this base method if method
            requires an id parameter. This is usually the case."""
        values = copy.copy(vals)
        values[self.idProp] = `wx.NewId()`
        dts = CollectionDTC.designTimeDefaults(self, values)
        dts[self.idProp] = wx.NewId()
        return dts

    def GetItemId(self, x):
        return self.textConstrLst[self.index].params[self.idProp]

    def SetItemId(self, value):
        oldValue = self.textConstrLst[self.index].params[self.idProp]
        self.textConstrLst[self.index].params[self.idProp] = value
        for evt in self.textEventList:
            if evt.windowid == oldValue:
                evt.windowid = value