Skip to content

Commit 7985e13

Browse files
committed
created own wordwrap to better fit cell space
1 parent f465674 commit 7985e13

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/robotide/editor/cellrenderer.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,42 @@ def __init__(self, default_width, max_width, auto_fit, word_wrap=True):
3131
self.auto_fit = auto_fit
3232
self.word_wrap = word_wrap
3333

34+
def _wordwrap(self, text, width, dc, breakLongWords=True, margin=0):
35+
''' modification of ofiginal wordwrap function without extra space'''
36+
wrapped_lines = []
37+
text = text.split('\n')
38+
for line in text:
39+
pte = dc.GetPartialTextExtents(line)
40+
wid = (width - (2 * margin + 1) * dc.GetTextExtent(' ')[0])
41+
idx = 0
42+
start = 0
43+
startIdx = 0
44+
spcIdx = -1
45+
while idx < len(pte):
46+
# remember the last seen space
47+
if line[idx] == ' ':
48+
spcIdx = idx
49+
50+
# have we reached the max width?
51+
if pte[idx] - start > wid and (spcIdx != -1 or breakLongWords):
52+
if spcIdx != -1:
53+
idx = min(spcIdx + 1, len(pte) - 1)
54+
wrapped_lines.append(' ' * margin + line[startIdx: idx] + ' ' * margin)
55+
start = pte[idx]
56+
startIdx = idx
57+
spcIdx = -1
58+
59+
idx += 1
60+
61+
wrapped_lines.append(' ' * margin + line[startIdx: idx] + ' ' * margin)
62+
63+
return '\n'.join(wrapped_lines)
64+
3465
def Draw(self, grid, attr, dc, rect, row, col, isSelected):
3566
text = grid.GetCellValue(row, col)
3667
dc.SetFont(attr.GetFont())
37-
text = wordwrap.wordwrap(text, grid.GetColSize(col) + 10, dc, breakLongWords=False)
68+
suggest_width = grid.GetColSize(col)
69+
text = self._wordwrap(text, suggest_width, dc, breakLongWords=False)
3870
hAlign, vAlign = attr.GetAlignment()
3971
if isSelected:
4072
bg = grid.GetSelectionBackground()
@@ -67,9 +99,9 @@ def GetBestSize(self, grid, attr, dc, row, col):
6799
if self.auto_fit:
68100
col_width = min(w, self.max_width)
69101
else:
70-
col_width = min(self.default_width, w)
71-
suggest_width = grid.GetColSize(col) + dc.GetTextExtent("w")[0]
72-
text = wordwrap.wordwrap(text, suggest_width, dc, breakLongWords=False)
102+
col_width = min(w, self.default_width)
103+
suggest_width = grid.GetColSize(col)
104+
text = self._wordwrap(text, suggest_width, dc, breakLongWords=False)
73105
w, h = dc.GetMultiLineTextExtent(text)
74106
row_height = h
75107
else:

0 commit comments

Comments
 (0)