views:

132

answers:

2

I'm writing an e-book reader in Python + wxPython, and I'd like to find out how many lines of text can be displayed in a given RichTextCtrl with the current formatting without scrolling.

I thought of using and dividing the control's height by RichTextCtrl.GetFont().GetPixelSize(), but it appears that the pixel size parameter of wx.Font is only specified on Windows and GTK. In addition, this won't cover any additional vertical spacing between lines/paragraphs.

I could of course get the font size in points, attempt to get the display's resolution in ppi, and do it that way, but 1) the line spacing problem still remains and 2) this is far too low a level of abstraction for something like this.

Is there a sane way of doing this?

EDIT: The objective is, to divide the ebook up into pages, so the scrolling unit is a whole page, as opposed to a line.

A: 

Did you try calling the GetNumberOfLines() method? According to Robin Dunn, that should work, although it doesn't take wrapped lines into account.

Mike Driscoll
That doesn't help though. `GetNumberOfLines()` will only show me how many lines of text the `RichTextCtrl` has, not how many it _can_ display without scrolling. And even if it didn't, the fact that it ignores wrapped lines is a deal-breaker, I'm afraid...
Chinmay Kanchi
Well, I'm not sure that it can be done. Your best bet is to ask on the official wxPython mailing list though. That's where the the advanced wx users/devs and the wx creator hang out.
Mike Driscoll
I'm actually well on the way to the solution at the moment :). Will post it up here if no one gets the bounty. It is still very low-level though...
Chinmay Kanchi
+2  A: 

Source code of PageDown method suggest that there is not a sane way to do this...

Here is my insane proposition (which breaks widget content, caret, displayed position...) which scroll one page and measure how long this scroll is...

def GetLineHeight(rtc):
    tallString = "\n".join([str(i) for i in xrange(200)])
    rtc.SetValue(tallString)
    rtc.SetInsertionPoint(0)
    rtc.PageDown()
    pos = rtc.GetInsertionPoint()
    end = tallString.find("\n",pos)
    lineHeight=int(tallString[pos:end])
    return lineHeight
FabienAndre
Yikes! That is one fugly piece of code (I mean the `PageDown()` method). I'm actually working on trying to use `wx.ScreenDC().GetPPI()` and `wx.richtext.RichTextCtrl.GetTextExtent()` to figure this out, and it works ok, but I still have some issues to iron out. +1 for the only solution though.
Chinmay Kanchi
I'm going to wait for the bounty to run its course, in case I get a better solution, but your solution looks at least as good as my own, as well as a bit less fiddly.
Chinmay Kanchi