1
2
3
4
5
6
7
8
9
10
11
12
13 """columnsizer -- a mixin to handle column resizing for list controls
14
15 This mixin provides the capabibility to resize columns in a
16 report-mode list control. Integer flags are used for each column to
17 indicate whether the column should be a fixed size or a width
18 proportional to te length of the largest item in the column. It also
19 tries (with only some success) to avoid using a horizontal scrollbar
20 if at all possible.
21 """
22 import wx
23 import logging
24
25
27 """Enhancement to ListCtrl to handle column resizing.
28
29 Resizes columns to a fixed size or based on the size of the
30 contents, but constrains the whole width to the visible area of
31 the list. Theoretically there won't be any horizontal scrollbars,
32 but this doesnt' yet work on GTK, at least.
33 """
35 self._resize_flags = None
36 self._resize_dirty = True
37 self._last_size = None
38 self.Bind(wx.EVT_SIZE, self.OnSize)
39
41 logging.debug("OnSize recieved event: %s" % evt.GetSize())
42 if self._last_size is None or self._last_size != evt.GetSize():
43 self._resize_dirty = True
44 self._last_size = evt.GetSize()
45 wx.CallAfter(self.resizeColumnsIfDirty)
46 evt.Skip()
47
53
55 try:
56 self._resize_dirty = True
57 self._resizeColumns(flags)
58 except wx._core.PyDeadObjectError:
59
60
61
62 logging.debug("Caught dead object error for %s" % self)
63 pass
64
66 """Resize each column according to the flag.
67
68 For each column, the respective flag indicates the following:
69
70 0: smallest width that fits the entire string
71 1: smallest width, and keep this column fixed width if possible
72 >1: maximum size
73 <0: absolute value is the minimum size
74 """
75 if not self._resize_dirty:
76 return
77 self.Freeze()
78 if self._resize_flags is None or len(flags) > 0:
79
80
81 copy = list(flags)
82 if len(copy) < self.GetColumnCount():
83 copy.extend([0] * (self.GetColumnCount() - len(copy)))
84 self._resize_flags = tuple(copy)
85 logging.debug("Resetting flags to %s" % str(self._resize_flags))
86
87 flags = self._resize_flags
88 fixed_width = 0
89 total_width = 0
90 num_fixed = 0
91 for col in range(self.GetColumnCount()):
92 self.SetColumnWidth(col, wx.LIST_AUTOSIZE)
93 flag = flags[col]
94 if flag > 1:
95 after = self.GetColumnWidth(col)
96 if after > flag:
97 self.SetColumnWidth(col, flag)
98 elif flag < 0:
99 after = self.GetColumnWidth(col)
100 if after < -flag:
101 self.SetColumnWidth(col, -flag)
102
103 after = self.GetColumnWidth(col)
104 total_width += after
105 if flag == 1:
106 num_fixed += 1
107 fixed_width += after
108
109
110
111
112 w, h = self.GetClientSizeTuple()
113 logging.debug("Client width = %d, fixed_width = %d" % (w, fixed_width))
114 w -= fixed_width
115 for col in range(self.GetColumnCount()):
116 before = self.GetColumnWidth(col)
117 logging.debug("Col %d: flag=%d before=%d" % (col, flags[col], before))
118 if flags[col] != 1:
119 self.SetColumnWidth(col, before*w/total_width)
120 self.Thaw()
121 self._resize_dirty = False
122