+ int y = 0;
+ for ( size_t line = 0; line < m_nLines; line++ )
+ {
+ int yPhys;
+ CalcScrolledPosition(0, y, NULL, &yPhys);
+
+ dc.DrawText(wxString::Format("Line %u (logical %d, physical %d)",
+ unsigned(line), y, yPhys), 0, y);
+ y += m_hLine;
+ }
+}
+
+void MyScrolledWindowSmart::OnDraw(wxDC& dc)
+{
+ // this is useful to see which lines are redrawn
+ static size_t s_redrawCount = 0;
+ dc.SetTextForeground(s_redrawCount++ % 2 ? *wxRED : *wxBLUE);
+
+ // update region is always in device coords, translate to logical ones
+ wxRect rectUpdate = GetUpdateRegion().GetBox();
+ CalcUnscrolledPosition(rectUpdate.x, rectUpdate.y,
+ &rectUpdate.x, &rectUpdate.y);
+
+ size_t lineFrom = rectUpdate.y / m_hLine,
+ lineTo = rectUpdate.GetBottom() / m_hLine;
+
+ if ( lineTo > m_nLines - 1)
+ lineTo = m_nLines - 1;
+
+ int y = lineFrom*m_hLine;
+ for ( size_t line = lineFrom; line <= lineTo; line++ )
+ {
+ int yPhys;
+ CalcScrolledPosition(0, y, NULL, &yPhys);
+
+ dc.DrawText(wxString::Format("Line %u (logical %d, physical %d)",
+ unsigned(line), y, yPhys), 0, y);
+ y += m_hLine;
+ }
+}
+
+// ----------------------------------------------------------------------------
+// MyAutoScrollingWindow
+// ----------------------------------------------------------------------------
+
+BEGIN_EVENT_TABLE(MyAutoScrollingWindow, wxScrolled<wxWindow>)
+ EVT_LEFT_DOWN(MyAutoScrollingWindow::OnMouseLeftDown)
+ EVT_LEFT_UP(MyAutoScrollingWindow::OnMouseLeftUp)
+ EVT_MOTION(MyAutoScrollingWindow::OnMouseMove)
+ EVT_MOUSE_CAPTURE_LOST(MyAutoScrollingWindow::OnMouseCaptureLost)
+ EVT_SCROLLWIN(MyAutoScrollingWindow::OnScroll)
+END_EVENT_TABLE()
+
+MyAutoScrollingWindow::MyAutoScrollingWindow(wxWindow* parent)
+ : wxScrolled<wxWindow>(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize,
+ wxVSCROLL | wxHSCROLL | wxSUNKEN_BORDER),
+ m_selStart(-1, -1),
+ m_cursor(-1, -1),
+ m_font(9, wxFONTFAMILY_TELETYPE, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL)
+{
+ wxClientDC dc(this);
+ // query dc for text size
+ dc.SetFont(m_font);
+ dc.GetTextExtent(wxString("A"), &m_fontW, &m_fontH);
+ // set up the virtual window
+ SetScrollbars(m_fontW, m_fontH, sm_lineLen, sm_lineCnt);
+}
+
+wxRect
+MyAutoScrollingWindow::DeviceCoordsToGraphicalChars(wxRect updRect) const
+{
+ wxPoint pos(updRect.GetPosition());
+ pos = DeviceCoordsToGraphicalChars(pos);
+ updRect.x = pos.x;
+ updRect.y = pos.y;
+ updRect.width /= m_fontW;
+ updRect.height /= m_fontH;
+ // the *CoordsToGraphicalChars() funcs round down to upper-left corner,
+ // so an off-by-one correction is needed
+ ++updRect.width; // kludge
+ ++updRect.height; // kludge
+ return updRect;
+}
+
+wxPoint
+MyAutoScrollingWindow::DeviceCoordsToGraphicalChars(wxPoint pos) const
+{
+ pos.x /= m_fontW;
+ pos.y /= m_fontH;
+ pos += GetViewStart();
+ return pos;
+}
+
+wxPoint
+MyAutoScrollingWindow::GraphicalCharToDeviceCoords(wxPoint pos) const
+{
+ pos -= GetViewStart();
+ pos.x *= m_fontW;
+ pos.y *= m_fontH;
+ return pos;
+}
+
+wxRect
+MyAutoScrollingWindow::LogicalCoordsToGraphicalChars(wxRect updRect) const
+{
+ wxPoint pos(updRect.GetPosition());
+ pos = LogicalCoordsToGraphicalChars(pos);
+ updRect.x = pos.x;
+ updRect.y = pos.y;
+ updRect.width /= m_fontW;
+ updRect.height /= m_fontH;
+ // the *CoordsToGraphicalChars() funcs round down to upper-left corner,
+ // so an off-by-one correction is needed
+ ++updRect.width; // kludge
+ ++updRect.height; // kludge
+ return updRect;
+}
+
+wxPoint
+MyAutoScrollingWindow::LogicalCoordsToGraphicalChars(wxPoint pos) const
+{
+ pos.x /= m_fontW;
+ pos.y /= m_fontH;
+ return pos;
+}
+
+wxPoint
+MyAutoScrollingWindow::GraphicalCharToLogicalCoords(wxPoint pos) const
+{
+ pos.x *= m_fontW;
+ pos.y *= m_fontH;
+ return pos;
+}
+
+void MyAutoScrollingWindow::MyRefresh()
+{
+ static wxPoint lastSelStart(-1, -1), lastCursor(-1, -1);
+ // refresh last selected area (to deselect previously selected text)
+ wxRect lastUpdRect(
+ GraphicalCharToDeviceCoords(lastSelStart),
+ GraphicalCharToDeviceCoords(lastCursor)
+ );
+ // off-by-one corrections, necessary because it's not possible to know
+ // when to round up until rect is normalized by lastUpdRect constructor
+ lastUpdRect.width += m_fontW; // kludge
+ lastUpdRect.height += m_fontH; // kludge
+ // refresh currently selected (to select previously unselected text)
+ wxRect updRect(
+ GraphicalCharToDeviceCoords(m_selStart),
+ GraphicalCharToDeviceCoords(m_cursor)
+ );
+ // off-by-one corrections
+ updRect.width += m_fontW; // kludge
+ updRect.height += m_fontH; // kludge
+ // find necessary refresh areas
+ int rx = lastUpdRect.x;
+ int ry = lastUpdRect.y;
+ int rw = updRect.x - lastUpdRect.x;
+ int rh = lastUpdRect.height;
+ if (rw && rh) {
+ RefreshRect(DCNormalize(rx, ry, rw, rh));
+ }
+ rx = updRect.x;
+ ry = updRect.y + updRect.height;
+ rw= updRect.width;
+ rh = (lastUpdRect.y + lastUpdRect.height) - (updRect.y + updRect.height);
+ if (rw && rh) {
+ RefreshRect(DCNormalize(rx, ry, rw, rh));
+ }
+ rx = updRect.x + updRect.width;
+ ry = lastUpdRect.y;
+ rw = (lastUpdRect.x + lastUpdRect.width) - (updRect.x + updRect.width);
+ rh = lastUpdRect.height;
+ if (rw && rh) {
+ RefreshRect(DCNormalize(rx, ry, rw, rh));
+ }
+ rx = updRect.x;
+ ry = lastUpdRect.y;
+ rw = updRect.width;
+ rh = updRect.y - lastUpdRect.y;
+ if (rw && rh) {
+ RefreshRect(DCNormalize(rx, ry, rw, rh));
+ }
+ // update last
+ lastSelStart = m_selStart;
+ lastCursor = m_cursor;
+}
+
+bool MyAutoScrollingWindow::IsSelected(int chX, int chY) const
+{
+ if (IsInside(chX, m_selStart.x, m_cursor.x)
+ && IsInside(chY, m_selStart.y, m_cursor.y)) {
+ return true;
+ }
+ return false;
+}
+
+bool MyAutoScrollingWindow::IsInside(int k, int bound1, int bound2)
+{
+ if ((k >= bound1 && k <= bound2) || (k >= bound2 && k <= bound1)) {
+ return true;
+ }
+ return false;
+}
+
+wxRect
+MyAutoScrollingWindow::DCNormalize(int x, int y, int w, int h)
+{
+ // this is needed to get rid of the graphical remnants from the selection
+ // I think it's because DrawRectangle() excludes a pixel in either direction
+ const int kludge = 1;
+ // make (x, y) the top-left corner
+ if (w < 0) {
+ w = -w + kludge;
+ x -= w;
+ } else {
+ x -= kludge;
+ w += kludge;
+ }
+ if (h < 0) {
+ h = -h + kludge;
+ y -= h;
+ } else {
+ y -= kludge;
+ h += kludge;
+ }
+ return wxRect(x, y, w, h);
+}
+
+void MyAutoScrollingWindow::OnDraw(wxDC& dc)
+{
+ dc.SetFont(m_font);
+ wxBrush normBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
+ wxBrush selBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT));
+ dc.SetPen(*wxTRANSPARENT_PEN);
+ const wxString str = sm_testData;
+ size_t strLength = str.length();
+ wxString::const_iterator str_i = str.begin();
+
+ // draw the characters
+ // 1. for each update region
+ for (wxRegionIterator upd(GetUpdateRegion()); upd; ++upd) {
+ wxRect updRect = upd.GetRect();
+ wxRect updRectInGChars(DeviceCoordsToGraphicalChars(updRect));
+ // 2. for each row of chars in the update region
+ for (int chY = updRectInGChars.y
+ ; chY <= updRectInGChars.y + updRectInGChars.height; ++chY) {
+ // 3. for each character in the row
+ bool isFirstX = true;
+ for (int chX = updRectInGChars.x
+ ; chX <= updRectInGChars.x + updRectInGChars.width
+ ; ++chX) {
+ // 4. set up dc
+ if (IsSelected(chX, chY)) {
+ dc.SetBrush(selBrush);
+ dc.SetTextForeground( wxSystemSettings::GetColour
+ (wxSYS_COLOUR_HIGHLIGHTTEXT));
+ } else {
+ dc.SetBrush(normBrush);
+ dc.SetTextForeground( wxSystemSettings::GetColour
+ (wxSYS_COLOUR_WINDOWTEXT));
+ }
+ // 5. find position info
+ wxPoint charPos = GraphicalCharToLogicalCoords(wxPoint
+ (chX, chY));
+ // 6. draw!
+ dc.DrawRectangle(charPos.x, charPos.y, m_fontW, m_fontH);
+ size_t charIndex = chY * sm_lineLen + chX;
+ if (chY < sm_lineCnt &&
+ chX < sm_lineLen &&
+ charIndex < strLength)
+ {
+ if (isFirstX)
+ {
+ str_i = str.begin() + charIndex;
+ isFirstX = false;
+ }
+ dc.DrawText(*str_i, charPos.x, charPos.y);
+ ++str_i;
+ }
+ }
+ }
+ }
+}
+
+void MyAutoScrollingWindow::OnMouseLeftDown(wxMouseEvent& event)
+{
+ // initial press of mouse button sets the beginning of the selection
+ m_selStart = DeviceCoordsToGraphicalChars(event.GetPosition());
+ // set the cursor to the same position
+ m_cursor = m_selStart;
+ // draw/erase selection
+ MyRefresh();
+}
+
+void MyAutoScrollingWindow::OnMouseLeftUp(wxMouseEvent& WXUNUSED(event))
+{
+ // this test is necessary
+ if (HasCapture()) {
+ // uncapture mouse
+ ReleaseMouse();
+ }
+}
+
+void MyAutoScrollingWindow::OnMouseMove(wxMouseEvent& event)
+{
+ // if user is dragging
+ if (event.Dragging() && event.LeftIsDown()) {
+ // set the new cursor position
+ m_cursor = DeviceCoordsToGraphicalChars(event.GetPosition());
+ // draw/erase selection
+ MyRefresh();
+ // capture mouse to activate auto-scrolling
+ if (!HasCapture()) {
+ CaptureMouse();
+ }
+ }
+}
+
+void
+MyAutoScrollingWindow::OnMouseCaptureLost(wxMouseCaptureLostEvent&
+ WXUNUSED(event))
+{
+ // we only capture mouse for timed scrolling, so nothing is needed here
+ // other than making sure to not call event.Skip()
+}
+
+void MyAutoScrollingWindow::OnScroll(wxScrollWinEvent& event)
+{
+ // need to move the cursor when autoscrolling
+ // FIXME: the cursor also moves when the scrollbar arrows are clicked
+ if (HasCapture()) {
+ if (event.GetOrientation() == wxHORIZONTAL) {
+ if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP) {
+ --m_cursor.x;
+ } else if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN) {
+ ++m_cursor.x;
+ }
+ } else if (event.GetOrientation() == wxVERTICAL) {
+ if (event.GetEventType() == wxEVT_SCROLLWIN_LINEUP) {
+ --m_cursor.y;
+ } else if (event.GetEventType() == wxEVT_SCROLLWIN_LINEDOWN) {
+ ++m_cursor.y;
+ }
+ }
+ }
+ MyRefresh();
+ event.Skip();