| 1 | |
| 2 | from wxPython.wx import * |
| 3 | |
| 4 | #---------------------------------------------------------------------- |
| 5 | |
| 6 | text = "one two buckle my shoe three four shut the door five six pick up sticks seven eight lay them straight nine ten big fat hen" |
| 7 | |
| 8 | |
| 9 | class ScrolledPanel(wxScrolledWindow): |
| 10 | def __init__(self, parent, log): |
| 11 | self.log = log |
| 12 | wxScrolledWindow.__init__(self, parent, -1, |
| 13 | style = wxTAB_TRAVERSAL) |
| 14 | |
| 15 | box = wxBoxSizer(wxVERTICAL) |
| 16 | box.Add(wxStaticText(self, -1, |
| 17 | "This sample shows how to make a scrollable data entry \n" |
| 18 | "form by using a wxSizer in a wxScrolledWindow."), |
| 19 | 0, wxCENTER|wxALL, 5) |
| 20 | box.Add(wxStaticLine(self, -1), 0, wxEXPAND|wxALL, 5) |
| 21 | |
| 22 | fgs = wxFlexGridSizer(cols=2, vgap=4, hgap=4) |
| 23 | fgs.AddGrowableCol(1) |
| 24 | |
| 25 | # Add some spacers |
| 26 | fgs.Add(75, 10) |
| 27 | fgs.Add(150, 10) |
| 28 | |
| 29 | for word in text.split(): |
| 30 | label = wxStaticText(self, -1, word+":") |
| 31 | tc = wxTextCtrl(self, -1, word) |
| 32 | fgs.Add(label, flag=wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL) |
| 33 | fgs.Add(tc, flag=wxEXPAND) |
| 34 | |
| 35 | box.Add(fgs, 1) |
| 36 | box.Add(10, 40) # some more empty space at the bottom |
| 37 | self.SetSizer(box) |
| 38 | |
| 39 | |
| 40 | # The following is all that is needed to integrate the sizer and the |
| 41 | # scrolled window. In this case we will only support vertical scrolling. |
| 42 | self.EnableScrolling(false, true) |
| 43 | self.SetScrollRate(0, 20) |
| 44 | box.SetVirtualSizeHints(self) |
| 45 | EVT_CHILD_FOCUS(self, self.OnChildFocus) |
| 46 | |
| 47 | wxCallAfter(self.Scroll, 0, 0) # scroll back to top after initial events |
| 48 | |
| 49 | |
| 50 | def OnChildFocus(self, evt): |
| 51 | # If the child window that gets the focus is not visible, |
| 52 | # this handler will try to scroll enough to see it. If you |
| 53 | # need to handle horizontal auto-scrolling too then this will |
| 54 | # need adapted. |
| 55 | evt.Skip() |
| 56 | child = evt.GetWindow() |
| 57 | |
| 58 | sppu_y = self.GetScrollPixelsPerUnit()[1] |
| 59 | vs_y = self.GetViewStart()[1] |
| 60 | cpos = child.GetPosition() |
| 61 | csz = child.GetSize() |
| 62 | |
| 63 | # is it above the top? |
| 64 | if cpos.y < 0: |
| 65 | new_vs = cpos.y / sppu_y |
| 66 | self.Scroll(-1, new_vs) |
| 67 | |
| 68 | # is it below the bottom ? |
| 69 | if cpos.y + csz.height > self.GetClientSize().height: |
| 70 | diff = (cpos.y + csz.height - self.GetClientSize().height) / sppu_y |
| 71 | self.Scroll(-1, vs_y + diff + 1) |
| 72 | |
| 73 | |
| 74 | #---------------------------------------------------------------------- |
| 75 | |
| 76 | |
| 77 | def runTest(frame, nb, log): |
| 78 | win = ScrolledPanel(nb, log) |
| 79 | return win |
| 80 | |
| 81 | #---------------------------------------------------------------------- |
| 82 | |
| 83 | |
| 84 | |
| 85 | overview = """<html><body> |
| 86 | This sample shows how to make a scrollable data entry form by |
| 87 | using a wxSizer in a wxScrolledWindow. |
| 88 | </body></html> |
| 89 | """ |
| 90 | |
| 91 | |
| 92 | |
| 93 | if __name__ == '__main__': |
| 94 | import sys,os |
| 95 | import run |
| 96 | run.main(['', os.path.basename(sys.argv[0])]) |
| 97 | |