2 # Illustrates how to create a wxListCtrl with an associated pop-up menu, which is
3 # activated when the right mouse button is clicked.
5 from wxPython
.wx
import *
8 class cPopupHandler(wxEvtHandler
):
10 def __init__(self
, this
):
11 wxEvtHandler
.__init
__(self
, this
)
14 def ProcessEvent(self
, event
):
16 #wxEvtHandler.ProcessEvent(self, event)
18 if event
.GetEventClass() != wxTYPE_MOUSE_EVENT
:
21 if not event
.ButtonUp(3):
24 if event
.ButtonDown(1):
26 elif event
.ButtonUp(1):
28 elif event
.ButtonDown(3):
30 elif event
.ButtonUp(3):
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.
43 class cMyFrame(wxFrame
):
45 def __init__(self
, parent
, id, title
):
46 wxFrame
.__init
__(self
, parent
, -1, title
, wxDefaultPosition
, wxSize(800, 600))
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))
56 self
.listctrl
= wxListCtrl(self
, id, wxDefaultPosition
, wxDefaultSize
, wxLC_REPORT
)
57 #self.listctrl.SetImageList(self.imagelist, wxIMAGE_LIST_SMALL)
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)
64 #EVT_RIGHT_DOWN(self.listctrl, self.OnSaveMousePos)
66 EVT_LIST_ITEM_SELECTED(self
, id, self
.OnSaveSelection
)
67 EVT_COMMAND_RIGHT_CLICK(self
, id, self
.OnListRightClick
)
69 # create an wxEvtHandler and connect it to the wxListCtrl
71 self
.listctrl
.handler
= cPopupHandler(self
.listctrl
)
74 self
.listctrl
.Connect(id, id, wxEVT_RIGHT_DOWN
, self
.OnListMouseEvent
)
77 # define the ListCtrl column
78 self
.listctrl
.InsertColumn(0, "Name")
80 # create a set of dummy ListCtrl entries
81 for Index
in range(20):
82 self
.listctrl
.InsertStringItem(Index
, "Item number %d" % Index
)
84 # re-adjust the width of the column
85 self
.listctrl
.SetColumnWidth(0, wxLIST_AUTOSIZE_USEHEADER
)
88 def OnSaveSelection(self
, event
):
89 self
.lastSelection
= event
.m_itemIndex
90 print self
.lastSelection
93 def OnListRightClick(self
, event
):
97 menu
.Append(2, "Three")
99 pos
= self
.listctrl
.GetItemPosition(self
.lastSelection
)
100 self
.listctrl
.PopupMenu(menu
, pos
.x
, pos
.y
)
106 frame
= cMyFrame(NULL
, -1, "Popup Sample")
108 self
.SetTopWindow(frame
)
117 if __name__
== "__main__":