File size: 4,553 Bytes
d12bc25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import commctrl
import win32api
import win32con
import win32ui
from pywin.mfc import dialog


class ListDialog(dialog.Dialog):
    def __init__(self, title, list):
        dialog.Dialog.__init__(self, self._maketemplate(title))
        self.HookMessage(self.on_size, win32con.WM_SIZE)
        self.HookNotify(self.OnListItemChange, commctrl.LVN_ITEMCHANGED)
        self.HookCommand(self.OnListClick, win32ui.IDC_LIST1)
        self.items = list

    def _maketemplate(self, title):
        style = win32con.WS_DLGFRAME | win32con.WS_SYSMENU | win32con.WS_VISIBLE
        ls = (
            win32con.WS_CHILD
            | win32con.WS_VISIBLE
            | commctrl.LVS_ALIGNLEFT
            | commctrl.LVS_REPORT
        )
        bs = win32con.WS_CHILD | win32con.WS_VISIBLE
        return [
            [title, (0, 0, 200, 200), style, None, (8, "MS Sans Serif")],
            ["SysListView32", None, win32ui.IDC_LIST1, (0, 0, 200, 200), ls],
            [128, "OK", win32con.IDOK, (10, 0, 50, 14), bs | win32con.BS_DEFPUSHBUTTON],
            [128, "Cancel", win32con.IDCANCEL, (0, 0, 50, 14), bs],
        ]

    def FillList(self):
        size = self.GetWindowRect()
        width = size[2] - size[0] - (10)
        itemDetails = (commctrl.LVCFMT_LEFT, width, "Item", 0)
        self.itemsControl.InsertColumn(0, itemDetails)
        index = 0
        for item in self.items:
            index = self.itemsControl.InsertItem(index + 1, str(item), 0)

    def OnListClick(self, id, code):
        if code == commctrl.NM_DBLCLK:
            self.EndDialog(win32con.IDOK)
        return 1

    def OnListItemChange(self, std, extra):
        (hwndFrom, idFrom, code), (
            itemNotify,
            sub,
            newState,
            oldState,
            change,
            point,
            lparam,
        ) = (std, extra)
        oldSel = (oldState & commctrl.LVIS_SELECTED) != 0
        newSel = (newState & commctrl.LVIS_SELECTED) != 0
        if oldSel != newSel:
            try:
                self.selecteditem = itemNotify
                self.butOK.EnableWindow(1)
            except win32ui.error:
                self.selecteditem = None

    def OnInitDialog(self):
        rc = dialog.Dialog.OnInitDialog(self)
        self.itemsControl = self.GetDlgItem(win32ui.IDC_LIST1)
        self.butOK = self.GetDlgItem(win32con.IDOK)
        self.butCancel = self.GetDlgItem(win32con.IDCANCEL)

        self.FillList()

        size = self.GetWindowRect()
        self.LayoutControls(size[2] - size[0], size[3] - size[1])
        self.butOK.EnableWindow(0)  # wait for first selection
        return rc

    def LayoutControls(self, w, h):
        self.itemsControl.MoveWindow((0, 0, w, h - 30))
        self.butCancel.MoveWindow((10, h - 24, 60, h - 4))
        self.butOK.MoveWindow((w - 60, h - 24, w - 10, h - 4))

    def on_size(self, params):
        lparam = params[3]
        w = win32api.LOWORD(lparam)
        h = win32api.HIWORD(lparam)
        self.LayoutControls(w, h)


class ListsDialog(ListDialog):
    def __init__(self, title, list, colHeadings=["Item"]):
        ListDialog.__init__(self, title, list)
        self.colHeadings = colHeadings

    def FillList(self):
        index = 0
        size = self.GetWindowRect()
        width = (
            size[2] - size[0] - (10) - win32api.GetSystemMetrics(win32con.SM_CXVSCROLL)
        )
        numCols = len(self.colHeadings)

        for col in self.colHeadings:
            itemDetails = (commctrl.LVCFMT_LEFT, int(width / numCols), col, 0)
            self.itemsControl.InsertColumn(index, itemDetails)
            index = index + 1
        index = 0
        for items in self.items:
            index = self.itemsControl.InsertItem(index + 1, str(items[0]), 0)
            for itemno in range(1, numCols):
                item = items[itemno]
                self.itemsControl.SetItemText(index, itemno, str(item))


def SelectFromList(title, lst):
    dlg = ListDialog(title, lst)
    if dlg.DoModal() == win32con.IDOK:
        return dlg.selecteditem
    else:
        return None


def SelectFromLists(title, lists, headings):
    dlg = ListsDialog(title, lists, headings)
    if dlg.DoModal() == win32con.IDOK:
        return dlg.selecteditem
    else:
        return None


def test():
    # 	print SelectFromList('Single list',  [1,2,3])
    print(
        SelectFromLists(
            "Multi-List",
            [("1", 1, "a"), ("2", 2, "b"), ("3", 3, "c")],
            ["Col 1", "Col 2"],
        )
    )


if __name__ == "__main__":
    test()