Welcome, guest | Sign In | My Account | Store | Cart

ObjectListView is a 3rd party wxPython 2.8+ module that adds a more object-friendly API to the wx.ListCtrl(). When clicking on an item in the list it's easy to process mouse click events or item selection events. However some OS platforms do not set all the event's attributes. Also the various HitTest() methods currently on various platforms are not implemented the same. So here is a little recipe to get the column number (first column equals zero) when left clicking and item in the ObjectListView with the mouse.

Python, 78 lines
 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
def GetOLVColClicked(event):

    # DevPlayer@gmail.com  2011-01 Jan-13
    # For use with a 3rd party module named ObjectListView
    # used with wxPython.

    """
    GetColClicked( event ) -> int Column number of mouse click.

    Get ObjectListView() column the user single-left-clicked the mouse in.

    You can use the column number to set the modelObject's attributes 
    without removing, re-adding, resorting the items in the OVL.

    This event handler is often bound to the event handler of the 
    wx.EVT_LIST_ITEM_SELECTED event. Other events may be needed for 
    the column's labels - the labels visually naming a column.

    This assumes the OLV.LayoutDirection() is LTR.
    """

    # ----------------------------------------------------------
    # Get the mouse position. Determine which column the user 
    # clicked in.
    # This could probably all be done in some list hit test event.
    # Not all OS platforms set all events m_...atributes. This is a 
    # work around.

    # Get point user clicked, here in screen coordinates. 
    # Then convert the point to OVL control coordinates.

    spt = wx.GetMousePosition()
    fpt = folv.ScreenToClient(spt)    # USE THIS ONE
    x, y = fpt

    #log( o, "GetOLVColClicked()                                   event.m_col: %d "% event.m_col)
    #log( o, "GetOLVColClicked()    folv.ScreenToClient(wx.GetMousePosition()): %d "% x)

    # Get all column widths, individually, of the OLV control .
    # Then compare if the mouse clicked in that column. 

    # Make this a per-click calculation as column widths can 
    # change often by the user and dynammically by different 
    # lengths of data strings in rows.

    last_col = 0
    for col in range(folv.GetColumnCount()) :

        # Get this OLV column's width in pixels.

        col_width = folv.GetColumnWidth(col)

        # Calculate the left and right vertical pixel positions
        # of this current column.

        left_pxl_col = last_col
        right_pxl_col = last_col + col_width - 1

        # Compare mouse click point in control coordinates,
        # (verse screen coordinates) to left and right coordinate of
        # each column consecutively until found.
        
        if left_pxl_col <= x <= right_pxl_col :

            # Mouse was clicked in the current column "col"; done

            col_selected = col
            break

        col_selected = None

        # prep for next calculation of next column

        last_col = last_col + col_width

    #log( o, 'GetOLVColClicked()    clicked in COLUMN %d' % col_selected)

    return col_selected

I left in a bunch of comments to help novices follow along, as some parts of the ObjectListView widget can get confusing. Removing the comments may help understanding.