~launchpad-p-s/sofastatistics/main

« back to all changes in this revision

Viewing changes to text_browser.py

  • Committer: Grant Paton-Simpson
  • Date: 2009-05-19 04:21:43 UTC
  • Revision ID: g@ubuntu-20090519042143-p561mbokz3inefvd
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
import wx
 
3
 
 
4
class KeyDownEvent(wx.PyCommandEvent):
 
5
    "See 3.6.1 in wxPython in Action"
 
6
    def __init__(self, evtType, id):
 
7
        wx.PyCommandEvent.__init__(self, evtType, id)
 
8
    
 
9
    def SetKeyCode(self, keycode):
 
10
        self.keycode = keycode
 
11
    
 
12
    def GetKeyCode(self):
 
13
        return self.keycode
 
14
 
 
15
# new event type to pass around
 
16
myEVT_TEXT_BROWSE_KEY_DOWN = wx.NewEventType()
 
17
# event to bind to
 
18
EVT_TEXT_BROWSE_KEY_DOWN = wx.PyEventBinder(myEVT_TEXT_BROWSE_KEY_DOWN, 1)
 
19
 
 
20
 
 
21
class TextBrowse(wx.PyControl):
 
22
    """
 
23
    Custom control with a text box and browse button (to populate text box).
 
24
    Handles Enter key and tab key strokes as expected.
 
25
    NB if Enter hit when in text box, custom event sent for processing.
 
26
    """
 
27
 
 
28
    def __init__(self, parent, id, file_phrase, wildcard=""):
 
29
        wx.Control.__init__(self, parent, -1)
 
30
        self.file_phrase = file_phrase
 
31
        self.wildcard = wildcard
 
32
        self.txt = wx.TextCtrl(self, -1, "", style=wx.TE_PROCESS_ENTER)
 
33
        self.Bind(wx.EVT_SIZE, self.OnSize)
 
34
        self.txt.Bind(wx.EVT_KEY_DOWN, self.OnTxtKeyDown)
 
35
        self.btnBrowse = wx.Button(self, -1, "Browse ...")
 
36
        self.btnBrowse.Bind(wx.EVT_BUTTON, self.OnBtnBrowseClick)
 
37
        self.btnBrowse.Bind(wx.EVT_KEY_DOWN, self.OnBtnBrowseKeyDown)
 
38
        szr = wx.BoxSizer(wx.HORIZONTAL)
 
39
        szr.Add(self.txt, 1, wx.GROW|wx.RIGHT|wx.LEFT, 5)
 
40
        szr.Add(self.btnBrowse, 0)
 
41
        szr.SetSizeHints(self)
 
42
        self.SetSizer(szr)
 
43
        self.Layout()
 
44
    
 
45
    def OnTxtKeyDown(self, event):
 
46
        """
 
47
        http://wiki.wxpython.org/AnotherTutorial#head-999ff1e3fbf5694a51a91cf4ed2140f692da013c
 
48
        """
 
49
        if event.GetKeyCode() in [wx.WXK_RETURN]:
 
50
            key_event = KeyDownEvent(myEVT_TEXT_BROWSE_KEY_DOWN, self.GetId())
 
51
            key_event.SetEventObject(self)
 
52
            key_event.SetKeyCode(wx.WXK_RETURN)
 
53
            self.GetEventHandler().ProcessEvent(key_event)
 
54
        elif event.GetKeyCode() in [wx.WXK_TAB]:
 
55
            self.btnBrowse.SetFocus()
 
56
        else:
 
57
            event.Skip()
 
58
    
 
59
    def OnBtnBrowseKeyDown(self, event):
 
60
        if event.GetKeyCode() in [wx.WXK_TAB]:
 
61
            self.txt.SetFocus()
 
62
        else:
 
63
            event.Skip()
 
64
    
 
65
    def OnSize(self, event):
 
66
        overall_width = self.GetSize()[0]
 
67
        btn_size = self.btnBrowse.GetSize()
 
68
        btn_width = btn_size[0]
 
69
        btn_height = btn_size[1]
 
70
        inner_padding = 10 # look at settings for left and right margins
 
71
        self.txt.SetSize(wx.Size(overall_width - btn_width - inner_padding, -1))
 
72
        self.btnBrowse.SetDimensions(overall_width - btn_width, -1, btn_width, btn_height)
 
73
        #event.Skip() # otherwise, resizing sets infinite number of EndEdits!
 
74
    
 
75
    def OnBtnBrowseClick(self, event):
 
76
        "Open dialog and takes the file selected (if any)"
 
77
        dlgGetFile = wx.FileDialog(self, message=self.file_phrase, 
 
78
                                   wildcard=self.wildcard) 
 
79
            #defaultDir="spreadsheets", 
 
80
            #defaultFile="", )
 
81
        #MUST have a parent to enforce modal in Windows
 
82
        if dlgGetFile.ShowModal() == wx.ID_OK:
 
83
            self.txt.SetValue("%s" % dlgGetFile.GetPath())
 
84
        dlgGetFile.Destroy()
 
85
        self.txt.SetFocus()
 
86
    
 
87
    def SetText(self, text):
 
88
        self.txt.SetValue(text)
 
89
    
 
90
    def SetInsertionPoint(self, i):
 
91
        self.txt.SetInsertionPoint(i)
 
92
    
 
93
    def GetText(self):
 
94
        return self.txt.GetValue()
 
95
    
 
96
    def SetFocus(self):
 
97
        "Must implement this if I want to call for the custom control"
 
98
        self.txt.SetFocus()
 
99
 
 
100
 
 
101
class GridCellTextBrowseEditor(wx.grid.PyGridCellEditor):
 
102
    """
 
103
    Provides a text box and a browse button (which can populate the text box).
 
104
    The text browser can send a special event to the grid frame if Enter key
 
105
        pressed while in the text box.
 
106
    """
 
107
    def __init__(self, file_phrase, wildcard):
 
108
        self.debug = False
 
109
        wx.grid.PyGridCellEditor.__init__(self)
 
110
        self.file_phrase = file_phrase
 
111
        self.wildcard = wildcard
 
112
    
 
113
    def Create(self, parent, id, evt_handler):
 
114
        self.text_browse = TextBrowse(parent, -1, self.file_phrase, 
 
115
                                      self.wildcard)
 
116
        self.SetControl(self.text_browse)
 
117
        if evt_handler:
 
118
            self.text_browse.PushEventHandler(evt_handler)
 
119
    
 
120
    def BeginEdit(self, row, col, grid):
 
121
        if self.debug: print "Beginning edit"
 
122
        self.text_browse.SetText(grid.GetCellValue(row, col))
 
123
        self.text_browse.SetFocus()
 
124
    
 
125
    def StartingKey(self, event):
 
126
        if event.GetKeyCode() <= 255:
 
127
            self.text_browse.SetText(chr(event.GetKeyCode()))
 
128
            self.text_browse.SetInsertionPoint(1)
 
129
        else:
 
130
            event.Skip()
 
131
    
 
132
    def SetSize(self, rect):
 
133
        self.text_browse.SetDimensions(rect.x, rect.y-2, rect.width, rect.height+5,
 
134
                                       wx.SIZE_ALLOW_MINUS_ONE)
 
135
       
 
136
    def EndEdit(self, row, col, grid):
 
137
        "TODO - if resized while editing, infinite cycling of this!"
 
138
        grid.SetCellValue(row, col, self.text_browse.GetText())
 
139
        return True
 
140
    
 
141
    def Reset(self):
 
142
        pass # N/A
 
143
    
 
144
    def Clone(self):
 
145
        return GridCellTextBrowseEditor(self.file_phrase, self.wildcard)
 
146
    
 
 
b'\\ No newline at end of file'