Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pynxc/waxy/listbox.py
blob: c1283cc0c34d5bbc79d3a5388448e6af9aa1fe60 (plain)
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
# listbox.py

import wx
import containers
import waxyobject
import styles

class ListBox(wx.ListBox, waxyobject.WaxyObject):

    __events__ = {
        'Click': wx.EVT_LISTBOX,
        'DoubleClick': wx.EVT_LISTBOX_DCLICK,
    }

    def __init__(self, parent, choices=[], size=None, **kwargs):
        style = 0
        style |= self._params(kwargs)
        style |= styles.window(kwargs)

        wx.ListBox.__init__(self, parent, wx.NewId(), size=size or (-1,-1),
         choices=choices, style=style)

        self.BindEvents()

        styles.properties(self, kwargs)

    def SetSelectionType(self, selection):
        style = 0
        selection = selection.lower()
        if selection == "single":
            style |= wx.LB_SINGLE
        elif selection == "multiple":
            style |= wx.LB_MULTIPLE
        elif selection == "extended":
            style |= wx.LB_EXTENDED
        else:
            raise ValueError, "selection must be single, multiple or extended"
        return style

    def SetItems(self, items):
        """ Clear the internal list of items, and set new items.  <items> is
            a list of 2-tuples (string, data). """
        self.Clear()
        for s, data in items:
            self.Append(s, data)

    def Fill(self, items):
        """ Like SetItems, but just uses a list of strings. """
        self.Clear()
        for s in items:
            self.Append(s)

    def GetItems(self):
        """ Return a list of 2-tuples (string, data). """
        items = []
        for i in range(self.GetCount()):
            s = self.GetString(i)
            data = self.GetClientData(i)
            items.append((s, data))
        return items

    # TODO:
    # Add __getitem__, __setitem__, __delitem__
    # These should probably work with tuples (string, data).
    # 1-tuples can be used, in which case data are left alone.
    # Non-tuples are not allowed.

    # We should also be able to iterate over a ListBox...
    # Although for (s, data) in listbox.GetItems() would work too.

    #
    # style parameters

    _listbox_selection = {
        'single': wx.LB_SINGLE,
        'multiple': wx.LB_MULTIPLE,
        'extended': wx.LB_EXTENDED,
    }

    _listbox_scrollbar = {
        'always': wx.LB_ALWAYS_SB,
        'needed': wx.LB_NEEDED_SB,
    }

    def _params(self, kwargs):
        flags = 0
        flags |= styles.styledictstart('selection', self._listbox_selection, kwargs)
        flags |= styles.styledictstart('scrollbar', self._listbox_scrollbar, kwargs)
        flags |= styles.stylebool('sort', wx.LB_SORT, kwargs)
        flags |= styles.stylebool('horizontal_scrollbar', wx.LB_HSCROLL, kwargs)

        return flags