| 1 | # popup.py: |
| 2 | # Illustrates how to create a wxListCtrl with an associated pop-up menu, which is |
| 3 | # activated when the right mouse button is clicked. |
| 4 | |
| 5 | from wxPython.wx import * |
| 6 | |
| 7 | |
| 8 | class cPopupHandler(wxEvtHandler): |
| 9 | |
| 10 | def __init__(self, this): |
| 11 | wxEvtHandler.__init__(self, this) |
| 12 | |
| 13 | |
| 14 | def ProcessEvent(self, event): |
| 15 | print "G" |
| 16 | #wxEvtHandler.ProcessEvent(self, event) |
| 17 | |
| 18 | if event.GetEventClass() != wxTYPE_MOUSE_EVENT: |
| 19 | return |
| 20 | |
| 21 | if not event.ButtonUp(3): |
| 22 | return |
| 23 | |
| 24 | if event.ButtonDown(1): |
| 25 | print "left down" |
| 26 | elif event.ButtonUp(1): |
| 27 | print "left up" |
| 28 | elif event.ButtonDown(3): |
| 29 | print "right down" |
| 30 | elif event.ButtonUp(3): |
| 31 | print "right up" |
| 32 | |
| 33 | |
| 34 | def xProcessEvent(self, event): |
| 35 | # I tried to pass this one in as the Connect() handler, |
| 36 | # but all I got from that was that the icons disappeared |
| 37 | # from the wxListCtrl. |
| 38 | print "H" |
| 39 | pass |
| 40 | |
| 41 | |
| 42 | |
| 43 | class cMyFrame(wxFrame): |
| 44 | |
| 45 | def __init__(self, parent, id, title): |
| 46 | wxFrame.__init__(self, parent, -1, title, wxDefaultPosition, wxSize(800, 600)) |
| 47 | |
| 48 | self.Centre(wxBOTH) |
| 49 | |
| 50 | # create a dummy icon; can't seem to get the wxListCtrl to work without an icon |
| 51 | #self.imagelist = wxImageList(16, 16) |
| 52 | #self.image = self.imagelist.Add(wxNoRefBitmap('smile.bmp', wxBITMAP_TYPE_BMP)) |
| 53 | |
| 54 | # create a ListCtrl |
| 55 | id = NewId() |
| 56 | self.listctrl = wxListCtrl(self, id, wxDefaultPosition, wxDefaultSize, wxLC_REPORT) |
| 57 | #self.listctrl.SetImageList(self.imagelist, wxIMAGE_LIST_SMALL) |
| 58 | |
| 59 | if 1: |
| 60 | # install a handler for mouse right button up events |
| 61 | #EVT_RIGHT_DOWN(self.listctrl, self.OnListMouseEvent) |
| 62 | #EVT_RIGHT_UP(self.listctrl, self.OnListMouseEvent) |
| 63 | |
| 64 | #EVT_RIGHT_DOWN(self.listctrl, self.OnSaveMousePos) |
| 65 | |
| 66 | EVT_LIST_ITEM_SELECTED(self, id, self.OnSaveSelection) |
| 67 | EVT_COMMAND_RIGHT_CLICK(self, id, self.OnListRightClick) |
| 68 | else: |
| 69 | # create an wxEvtHandler and connect it to the wxListCtrl |
| 70 | print "A" |
| 71 | self.listctrl.handler = cPopupHandler(self.listctrl) |
| 72 | print "B" |
| 73 | id = NewId() |
| 74 | self.listctrl.Connect(id, id, wxEVT_RIGHT_DOWN, self.OnListMouseEvent) |
| 75 | print "C" |
| 76 | |
| 77 | # define the ListCtrl column |
| 78 | self.listctrl.InsertColumn(0, "Name") |
| 79 | |
| 80 | # create a set of dummy ListCtrl entries |
| 81 | for Index in range(20): |
| 82 | self.listctrl.InsertStringItem(Index, "Item number %d" % Index) |
| 83 | |
| 84 | # re-adjust the width of the column |
| 85 | self.listctrl.SetColumnWidth(0, wxLIST_AUTOSIZE_USEHEADER) |
| 86 | |
| 87 | |
| 88 | def OnSaveSelection(self, event): |
| 89 | self.lastSelection = event.m_itemIndex |
| 90 | print self.lastSelection |
| 91 | |
| 92 | |
| 93 | def OnListRightClick(self, event): |
| 94 | menu = wxPyMenu() |
| 95 | menu.Append(0, "One") |
| 96 | menu.Append(1, "Two") |
| 97 | menu.Append(2, "Three") |
| 98 | |
| 99 | pos = self.listctrl.GetItemPosition(self.lastSelection) |
| 100 | self.listctrl.PopupMenu(menu, pos.x, pos.y) |
| 101 | |
| 102 | |
| 103 | class cMyApp(wxApp): |
| 104 | |
| 105 | def OnInit(self): |
| 106 | frame = cMyFrame(NULL, -1, "Popup Sample") |
| 107 | frame.Show(true) |
| 108 | self.SetTopWindow(frame) |
| 109 | return true |
| 110 | |
| 111 | |
| 112 | def main(): |
| 113 | App = cMyApp(0) |
| 114 | App.MainLoop() |
| 115 | |
| 116 | |
| 117 | if __name__ == "__main__": |
| 118 | main() |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |