+void MyScrolledWindowDumb::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);
+
+ wxCoord y = 0;
+ for ( size_t line = 0; line < m_nLines; line++ )
+ {
+ wxCoord yPhys;
+ CalcScrolledPosition(0, y, NULL, &yPhys);
+
+ dc.DrawText(wxString::Format(_T("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;
+
+ wxCoord y = lineFrom*m_hLine;
+ for ( size_t line = lineFrom; line <= lineTo; line++ )
+ {
+ wxCoord yPhys;
+ CalcScrolledPosition(0, y, NULL, &yPhys);
+
+ dc.DrawText(wxString::Format(_T("Line %u (logical %d, physical %d)"),
+ unsigned(line), y, yPhys), 0, y);
+ y += m_hLine;
+ }
+}
+
+// ----------------------------------------------------------------------------
+// MyAutoTimedScrollingWindow
+// ----------------------------------------------------------------------------
+
+BEGIN_EVENT_TABLE(MyAutoTimedScrollingWindow, wxScrolledWindow)
+ EVT_LEFT_DOWN(MyAutoTimedScrollingWindow::OnMouseLeftDown)
+ EVT_LEFT_UP(MyAutoTimedScrollingWindow::OnMouseLeftUp)
+ EVT_MOTION(MyAutoTimedScrollingWindow::OnMouseMove)
+ EVT_MOUSE_CAPTURE_LOST(MyAutoTimedScrollingWindow::OnMouseCaptureLost)
+ EVT_SCROLLWIN(MyAutoTimedScrollingWindow::OnScroll)
+END_EVENT_TABLE()
+
+MyAutoTimedScrollingWindow::MyAutoTimedScrollingWindow(wxWindow* parent)
+ : wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize
+ //, wxSUNKEN_BORDER) // can't seem to do it this way
+ , 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(_T("A")), &m_fontW, &m_fontH);
+ // set up the virtual window
+ SetScrollbars(m_fontW, m_fontH, sm_lineLen, sm_lineCnt);
+}
+
+wxRect MyAutoTimedScrollingWindow::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 MyAutoTimedScrollingWindow::DeviceCoordsToGraphicalChars
+ (wxPoint pos) const
+{
+ pos.x /= m_fontW;
+ pos.y /= m_fontH;
+ int vX, vY;
+ GetViewStart(&vX, &vY);
+ pos.x += vX;
+ pos.y += vY;
+ return pos;
+}
+
+wxPoint MyAutoTimedScrollingWindow::GraphicalCharToDeviceCoords
+ (wxPoint pos) const
+{
+ int vX, vY;
+ GetViewStart(&vX, &vY);
+ pos.x -= vX;
+ pos.y -= vY;
+ pos.x *= m_fontW;
+ pos.y *= m_fontH;
+ return pos;
+}
+
+wxRect MyAutoTimedScrollingWindow::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 MyAutoTimedScrollingWindow::LogicalCoordsToGraphicalChars
+ (wxPoint pos) const
+{
+ pos.x /= m_fontW;
+ pos.y /= m_fontH;
+ return pos;
+}
+
+wxPoint MyAutoTimedScrollingWindow::GraphicalCharToLogicalCoords
+ (wxPoint pos) const
+{
+ pos.x *= m_fontW;
+ pos.y *= m_fontH;
+ return pos;
+}
+
+void MyAutoTimedScrollingWindow::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
+ wxCoord rx = lastUpdRect.x;
+ wxCoord ry = lastUpdRect.y;
+ wxCoord rw = updRect.x - lastUpdRect.x;
+ wxCoord 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 MyAutoTimedScrollingWindow::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 MyAutoTimedScrollingWindow::IsInside(int k, int bound1, int bound2)
+{
+ if ((k >= bound1 && k <= bound2) || (k >= bound2 && k <= bound1)) {
+ return true;
+ }
+ return false;
+}
+
+wxRect MyAutoTimedScrollingWindow::DCNormalize(wxCoord x, wxCoord y
+ , wxCoord w, wxCoord 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 MyAutoTimedScrollingWindow::OnDraw(wxDC& dc)
+{
+ dc.SetFont(m_font);
+ wxBrush normBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)
+ , wxSOLID);
+ wxBrush selBrush(wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT)
+ , wxSOLID);
+ dc.SetPen(*wxTRANSPARENT_PEN);
+ const wxString str = sm_testData;
+ size_t strLength = str.length();
+ wxString::const_iterator str_i;
+
+ // 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 MyAutoTimedScrollingWindow::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 MyAutoTimedScrollingWindow::OnMouseLeftUp(wxMouseEvent& WXUNUSED(event))
+{
+ // this test is necessary
+ if (HasCapture()) {
+ // uncapture mouse
+ ReleaseMouse();
+ }
+}
+
+void MyAutoTimedScrollingWindow::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 MyAutoTimedScrollingWindow::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 MyAutoTimedScrollingWindow::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();