views:

366

answers:

3

I am using a wxPython wx.html.HtmlWindow to display part of my interface. The user can scroll down a list of links in a window smaller than the list. When they click on a link, I need to repaint the web page, but I want to return the page position back to where they clicked it.

I've tried MouseEvent.GetLogicalPosition() on the event, but it wants a DC and the best I've been able to do is get the same information as GetPosition(), so I must not be feeding it the right one.

I also tried HtmlWindow.CalcScrolledPosition(), but apparently that isn't available in HtmlWindow because I get a NotImplementedError...

What I would like is a scroll position that can be derived from the MouseEvent, or the OnLinkClicked information.

I know about HtmlWindow.ScrollToAnchor(), but it's flaky and unaesthetic -- I would prefer to bypass it if possible so that I can scroll back exactly to where the user clicked.

Thanks!

+1  A: 

how about having a look at the source of wxHtmlWindow for inspiration? for example at wxHtmlWindow::LoadPage(): it

// store[s the current] scroll position into history item:
int x, y;
GetViewStart(&x, &y);
(*m_History)[m_HistoryPos].SetPos(y);

this saved scroll position is used in wxHtmlWindow::HistoryBack():

Scroll(0, (*m_History)[m_HistoryPos].GetPos());
Refresh();

to go back to the saved position.

i would assume that this built-in "go-to-the-last-position-in-window" handling isn't the most "flaky and unaesthetic". could something similar work for you, too?

ax
A: 

Usually, click events are trigged by MouseUp events. If you track the mouse position by capturing any MouseDown events, you will know where the last click (MouseUp) happened, and that should allow you to reconstruct things.

For this particular problem, you might have to do a bit more work in MouseDown like checking if they are within the wxHtmlWindow control and if so, then saving something like a line number.

Michael Dillon
A: 

Maybe a bit late, but with some tips from ax, and some hints from here, I think that calling:

scrollpos = wx.html.HtmlWindow.GetViewStart()[1]

and storing that, and then later doing a call to:

wx.html.HtmlWindow.Scroll(0, scrollpos)

works for me. Of course, you do need to change wx.html.HtmlWindow to an actual reference to an instance.

Laurens