Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pynxc/waxy/demos/Grid2.py
blob: e8d8f5b9b2317801aab71b806930e0a3fcbb72f8 (plain)
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
#!/usr/bin/env python

from waxy import *
import  wx


class CustomDataTable(wx.grid.PyGridTableBase):
    def __init__(self):
        wx.grid.PyGridTableBase.__init__(self)

        self.colLabels = ['ID', 'Description', 'Severity', 'Priority', 'Platform',
                          'Opened?', 'Fixed?', 'Tested?', 'TestFloat']

        self.dataTypes = [wx.grid.GRID_VALUE_NUMBER,
                          wx.grid.GRID_VALUE_STRING,
                          wx.grid.GRID_VALUE_CHOICE + ':only in a million years!,wish list,minor,normal,major,critical',
                          wx.grid.GRID_VALUE_NUMBER + ':1,5',
                          wx.grid.GRID_VALUE_CHOICE + ':all,MSW,GTK,other',
                          wx.grid.GRID_VALUE_BOOL,
                          wx.grid.GRID_VALUE_BOOL,
                          wx.grid.GRID_VALUE_BOOL,
                          wx.grid.GRID_VALUE_FLOAT + ':6,2',
                          ]

        self.data = [
            [1010, "The foo doesn't bar", "major", 1, 'MSW', 1, 1, 1, 1.12],
            [1011, "I've got a wicket in my wocket", "wish list", 2, 'other', 0, 0, 0, 1.50],
            [1012, "Rectangle() returns a triangle", "critical", 5, 'all', 0, 0, 0, 1.56]

            ]


    #--------------------------------------------------
    # required methods for the wxPyGridTableBase interface

    def GetNumberRows(self):
        return len(self.data) + 1

    def GetNumberCols(self):
        return len(self.data[0])

    def IsEmptyCell(self, row, col):
        try:
            return not self.data[row][col]
        except IndexError:
            return True

    # Get/Set values in the table.  The Python version of these
    # methods can handle any data-type, (as long as the Editor and
    # Renderer understands the type too,) not just strings as in the
    # C++ version.
    def GetValue(self, row, col):
        try:
            return self.data[row][col]
        except IndexError:
            return ''

    def SetValue(self, row, col, value):
        try:
            self.data[row][col] = value
        except IndexError:
            # add a new row
            self.data.append([''] * self.GetNumberCols())
            self.SetValue(row, col, value)

            # tell the grid we've added a row
            msg = wx.grid.GridTableMessage(self,            # The table
                    wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED, # what we did to it
                    1                                       # how many
                    )

            self.GetView().ProcessTableMessage(msg)


    #--------------------------------------------------
    # Some optional methods

    # Called when the grid needs to display labels
    def GetColLabelValue(self, col):
        return self.colLabels[col]

    # Called to determine the kind of editor/renderer to use by
    # default, doesn't necessarily have to be the same type used
    # natively by the editor/renderer if they know how to convert.
    def GetTypeName(self, row, col):
        return self.dataTypes[col]

    # Called to determine how the data can be fetched and stored by the
    # editor and renderer.  This allows you to enforce some type-safety
    # in the grid.
    def CanGetValueAs(self, row, col, typeName):
        colType = self.dataTypes[col].split(':')[0]
        if typeName == colType:
            return True
        else:
            return False

    def CanSetValueAs(self, row, col, typeName):
        return self.CanGetValueAs(row, col, typeName)



class MyGrid(Grid):
    
    def __init__(self, parent):
        Grid.__init__(self, parent)
        table = CustomDataTable()

        self.SetTable(table, True)

        self.SetRowLabelSize(0)
        self.SetMargins(0,0)
        self.AutoSizeColumns(False)


class MainFrame(VerticalFrame): # frame has a sizer built in

    def Body(self):


        self.CreateStatusBar()
        self.SetStatusText("This is the statusbar")

        menubar = MenuBar(self)
        menu1 = Menu(self)
        menu1.Append("E&xit", self.CloseWindow, "Exit demo",hotkey="Ctrl+Q")
        menubar.Append(menu1, "&File")
        
        
        self.cellbox=TextBox(self)
        self.AddComponent(self.cellbox,border=10,stretch=True)
        self.cellbox.OnChar=self.EditCell
        
        self.grid = MyGrid(self)
        self.AddComponent(self.grid,border=10,expand='both')
        self.grid.OnSelectCell=self.OnSelectCell
        
        self.Pack()
        self.SetSize((1000, 800))
        
        self.CenterOnScreen()

        self.Show()
        self.grid.SetFocus()
        self.cellbox.SetValue(self.grid[0,0])

    def EditCell(self,event):
        
        if event.KeyCode>0 and event.KeyCode<255:  # a character
        
            v=self.cellbox.GetValue()
            r,c=self.grid.GetGridCursorRow(),self.grid.GetGridCursorCol()


            if event.KeyCode==13: # return
                self.grid[r,c]=v
                self.grid.SetFocus()
            elif event.KeyCode==27: # escape
                self.cellbox.SetValue("")
                self.grid.SetFocus()

        event.Skip()
        

    def CloseWindow(self,event):
        self.Close()

    def OnSelectCell(self,event):
        r,c=event.Row,event.Col
        self.cellbox.SetValue(self.grid[r,c])
        event.Skip()
        
        

if __name__=="__main__":
    app = Application(MainFrame, title="Grid")
    app.Run()