This file is indexed.

/usr/share/pyshared/sqlkit/layout/widgets.py is in python-sqlkit 0.9.5-1ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
# Copyright (C) 2005 Sandro Dentella. All rights reserved.
# e-mail: sandro@e-den.it
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software 
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

import re
import os

import gtk
import gobject

from sqlkit import _
from sqlkit import debug as  dbg
from sqlkit.layout.greedy_treeview import GreedyTreeView

class IdentifierAlreadyPresent: pass

info = {}
class LWidget(object):
    #__metaclass__ = LogTheMethods
    #logMatch = "position*"
    
    gtkClass = None
    transpose = {'w' : 'width_request',
                 'h' : 'height_request'
                 }
    class_properties = {'visible' : True}
    class_pack_properties  = {'y-options' : '0'}

    def __init__(self, el_def, pos, container, elements, layout):
        ## update info for this class
        # print "Lwidget: %s %s" % (el_def,  self.gtkClass)
#         if self.gtkClass == 'GtkAlignment':
#             dbg.show_caller(sfilter='elements')

        self.el_def = el_def
        assert layout is not None
        self.layout = layout
        self.lw = {}
        self.container_flag = container
        self.properties = {}
        self.pack_properties = {}
        # By default, widgets do not expand vertically. Those who do can be
        # recognized (and set) by this attribute being True
        self.v_expand = False
        # used to specify order in properties
        self.ordered_properties = []
        if self.gtkClass:
            self.properties.update(LWidget.class_properties)
            self.pack_properties.update(LWidget.class_pack_properties)
            
        self.properties.update(self.class_properties)

        self._parse_cell_def(el_def)
        self.lw.update( {   ## lw : layout widget
           'container' : container,
           'el_def'    : el_def,
           'gtkClass'  : self.gtkClass,
           'children_list' : [],
        } )
        self.position(pos)
        self.pos = pos
        self.elements = elements
        self._labels_and_tips_from_info()
        #dbg.write('class_specific', type(self))
        self._class_specific()
#         if re.match('GtkEventBox', self.gtkClass):
#             dbg.write('ok')            
        self._set_dimentions()
        #dbg.debug("elements[%s] = %s (%s)" % (self.lw['el_key'],
        #                                     self,elements.keys()) )
        elements[self.lw['el_key']] = self

    def position(self, (row, col)):
        ## ask the class of the container which are the pack_properties
        ## apart from Table. Table has position changing, the others pack
        ## widgets as they arrive. I tried with classmethod for Table w/ no luck
        if self.container_flag in ('T'):                  
            self.pack_properties.update( {
                'left_attach'       : col,
                'top_attach'        : row,
                'right_attach'      : col+1,
                'bottom_attach'     : row+1,
                })
        else:
            self.pack_properties.update(
                _W[self.container_flag].class_pack_properties)

    def _debug_pos(self):
        p = self.pack_properties
        if self.container_flag == 'T':
            return "pos: %s : %s" % ((p['top_attach'],p['left_attach']),
                                     (p['bottom_attach'],p['right_attach']))
        else:
            return ''

    def _parse_cell_def(self, el_def):
        pattern = re.compile(r"""
          (?:(?P<flag>\w+)=)?      # ignore if passed the full string
          (?P<val>[^/:<>]*)        # value
          (?:/(?P<var>[^:]+))?     # variable
          (?:(?::(?P<w>[\d]*))?      # width
          \.?
          (?P<h>-?[\d]+)?)?            # height
          (?P<align>[-<>])?         # align, a < or a > at the end of the string
        """, re.VERBOSE)
        match = pattern.search(el_def)
        self.class_flag = match.group("flag")
        self.val = match.group("val")
        self.var = match.group("var")
        self.w = match.group("w")
        self.h = match.group("h")
        self.align = match.group("align")
        #dbg.write("el: %s w=%s h=%s align=%s-" % (el_def,self.w,self.h, self.align))
        #print  "_parse_cell_def: el: %s var=%s val=%s h=%s align=%s" % (
        #    el_def,self.var,self.val, self.h, self.align)
        #print match.groups()
        if not self.var:
            ## radiobutton use self.val as 'group'
            if not re.search("GtkRadioButton",self.gtkClass):
                self.var = self.val

        ## let's set a reasonable key. Strip any width info
        # not sure If I should put all Containers
        cont_pat = "Gtk(Frame|Table|HBox|VBox|Notebook)"
        if not re.search(cont_pat,self.gtkClass):
            self.lw['el_key'] = re.sub('(:.*)|[-<>]$','',el_def)
        else:
            self.lw['el_key'] = re.sub('[-<>|=]$','',el_def)

        ### gtk-stock?
#         if re.match('gtk-', self.val) and not self.gtkClass == 'GtkImage':
        if gtk.icon_factory_lookup_default(self.val) and not self.gtkClass == 'GtkImage':
            if re.match('GtkToolButton', self.gtkClass):
                self.properties['stock_id'] = re.sub('\..+','',self.val)
                self.properties['label'] = None
            elif re.match('GtkButton', self.gtkClass):
                self.properties['label'] = _(re.sub('\..+','',self.val))
                #dbg.write(self.properties['label'])
                self.properties['use-stock'] = True
            else:
                self.properties['use-stock'] = True


    def _labels_and_tips_from_info(self):
        """
        set possible label and tip 
        """

        if not self.class_flag:
            return

        label, tip = self.get_label_and_tip()
        
        if label:
            self.val = label

        if tip:
            self.properties['tooltip_text'] = tip

    def get_label_and_tip(self):
        """
        keys can be mapped to labels in 3 ways:

          1. via a label_map local to the layout (shared with all nested layout)
          2. via a global info dictionary
          3. via gettext

        1. is attempted first, failing that 2. is attempted.
        The resulting string is anyhow passed via gettext.
        The aim is to allow to use per-table (meaning database table) translation from a
        name to the relative label. Names can normally be similar between tables.
        """

        name = self.val
        key = "%s=%s" % (self.class_flag, name)

        details = self.layout.label_map.get(key, None) or info.get(key, None)
        if not details:
            details = self.layout.label_map.get(name, None) or info.get(name, None)

        if details:
            return details
        else:
            return None, None


    def _set_dimentions(self):
        """Give a meaning to dimentions for this object"""
        ## width
        if self.w and  'w' in self.transpose:
            name = self.transpose['w']
            self.properties[name] = self.w
            #self.pack_properties['x_options'] = 'shrink'
        ##  'height'
        if self.h and  'h' in self.transpose:
            name = self.transpose['h']
            self.properties[name] = self.h
            
    def rowspan(self):
        if self.lw['container'] in ('T'):
            self.pack_properties['bottom_attach'] += 1
#         else:
#             dbg.warning("Span available only for Table qui: %s" % \
#                       self.lw['container'])

    def colspan(self):
        if self.lw['container'] in ('T'):
            self.pack_properties['right_attach'] += 1
#         else:
#             dbg.write("Span available only for Table qui: %s" % self.lw['container'])


    def _class_specific(self):
        pass
    
    ### Real pack
    def xml(self, ContGtkClass=None):
        return "%s\n%s" % (self._widget_xml(),
                           self._packing_xml(self.pack_properties,ContGtkClass))

    def create(self):
        """ Create the wdget """
        #dbg.write(self.gtkClass)
        #print dbg.dshow(self.properties)
        self.gtkWdg = gobject.new(gobject.type_from_name(self.gtkClass))
        # **self.properties
        for p, val in self.properties.iteritems():
            try:
                self.gtkWdg.set_property(p, val)
            except:
                pass
                #dbg.write("Fail prop %s for %s" % (p, self.gtkClass))

        return self.gtkWdg
                          
    def _packing_xml(self, pack_properties, ContGtkClass):
        # no packing if adding with 'addto'. In that case ContGtkClass
        # is not defined
        if not ContGtkClass:
            return ""

        allowed_pack_properties = ()
        for i in gtk.container_class_list_child_properties(ContGtkClass):
            allowed_pack_properties += (i.name,)

        xml = ["<packing>"]
        for key, value in pack_properties.iteritems():
            ## tabs label are child of notebook but of a type that
            ## is not returned by container_class... function above
            if (key == 'type' and ContGtkClass == 'GtkNotebook') \
                   or key.replace('_','-') in allowed_pack_properties:
                 xml.append('  <property name="%s">%s</property>' % (key, value))
        xml.append("</packing>")
       
        return "\n".join(xml)
    
    def _widget_xml(self):
        # If a children is v_expandable, the parent becomes too.
        if 'children_list' in self.lw and self.lw['children_list']:
            if True in [child.v_expand for child in self.lw['children_list']]:
                self.make_v_expandable()
    
        xml = ['<object class="%(gtkClass)s" id="%(el_key)s">' % self.lw]
        inner_xml = []
        # write properties with order before
        extra_text = ""
        for key in self.ordered_properties:
            value = self.properties[key]
            if key in self.transpose:
                pass
                #print key, self.transpose[key]
            if key in ('text', 'label'):
                extra_text = ' translatable="yes"'
                if value == '':
                    value = None
            else:
                extra_text = ""
                
            if value:
                inner_xml.append('<property name="%s"%s>%s</property>' %\
                                (key, extra_text, value))
        
        # now write everything else
        extra_text = ""
        for item, value in self.properties.iteritems():
            if item not in self.ordered_properties:
                if item in self.transpose:
                    item = self.transpose[item]
                if item in ('text', 'label'):
                    extra_text = ' translatable="yes"'
                else:
                    extra_text = ""
                    
                if value is not None:
                    inner_xml.append('<property name="%s"%s>%s</property>' %\
                                     (item, extra_text, value))

        children = self._children_xml()
        if children:
            inner_xml.append(children)
        xml.append(self._offset_txt("\n".join(inner_xml), 2))
        xml.append("</object>")
        return "\n".join(xml)

    def _children_xml(self):
        # this is significant only for LContainer
        return None

    def _offset_txt(self, txt, amount):
        ## offset text to get a prettier xml
        p = re.compile("^", re.MULTILINE)
        return p.sub(' ' * amount, txt)

    def make_v_expandable(self):
        """Teach a widget it must expand (vertically); it can be because it has
        a viewing benefit (e.g: a Treeview) or because a child of it has.
        """
        if self.v_expand:
            return
        self.v_expand = True
        # FIXME: (how?!) this expands horizontally if parent is a HBox
        self.pack_properties.update({'expand' : True})
        self.pack_properties.update({'y-options' : 'expand | fill'})


    def __str__(self):
        return "<%s: %s >" % (self.__class__.__name__, self.el_def)
    def __repr__(self):
        return "<%s: %s >" % (self.__class__.__name__, self.el_def)
class LContainer(LWidget):
    # the difference with LWidget is it has children
    # and the el_def is not written in the layout but created on the fly
    # (Layout._create_container_label). We use it as label
    class_pack_properties  = {'y-options' : 'fill|expand'}

    def __init__(self, el_def, pos, container, children_list=None, elements=None, layout=None):
        LWidget.__init__(self, el_def, pos, container, elements=elements, layout=layout)
        ## the children
        self.lw['children_list'] = children_list
        #self.properties['label'] = el_def
        
    def _children_xml(self):
        txt = ''
        try: 
            for obj in self.lw['children_list']:
                child = self._offset_txt(obj.xml(self.gtkClass), 4)
                txt += "\n<child>\n%s\n</child> <!-- closes %s in %s -->\n" % (
                    child, obj.lw['el_key'], obj.lw['container'])
        except:
            pass
        return txt
    def create(self, visible=None):
        """ Create the wdget """
        if visible is not None:
            self.properties['visible'] = visible
            
        #dbg.write(self.gtkClass)
        self.gtkWdg = gobject.new(gobject.type_from_name(self.gtkClass),
                          **self.properties)

        #dbg.write(gtkWdg)
        self.add_children(self.gtkWdg)
        return self.gtkWdg
                          
    def add_children(self, gtkWdg):

        if not self.lw['children_list']:
            return
        
        for obj in self.lw['children_list']:
            #dbg.write("gtkWdg", gtkWdg, obj.gtkClass)
            child = obj.create()
            pack_props = self.clean_pack_properties(obj.pack_properties, gtkWdg)
            
            if self.gtkClass not in ('GtkTable'):
                #dbg.write('%s.add(%s, %s)' %(gtkWdg, child, pack_props ))
                gtkWdg.add(child)
                for p, val in pack_props.iteritems():
                    #dbg.write("%s.set_property(%s, %s)" % (gtkWdg, p, val))
                    setattr(gtkWdg, p, val)
            if self.gtkClass == 'GtkTable':
                P = pack_props
                #dbg.write("pack_props", P, obj.pack_properties)
                gtkWdg.attach(child,
                           P['left_attach'],
                           P['right_attach'],
                           P['top_attach'],
                           P['bottom_attach'],
                           )

        return 
    def clean_pack_properties(self, pack_props, gtkWdg):
        return pack_props
#         dbg.write(gtkWdg, gtkWdg.name)
#         dbg.write(gtk.container_class_list_child_properties(gtkWdg))
#         props = [p.name for p in gtk.container_class_list_child_properties(gtkWdg)]
#         dbg.write(props)
#         ko = []
#         for p in pack_props:
#             if p not in props:
#                 ko += [p]
#         for p in ko:
#             del pack_props[p]
#         dbg.write(pack_props)
#         return pack_props

class AlignmentEntry(LContainer):
    gtkClass = 'GtkAlignment'

    def __init__(self,  el_def, pos, container, children_list=None, elements={}, layout=None):
        el_def = el_def.replace('ae=','A=')
        super(LContainer, self).__init__(el_def, pos, container, elements=elements, layout=layout)
        ## prepare the names...

        entry_str   = "e=%s" % (el_def.replace('A=',''))
        wdg = Entry(entry_str,(0,0),    'A', elements=elements, layout=layout)
        self.lw['children_list'] = [ wdg ]
        if self.align == '-':
            self.properties['xscale'] = 1
        else:
            self.properties['xscale'] = 0

        if self.align == '>':
            self.properties['xalign'] = 1
        else:
            self.properties['xalign'] = 0

    def _class_specific(self):
        self.pack_properties.update({
            'x_options' : 'expand|fill',
            })

    def _get_w(self):
        return -1
    def _set_w(self, value):
        ## when setting the with of an ae object we just want to set the width of the entry
        return
    w = property(_get_w, _set_w )

class Entry(LWidget):
    gtkClass = 'GtkEntry'
    transpose = {
        'w' : 'width_chars',
        }
#     def _class_specific(self):

#         if self.w:
#             self.properties['max-length'] = self.w

            
class DateEdit(LWidget):
    gtkClass = 'DateEdit'
    ## this widget is provided by dateedit module
            
class ImageWidget(LWidget):
    gtkClass = 'ImageWidget'
    ## this widget is provided by image module
    class_properties = {
        'image' : None,
        }
            
class DateTimeEdit(LWidget):
    gtkClass = 'DateTimeEdit'
    ## this widget is provided by dateedit module
    class_properties = {
        'time' : None,
        'lower-hour' : 10,
        'upper-hour' : 23,
        'show-time' : None,
        'use-24h-format' : True
        }
            
class Calendar(LWidget):
    # TODO: add a constructor to pass dates in a friendly way
    gtkClass = 'GtkCalendar'
    def _class_specific(self):
        self.pack_properties.update({'y_options': ""})
    #class_properties = {
        #'visible'     : True,
        #'day'     : 7,
        #'month'     : 3,
        #'year'     : 1963,
        #}
    

class TreeView(LWidget):
    gtkClass = 'GreedyTreeView'
    transpose = {
        'w' : 'width_request',
        'h' : 'height_request',
        }
    class_properties = {
        'width_request'  : '-1',
        'height_request'  : '-1',
        }

    def _class_specific(self):
        self.make_v_expandable()
        
class TextView(LWidget):
    gtkClass = 'GtkTextView'
    def _class_specific(self):
        self.make_v_expandable()


class Statusbar(LWidget):
    gtkClass = 'GtkStatusbar'
    
    def _class_specific(self):
        if self.container_flag == "T":
            self.pack_properties.update({'y_options': ''})
        elif self.container_flag == "V":
            self.pack_properties.update({'pack_type': 'GTK_PACK_END'})


class Label(LWidget):
    gtkClass = 'GtkLabel'
    transpose = {
        'w' : 'width_chars',
        }
    def _class_specific(self):
        self.properties.update({
            'xalign' : '1',
            'label'  : _(self.val).replace('_',' ') + ':',
            'xpad'   : 10,
            'use-markup' : True,
            })
        self.pack_properties.update({
            'y_options' : 'fill',
            'x_options' : 'fill',
            })
        if self.align:
            #dbg.debug("SELF ALIGN for ", self.lw['el_key'])
            if self.align in '<':
                #dbg.debug("""properties['xalign'] = 0""")
                self.properties['xalign'] = 0
            else:
                self.properties['xalign'] = 1

class LabelFill(LWidget):
    gtkClass = 'GtkLabel'
    
    def _class_specific(self):
        self.properties.update({
            'xalign' : '0',
            'label'  : _(self.val),
            'xpad'   : 10,
            })
        self.pack_properties.update({
            'y_options' : 'expand|shrink|fill',
            })

class NotebookLabel(Label):
    """This is for the tab of a notebook"""
    gtkClass = 'GtkLabel'
    
    # def _class_specific(self):
    #     ## must overwrite any other info
    #     self.pack_properties = {
    #         'type' : 'tab',
    #         }

class FrameLabel(Label):
    """This is for the tab of a notebook"""
    gtkClass = 'GtkLabel'
    class_properties = {
        'xpad' : 5,
        }    
    def _class_specific(self):
        ## must overwrite any other info
        self.pack_properties = {
            'type' : 'label_item',
            }

class Button(LWidget):
    gtkClass = 'GtkButton'
#     transpose = {
#         'w' : 'width_chars',
#         }
    class_pack_properties = {
        'expand' : False,
        'fill'   : True,
        }

    def _class_specific(self):
        self.properties.update({
            'xalign' : '0',
            'label'  : _(re.sub('\..+','',self.val))
            })        
        
        self.pack_properties.update({'y_options': ""})


class ToolButton(LWidget):
    gtkClass = 'GtkToolButton'

    def _class_specific(self):
        #if re.match("gtk-",self.val):
        if gtk.icon_factory_lookup_default(self.val):
            del self.properties['label'] 
            #self.properties['stock_id'] = self.val

class RadioButton(Button):
    gtkClass = 'GtkRadioButton'
    def _class_specific(self):
        if self.var:
            ## this puts this radioButton in the same group as the radiobutton
            ## written after the '/'
            self.properties['group'] = 'r=' + self.var

class ToggleButton(Button):
    gtkClass = 'GtkToggleButton'
    
class CheckButton(Button):
    gtkClass = 'GtkCheckButton'
    def _class_specific(self):
        self.properties['label'] = ''

class LinkButton(LWidget):
    gtkClass = 'GtkLinkButton'
#     transpose = {
#         'w' : 'width_chars',
#         }
    class_pack_properties = {
        'expand' : False,
        'fill'   : True,
        }

    def _class_specific(self):
        self.properties.update({
            'xalign' : '0',
            'label'  : _(re.sub('\..+','',self.val))
            })        
        
        self.pack_properties.update({'y_options': ""})


class ProgressBar(LWidget):
    gtkClass = 'GtkProgressBar'

class ComboBox(Entry):
    gtkClass = 'GtkComboBox'
    transpose = {
        'w' : 'width_request',
        }
    def _class_specific(self):
        self.pack_properties.update({
            'x_options' : 'fill',
            })

class AlignmentComboBox(LContainer):
    gtkClass = 'GtkAlignment'

    def __init__(self,  el_def, pos, container, children_list=None, elements={}, layout=None):
        el_def = el_def.replace('aC=','A=')
        super(LContainer, self).__init__(el_def, pos, container, elements=elements, layout=layout)
        ## prepare the names...

        entry_str   = "C=%s" % (el_def.replace('A=',''))
        #print "ENTRY_STR", entry_str
        ## and create the children
        #print "EventLabel elements: ", elements.keys()
        wdg = ComboBox(entry_str,(0,0),    'A', elements=elements, layout=layout)
        self.lw['children_list'] = [ wdg ]
        if self.align == '-':
            self.properties['xscale'] = 1
        else:
            self.properties['xscale'] = 0

        if self.align == '>':
            self.properties['xalign'] = 1
        else:
            self.properties['xalign'] = 0

    def _class_specific(self):
        self.pack_properties.update({
            'x_options' : 'expand|fill',
            })

    def _get_w(self):
        return -1
    def _set_w(self, value):
        ## when setting the with of an ae object we just want to set the width of the entry
        return
    w = property(_get_w, _set_w )

class ComboBoxEntry(Entry):
    gtkClass = 'GtkComboBoxEntry'

class SpinButton(LContainer):
    gtkClass = 'GtkSpinButton'
    class_properties = {
        "can_focus"     : True,
        "climb_rate"    : 1,
        "digits"        : 0,
        "numeric"       : True,
        "update_policy" : 'GTK_UPDATE_ALWAYS',
        "snap_to_ticks" : False,
        "wrap"          : False,
#        "adjustment"    : '0 0 1000 1 10 0',
        }

class ToolSpinButton(LContainer):
    gtkClass = 'GtkToolItem'
    def __init__(self, el_def, pos, container, elements, layout=None):
        el_def = el_def.replace('ts=', 'ti=')
        #super(LContainer, self).__init__(el_def, pos, container=container, elements=elements, layout=layout)
        super(ToolSpinButton, self).__init__(el_def, pos, container=container, elements=elements, layout=layout)
        ## if spinbutton in toolbar, we need to encapsulate in a ToolItem
        spin_def = el_def.replace('ti=','s=')
        spin = SpinButton("%s" % (spin_def), (0,0), 'ti', elements=elements, layout=layout)
        self.lw['children_list'] = [ spin  ]
        #self.el_def = "ti=%s" % (self.var)
            
class Image(LWidget):
    gtkClass = 'GtkImage'
    def _class_specific(self):
        ## self.var is probably the name of a image file
        ## let's check, look for it and set it with a full path
        #if self.var.startswith('gtk-'):
        if gtk.icon_factory_lookup_default(self.var):
            self.properties['icon_name'] = self.var
            self.properties['icon_size'] = self.w
            self.w = None
            self.properties.pop('width_request', None)
        else:
            cdir = os.getcwd()
            filename = os.path.join(cdir,self.var)
            if os.path.exists(filename) and os.path.isfile(filename):
                self.properties['pixbuf'] = filename

class Window(LContainer):
    gtkClass = 'GtkWindow'
    ## secondo me questo non dovrebbe essere necessario
    class_pack_properties  = {  }
    class_properties = {
        'visible' :            True,
        'title' :              'window1',
        'type' :               'GTK_WINDOW_TOPLEVEL',
        'window_position' :    'GTK_WIN_POS_NONE',
        'modal' :              False,
        'resizable' :          True,
        'destroy_with_parent' : False,
        'decorated' :          True,
        'skip_taskbar_hint' :  False,
        'skip_pager_hint' :    False,
        'type_hint' :          'GDK_WINDOW_TYPE_HINT_NORMAL',
        'gravity' :            'GDK_GRAVITY_NORTH_WEST',
        }    

    def __init__(self, *args, **kw):
        LContainer.__init__(self, *args, **kw)

    def _packing_xml(self, packing_info, ContGtkClass):
        return ''

class Table(LContainer):
    gtkClass = 'GtkTable'
    class_pack_properties = {
        'homogeneous' : False,
#        'expand'       : True,
        'y_options' : 'shrink',
#        'y_options' : 'expand|fill',
        'x_options' : 'shrink',
        }
    #def _class_specific(self):
        #self.pack_properties.update({"y_options": ""})

class VBox(LContainer):
    gtkClass = 'GtkVBox'
    class_pack_properties = {
        'padding' : '0',
        'expand'  : False,
        'fill'    : False,
        }
    
    class_properties = {
        'homogeneous' : False,
        'spacing' : 0,
        }
    
class HBox(VBox):
    gtkClass = 'GtkHBox'
    
class Layout(LContainer):
    gtkClass = 'GtkLayout'
    
class Fixed(LContainer):
    gtkClass = 'GtkFixed'
    
class ScrolledWindow(LContainer):
    gtkClass = 'GtkScrolledWindow'
    def _class_specific(self):
        self.properties.update({
            'hscrollbar_policy' : 'GTK_POLICY_AUTOMATIC',
            'vscrollbar_policy' : 'GTK_POLICY_AUTOMATIC',
            'shadow_type'       : 'GTK_SHADOW_IN',

        })

        self.pack_properties.update({
            'expand' : True,
            'fill' : True,
            #'y_options' : 'GTK_EXPAND|GTK_FILL',
            #'y_options' : 'GTK_EXPAND|GTK_FILL',
            })
    

class Notebook(LContainer):
    gtkClass = 'GtkNotebook'
    def _class_specific(self):
        self.make_v_expandable()

    def _children_xml(self):
        txt = ''
        try: 
            for obj in self.lw['children_list']:
                ## incorrect: it would fail f the only sun of a tab is a Label!!!
                child_type = 'type="tab"' if obj.gtkClass == 'GtkLabel' else ''
                    
                child = self._offset_txt(obj.xml(self.gtkClass), 4)
                txt += "\n<child %s>\n%s\n</child> <!-- closes %s in %s -->\n" % (
                    child_type, child, obj.lw['el_key'], obj.lw['container'])
        except:
            pass
        return txt

class VPaned(LContainer):
    gtkClass = 'GtkVPaned'
    class_pack_properties = {
        'resize' : False,
        }
    def __init__(self, el_def, pos, container, children_list=None, elements=None, layout=None):
                 
        msg = "%s cannot have more that 2 children (now: %s)" % (self.gtkClass, children_list)
        assert len(children_list) <= 2, msg
        LWidget.__init__(self, el_def, pos, container, elements=elements, layout=layout)

        ## the children
        self.lw['children_list'] = children_list
    
    def _class_specific(self):
        self.make_v_expandable()

class HPaned(VPaned):
    gtkClass = 'GtkHPaned'
    #class_pack_properties = VPaned.class_pack_properties

class DrawingArea(LWidget):
    gtkClass = 'GtkDrawingArea'
    
class HandleBox(LContainer):
    gtkClass = 'GtkHandleBox'
    
class Expander(VPaned):
    gtkClass = 'GtkExpander'
    class_properties = {
        'expanded' : False,
        'border_width' : 3,
        }
    def _class_specific(self):
        label = self.el_def.replace('X.','').replace('_',' ')
        self.properties['label'] = label
    
class ViewPort(LContainer):
    gtkClass = 'GtkViewport'
    
class Frame(LContainer):
    # a frame conta
    gtkClass = 'GtkFrame'
    class_properties = {
        'label_xalign' : 0,
        'border_width' : 6,
        'label_yalign' : 0.5,
        'shadow_type' : 'GTK_SHADOW_ETCHED_IN',
        }
    
class FrameWithLabel(LContainer):
    # a frame conta
    gtkClass = 'GtkFrame'
    class_properties = {
        'label_xalign' : 0,
        'label_yalign' : 0.5,
        'border_width' : 6,
        'shadow_type' : 'GTK_SHADOW_ETCHED_IN',
        }
    
    def __init__(self,  el_def, pos, container, children_list=[], elements={}, layout=None):
        super(LContainer, self).__init__(el_def, pos, container, elements, layout=layout)
        ## prepare the names...
        al_str = "A=%s" % (self.val.replace('F','') )
        ## and create the children. We pass the child over to Alignment
        alignment = Alignment(al_str,(0,0),    'F' ,
                              children_list=children_list, elements=elements, layout=layout)
#         fl_def = "fl=%s" % self.val.replace('F','')
#         frame_label = FrameLabel(fl_def, (0,0), 'F', elements=elements, layout=layout) 
#         self.lw['children_list'] = [ alignment, frame_label ]
        self.lw['children_list'] = [ alignment ]
        ## correct the parent_container of the child
        alignment.lw['container_flag'] = 'A'
        
class Alignment(LContainer):
    gtkClass = 'GtkAlignment'
    class_properties = {
        'xalign' : 0.5,
        'yalign' : 0,
        'xscale' : 1,
        'yscale' : 1,
        'top_padding' : 4,
        'bottom_padding' : 4,
        'left_padding' : 8,
        'right_padding' : 4,
        }
    
class EventBox(LContainer):
    gtkClass = 'GtkEventBox'
    class_properties = {
        'visible_window'  : True,
        'above_child' : False,
        }
    def _class_specific(self):
        ## not executed frm EventLabel
        pass

class EventLabel(LContainer):
    gtkClass = 'GtkEventBox'

    def __init__(self,  el_def, pos, container, children_list=[], elements={}, layout=None):
        #dbg.write("EventLabel ", el_def)
        el_def = el_def.replace('L=','E=')
        super(LContainer, self).__init__(el_def, pos, container, elements=elements, layout=layout)
        ## prepare the names...

        lbl_str   = "l=%s:.%s" % (el_def.replace('E=',''), self.w or '')
        ## and create the children
        #print "EventLabel elements: ", elements.keys()
        label = Label(lbl_str,(0,0),    'E', elements=elements, layout=layout)
        self.lw['children_list'] = [ label ]
        
    def _class_specific(self):
        self.pack_properties.update({
            'x_options' : 'fill',
            })


class ComboLE(LContainer):
    # it'a HBox/VBox with a label and an entry
    gtkClass = 'GtkVBox'

    def __init__(self,  el_def, pos, container, children_list=[], elements=None, layout=None):
        super(ComboLE, self).__init__(el_def, pos, container, elements=elements, layout=layout)
        ## prepare the names...
        lbl_str   = "E=%s:%s%s" % (self.val, self.w or '', self.align or '')
        entry_str = "e=%s:%s%s" % (self.var, self.h or '', self.align or '')
        #dbg.debug(entry_str, lbl_str, self.w, self.h)
        ## and create the children
        label = EventLabel(lbl_str,(0,0),    'V', elements=elements, layout=layout)
        entry = Entry(entry_str,(0,1),  'V', elements=elements, layout=layout)
        self.lw['children_list'] = [ label, entry ]
        ### w and h are there only to propagate to label & entry
        self.w = None
        self.h = None
        try:
            del self.properties['height_request']
        except:
            pass
    
    def _class_specific(self):
        self.pack_properties.update({"y_options": ""})

class ScrolledTreeView(LContainer):
    gtkClass = 'GtkScrolledWindow'

    def __init__(self,  el_def, pos, container, children_list=[], elements={}, layout=None):
        el_def = el_def.replace('TVS=','S=')
        super(LContainer, self).__init__(el_def, pos, container, elements, layout=layout)
        ## prepare the names...
        tree = "TV=%s:%s.%s" % (self.val, self.h or '', self.w or '')

        ## and create the children
        new = TreeView(tree,(0,0),    'S', elements, layout=layout)
        self.lw['children_list'] = [ new  ]
    def _class_specific(self):
        self.pack_properties.update({
            'expand' : True,
            'fill' : True,
            })
        

class ScrolledLayout(ScrolledWindow):
    gtkClass = 'GtkScrolledWindow'

    def __init__(self,  el_def, pos, container, children_list=[], elements={}, layout=None):
        el_def = el_def.replace('LS=', 'S=')
        super(LContainer, self).__init__(el_def, pos, container=container, elements=elements, layout=layout)
        ## prepare the names...
        tree = "Lay=%s:%s.%s" % (self.val, self.h or '', self.w or '')

        ## and create the children
        new = Layout(tree,(0,0),    'S', elements=elements, layout=layout)
        self.lw['children_list'] = [ new  ]
    def _class_specific(self):
        self.pack_properties.update({
            'expand' : True,
            'fill' : True,
            })
        

class ScrolledTextView(ScrolledWindow):
    gtkClass = 'GtkScrolledWindow'

    def __init__(self,  el_def, pos, container, children_list=[], elements={}, layout=None):
        el_def = el_def.replace('TXS=','S=')
        #super(LContainer, self).__init__(el_def, pos, container, elements)
        ScrolledWindow.__init__(self, el_def, pos, container=container, elements=elements, layout=layout)
        ## prepare the names...
        tree = "TX=%s:%s.%s" % (el_def.replace('S=',''), self.h or '', self.w or '')

        ## and create the children
        new = TextView(tree,(0,0),    'S', elements=elements, layout=layout)
        self.lw['children_list'] = [ new  ]
        
class ScrolledDrawingArea(ScrolledWindow):
    gtkClass = 'GtkScrolledWindow'

    def __init__(self,  el_def, pos, container, children_list=[], elements={}, layout=None):
        el_def = el_def.replace('daS=','S=')
        #super(LContainer, self).__init__(el_def, pos, container, elements)
        ScrolledWindow.__init__(self, el_def, pos, container=container, elements=elements, layout=layout)
        ## prepare the names...
        wdg = "da=%s:%s.%s" % (self.val, self.h or '', self.w or '')
        #dbg.write('wdg', wdg)
        ## and create the children
        new = DrawingArea(wdg,(0,0),    'S', elements=elements, layout=layout)
        self.lw['children_list'] = [ new  ]
        

class MenuBar(LContainer):
    gtkClass = 'GtkMenuBar'
    class_pack_properties = {
        'y_options' : 'fill',
        'x_options' : 'fill',
        }
    def _class_specific(self):
        if self.container_flag == "T":
            #self.pack_properties.update({'y_options': ''})
            self.pack_properties.update(
                {'y_options': 'fill',
                'x_options' : 'fill',
                 }
                )

class Menu(LContainer):
    gtkClass = 'GtkMenu'
    # I don't mean to fill it up with MenuItems. I'll 

class ToolItem(LContainer):
    gtkClass = 'GtkToolItem'

class MenuItem(LContainer):
    gtkClass = 'GtkMenuItem'
    parent = 'm'

    def __init__(self,  el_def, pos, container, children_list=[], elements={}, layout=None):
        super(MenuItem, self).__init__(el_def, pos, container, [], elements, layout=layout)
        #print "MENU: " + el_def
        ## prepare the names...
        menu = "M=%s" % (self.val)

        ## and create the children
        new = Menu(menu,(0,0), self.parent, children_list=[], elements=elements, layout=layout)
        #print "new menu ", new, menu, self.parent
        self.lw['children_list'] = [ new  ]

    def _class_specific(self):
        self.properties['label'] = _(self.val)
        #self.properties['use_underline'] = True
        
        # specify that we want write label before use_underline
        #self.ordered_properties = ["label", "use_underline"]
        
    def _children_xml(self):
        txt = ''
        try: 
            for obj in self.lw['children_list']:
                ## incorrect: it would fail f the only sun of a tab is a Label!!!
                child_type = 'type="submenu"' if obj.gtkClass == 'GtkMenu' else ''
                    
                child = self._offset_txt(obj.xml(self.gtkClass), 4)
                txt += "\n<child %s>\n%s\n</child> <!-- closes %s in %s -->\n" % (
                    child_type, child, obj.lw['el_key'], obj.lw['container'])
        except:
            pass
        return txt

#         for f in self.lw['children_list']:
#             dbg.debug("%s %s" % (self, self.lw['children_list']))

class MenuToolButton(MenuItem):
    gtkClass = 'GtkMenuToolButton'
    parent = 'tm'
#     transpose = {'var' : 'label',
#                  }
#     # I don't mean to fill it up with MenuItems. I'll 

class Toolbar(LContainer):
    gtkClass = 'GtkToolbar'
    # fixme; transpose does nos seem to work...
    transpose = {'width' : 'width_request',
                 'height' : 'height_request'
                 }
    def _class_specific(self):
        if self.container_flag == "T":
            self.pack_properties.update({'y_options': ''})


class ColorSelection(LWidget):
    gtkClass = 'GtkColorSelection'
    
class ColorButton(LWidget):
    gtkClass = 'GtkColorButton'

##### Widgets list
_W = {
    'l'   : Label,
    'lf'  : LabelFill,
    'ro'  : LabelFill,
    'L'   : EventLabel,
    'nl'  : NotebookLabel,
    'fl'  : FrameLabel,
    'e'   : Entry,
    'ae'  : AlignmentEntry,
    'aC'  : AlignmentComboBox,
    'd'   : DateEdit,
    'dt'  : DateTimeEdit,
    'da'  : DrawingArea,
    'i'   : Image,
    'img' : ImageWidget,
    'b'   : Button,
    'lb'   : LinkButton,
    'r'   : RadioButton,
    't'   : ToggleButton,
    'ti'  : ToolItem,
    'tb'  : ToolButton,
    'c'   : CheckButton,
    's'   : SpinButton,
    'ts'  : ToolSpinButton,
    'prg'   : ProgressBar,
    'C'   : ComboBox,
    'Ce'  : ComboBoxEntry,
    'TV'  : GreedyTreeView,
    'TX'  : TextView,
    'daS' : ScrolledDrawingArea,
    'TVS' : ScrolledTreeView,
    'TXS' : ScrolledTextView,
    'cal' : Calendar,
    'sb'  : Statusbar,
    'Col' : ColorSelection,
    'ColB': ColorButton,

    # containers
    'T': Table,
    'W': Window,
    'E': EventBox,
    'X': Expander,
    'V': VBox,
    'H': HBox,
    'le': ComboLE,
    'S' : ScrolledWindow,
    'N' : Notebook,
    'h' : HPaned,
    'v' : VPaned,
    'p' : ViewPort,
    'f' : Frame,
    'Fix' : Fixed,
    'F' : FrameWithLabel,
    'A' : Alignment,
    'Lay' : Layout,
    'LS' : ScrolledLayout,
    


    'B': MenuBar,
    'M': Menu,
    'm': MenuItem,
    'tm': MenuToolButton,
    'O': Toolbar,
    'Z': HandleBox,
    'container_regexp' : '[EXTHVNPSvhpOBmMFfAZ]',
    }


def custom_widget(glade, function_name, widget_name,
                  str1=None, str2=None, int1=None , int2=None):
    cw = MyCustomWidget()
    return getattr(cw, function_name)(str1, str2)



def register(Class, mod, custom=None, force=False):
    """
    register a class to be used within widgets
    'mod' is the string used in layout
    """
    global _W
    exec "global " +  Class.__name__
    exec "%s = Class" % (Class.__name__)
    if mod in _W and not force:
        msg = "Alias %s is already present. Use force=True to force it" % mod
        raise IdentifiedAlreadyPresent(msg)
    _W[mod] = Class        
    
    if custom:
        MyCustomWidget = custom

def register_alias(Class, alias, force=False):
    """
    register an alias for Class
    """
    global _W
    if alias in _W and not force:
        msg = "Alias %s is already present. Use force=True to force it" % alias
        raise IdentifierAlreadyPresent(msg)
    _W[alias] = Class