2

I count lines in python for a text like this:

count = 0
for ch in text:
    if( ch == "\n" ):
        count += 1
return count

The same text goes into a textBox control. I would like to scroll to the last line but the line count does not help , because the textBox wraps long lines making them 2 or more lines.
I can go to the last line by scrolling to -1, but then I cannot scroll up anymore. I need to know the (actual) max line count so I can scroll to any position i want.

lastLine = self.count_lines( text )
self.getControl( 100 ).setText( text )
self.getControl( 100 ).scroll( lastLine )
ilomambo
  • 8,290
  • 12
  • 57
  • 106
  • 4
    To count the newlines in a given Python string, `len(text.splitlines()` would be easier. Or even `text.count('\n')`. – Martijn Pieters Mar 28 '13 at 14:16
  • @MartijnPieters But would it give me a count that takes into account the real number of lines in the textBox, including the wrapped ones? – ilomambo Mar 28 '13 at 14:20
  • @MartijnPieters - your two methods give different results. – eumiro Mar 28 '13 at 14:21
  • 2
    @eumiro: Sure, that depends on what you wanted to count. The second at least matches what the OP used, but the former is the real line count. – Martijn Pieters Mar 28 '13 at 14:22
  • 1
    @ilomambo: I was only commenting on your *I count lines in python for a text like this* text. No idea about XBMC text boxes, sorry. – Martijn Pieters Mar 28 '13 at 14:23
  • Does the `scroll` command scroll to a line number (as you assume) or does it scroll to a string position? As far as I can tell from the API, you don't have access to the number of lines (which is determined by the font-size, box width, etc.). If it's a line number, then it'll be hard to do this reliably. – Geoff Mar 30 '13 at 14:33
  • It looks like there's a discussion about this in github: https://github.com/xbmc/xbmc/pull/2073 – Geoff Mar 30 '13 at 17:33
  • @Geoff From the source code of GUITextBox.cpp, the position parameter is compared to m_lines.size, so it seems to be it represents lines. It does have a protected method GetRows(), but as it is protected it is not available. – ilomambo Mar 31 '13 at 14:22
  • @ilomambo - Yeah, it doesn't sound very useful at all... – Geoff Mar 31 '13 at 17:39

1 Answers1

1

you said a long line is divided in 2 lines or more,right?

you could count lines this way

count=0
for a in string.split():
    count+= 1+a//MaxLen

where string is the text you are dealing with, and MaxLen is the maximum number of characters that the textbox can show in a line

but this just doesn't solve the problem if you don't know how to get MaxLen,and i actually don't...

Alberto Perrella
  • 1,318
  • 5
  • 12
  • 19
  • 1
    thanks Alberto. It seems this problem is know and it is not solvable at the moment. The XBMC team did not foresee the uses of TextBox control in full, since XBMC is first and mostly media oriented. – ilomambo Apr 01 '13 at 09:37