// waste time looking for it right now. Search for occurences of
// MSW_CORRECTION to find all the places where I did it.
#ifdef __WXMSW__
- static const int MSW_CORRECTION = 5;
+ static const int MSW_CORRECTION = 10;
#else
static const int MSW_CORRECTION = 0;
#endif
m_CursorPos = wxPoint(0,0);
m_CursorScreenPos = wxPoint(0,0);
m_CursorSize = wxPoint(0,0);
+ m_movedCursor = true;
m_FirstLine = new wxLayoutLine(NULL, this); // empty first line
m_CursorLine = m_FirstLine;
InvalidateUpdateRect();
{
AddCursorPosToUpdateRect();
+ wxPoint cursorPosOld = m_CursorPos;
+
wxLayoutLine *line = m_FirstLine;
while(line && line->GetLineNumber() != p.y)
line = line->GetNextLine();
if(len >= p.x)
{
m_CursorPos.x = p.x;
- return true;
}
else
{
m_CursorPos.x = len;
- return false;
}
}
- return false;
+
+ m_movedCursor = m_CursorPos != cursorPosOld;
+
+ return m_CursorPos == p;
}
bool
{
AddCursorPosToUpdateRect();
+ wxPoint cursorPosOld = m_CursorPos;
+
bool rc;
if(n < 0) // move up
{
rc = true;
}
}
+
+ m_movedCursor = m_CursorPos != cursorPosOld;
+
return rc;
}
{
AddCursorPosToUpdateRect();
+ wxPoint cursorPosOld = m_CursorPos;
+
int move;
while(n < 0)
{
m_CursorPos.x += move;
n -= move;
}
+
+ m_movedCursor = m_CursorPos != cursorPosOld;
+
return n == 0;
}
m_CursorPos.x += text.Length();
+ m_movedCursor = true;
+
m_CursorLine->RecalculatePositions(0, this);
return true;
m_CursorLine->Insert(m_CursorPos.x, obj);
m_CursorPos.x += obj->GetLength();
+ m_movedCursor = true;
m_CursorLine->RecalculatePositions(0, this);
if(m_CursorPos.x != 0)
m_CursorPos.y++;
m_CursorPos.x = 0;
-// doesn't help m_CursorLine.MarkDirty();
wxLayoutLine *prev = m_CursorLine->GetPreviousLine();
wxCHECK_MSG(prev, false, "just broke the line, where is the previous one?");
height += prev->GetHeight();
+ m_movedCursor = true;
+
SetUpdateRect(position);
SetUpdateRect(position.x + width + MSW_CORRECTION,
position.y + height + MSW_CORRECTION);
m_CursorLine->RecalculatePositions(1, this);
+ m_movedCursor = true;
+
return true;
}
}
wxLayoutList::Delete(CoordType npos)
{
wxCHECK_MSG(m_CursorLine, false, "can't delete in non existing line");
- wxASSERT_MSG(npos > 0, "nothing to delete?");
+
+ if ( npos == 0 )
+ return true;
AddCursorPosToUpdateRect();
}
void
-wxLayoutList::UpdateCursorScreenPos(wxDC &dc)
+wxLayoutList::UpdateCursorScreenPos(wxDC &dc,
+ bool resetCursorMovedFlag,
+ const wxPoint& translate)
{
- wxASSERT(m_CursorLine);
- m_CursorLine->Layout(dc, this, (wxPoint *)&m_CursorScreenPos, (wxPoint *)&m_CursorSize, m_CursorPos.x);
+ wxCHECK_RET( m_CursorLine, "no cursor line" );
+
+ if ( m_movedCursor )
+ {
+ m_CursorLine->Layout(dc, this,
+ &m_CursorScreenPos, &m_CursorSize,
+ m_CursorPos.x);
+
+ if ( resetCursorMovedFlag )
+ {
+ #ifdef WXLAYOUT_USE_CARET
+ // adjust the caret position
+ wxPoint coords(m_CursorScreenPos);
+ coords += translate;
+
+ // and set it
+ m_caret->Move(coords);
+ #endif // WXLAYOUT_USE_CARET
+
+ m_movedCursor = false;
+ }
+ }
}
wxPoint
wxLayoutList::GetCursorScreenPos(wxDC &dc)
{
- UpdateCursorScreenPos(dc);
+ // this function is called with wxMemoryDC argument from ScrollToCursor(),
+ // for example, so it shouldn't clear "cursor moved" flag - or else the
+ // cursor won't be moved when UpdateCursorScreenPos() is called with the
+ // "real" (i.e. the one used for drawing) wxDC.
+ UpdateCursorScreenPos(dc, false /* don't reset the flag */);
+
return m_CursorScreenPos;
}
}
maxPoint.y = last->GetPosition().y + last->GetHeight();
+
+ // if the line was just added, its height would be 0 and we can't call
+ // Layout() from here because we don't have a dc and we might be not drawing
+ // at all, besides... So take the cursor height by default (taking 0 is bad
+ // because then the scrollbars won't be resized and the new line won't be
+ // shown at all)
+ if ( last->IsDirty() )
+ {
+ if ( last->GetHeight() == 0 )
+ maxPoint.y += m_CursorSize.y;
+ if ( last->GetWidth() == 0 && maxPoint.x < m_CursorSize.x )
+ maxPoint.x = m_CursorSize.x;
+ }
+
return maxPoint;
}
wxLogStatus("Cursor is at (%d, %d)", m_CursorPos.x, m_CursorPos.y);
#endif
-#ifdef WXLAYOUT_USE_CARET
- m_caret->Move(coords);
-#else // !WXLAYOUT_USE_CARET
+#ifndef WXLAYOUT_USE_CARET
dc.SetBrush(*wxBLACK_BRUSH);
dc.SetLogicalFunction(wxXOR);
dc.SetPen(wxPen(*wxBLACK,1,wxSOLID));
# define WXLO_BITMAP_FORMAT wxBITMAP_TYPE_PNG
#endif
-
/// Types of currently supported layout objects.
enum wxLayoutObjectType
{
@return cursor position in pixels
*/
wxPoint GetCursorScreenPos(wxDC &dc);
+ /** Calculates the cursor position on the screen.
+ @param dc the dc to use for cursor position calculations
+ @param resetCursorMovedFlag: if true, reset "cursor moved" flag
+ @param translate optional translation of cursor coords on screen
+ */
+ void UpdateCursorScreenPos(wxDC &dc,
+ bool resetCursorMovedFlag = true,
+ const wxPoint& translate = wxPoint(0, 0));
/** Draws the cursor.
@param active If true, draw a bold cursor to mark window as
/** Called by the objects to update the update rectangle.
@param p a point to include in it
*/
- inline void SetUpdateRect(const wxPoint &p)
+ void SetUpdateRect(const wxPoint &p)
{ SetUpdateRect(p.x,p.y); }
/// adds the cursor position to the update rectangle
void AddCursorPosToUpdateRect()
const wxRect *GetUpdateRect(void) const { return &m_UpdateRect; }
//@}
+ /// get the current cursor size
+ const wxPoint& GetCursorSize() const { return m_CursorSize; }
+
/**@name For exporting one object after another. */
//@{
/** Returns a pointer to the first line in the list. */
private:
/// Clear the list.
void InternalClear(void);
- /** Calculates the cursor position on the screen.
- */
- void UpdateCursorScreenPos(wxDC &dc);
/// The list of lines.
wxLayoutLine *m_FirstLine;
wxLayoutLine *m_CursorLine;
/// The size of the cursor.
wxPoint m_CursorSize;
+ /// Has the cursor moved (is m_CursorScreenPos up to date)?
+ bool m_movedCursor;
#ifdef WXLAYOUT_USE_CARET
/// the caret
wxCaret *m_caret;
* $Id$
*******************************************************************/
+// ===========================================================================
+// declarations
+// ===========================================================================
+
+// ----------------------------------------------------------------------------
+// headers
+// ----------------------------------------------------------------------------
+
#ifdef __GNUG__
# pragma implementation "wxlwindow.h"
#endif
#include <ctype.h>
+// ----------------------------------------------------------------------------
+// macros
+// ----------------------------------------------------------------------------
+
#ifdef WXLAYOUT_DEBUG
# define WXLO_DEBUG(x) wxLogDebug x
#else
# define WXLO_DEBUG(x)
#endif
+// ----------------------------------------------------------------------------
+// constants
+// ----------------------------------------------------------------------------
+
/// offsets to put a nice frame around text
#define WXLO_XOFFSET 4
#define WXLO_YOFFSET 4
#define WXLO_ROFFSET 20
#define WXLO_BOFFSET 20
+/// the size of one scrollbar page in pixels
+static const int X_SCROLL_PAGE = 10;
+static const int Y_SCROLL_PAGE = 20;
+
+// ----------------------------------------------------------------------------
+// event tables
+// ----------------------------------------------------------------------------
+
BEGIN_EVENT_TABLE(wxLayoutWindow,wxScrolledWindow)
EVT_PAINT (wxLayoutWindow::OnPaint)
EVT_CHAR (wxLayoutWindow::OnChar)
EVT_KILL_FOCUS(wxLayoutWindow::OnKillFocus)
END_EVENT_TABLE()
+// ===========================================================================
+// implementation
+// ===========================================================================
+
+// ----------------------------------------------------------------------------
+// wxLayoutWindow
+// ----------------------------------------------------------------------------
+
wxLayoutWindow::wxLayoutWindow(wxWindow *parent)
: wxScrolledWindow(parent, -1,
wxDefaultPosition, wxDefaultSize,
m_ScrollToCursor = false;
SetWrapMargin(0);
wxPoint max = m_llist->GetSize();
- SetScrollbars(10, 20 /*lineHeight*/, max.x/10+1, max.y/20+1);
- EnableScrolling(true,true);
- m_maxx = max.x; m_maxy = max.y;
+ SetScrollbars(X_SCROLL_PAGE, Y_SCROLL_PAGE,
+ max.x / X_SCROLL_PAGE + 1, max.y / Y_SCROLL_PAGE + 1);
+ EnableScrolling(true, true);
+ m_maxx = max.x + X_SCROLL_PAGE;
+ m_maxy = max.y + Y_SCROLL_PAGE;
m_Selecting = false;
#ifdef WXLAYOUT_USE_CARET
}
// always move cursor to mouse click:
- if(obj && eventId == WXLOWIN_MENU_LCLICK)
+ if(eventId == WXLOWIN_MENU_LCLICK)
{
m_llist->MoveCursorTo(cursorPos);
+
+ // Calculate where the top of the visible area is:
+ int x0, y0;
+ ViewStart(&x0,&y0);
+ int dx, dy;
+ GetScrollPixelsPerUnit(&dx, &dy);
+ x0 *= dx; y0 *= dy;
+
+ wxPoint offset(-x0+WXLO_XOFFSET, -y0+WXLO_YOFFSET);
+ m_llist->UpdateCursorScreenPos(dc, true, offset);
+
if(m_CursorVisibility == -1)
m_CursorVisibility = 1;
+
+ // VZ: this should be unnecessary because mouse can only click on a
+ // visible part of the canvas
+#if 0
ScrollToCursor();
+#endif // 0
+
+#ifdef __WXGTK__
DoPaint(FALSE); // DoPaint suppresses flicker under GTK
+#endif // wxGTK
}
if(!m_doSendEvents) // nothing to do
m_llist->MoveCursorVertically(1);
break;
case WXK_PRIOR:
- m_llist->MoveCursorVertically(-20);
+ m_llist->MoveCursorVertically(-Y_SCROLL_PAGE);
break;
case WXK_NEXT:
- m_llist->MoveCursorVertically(20);
+ m_llist->MoveCursorVertically(Y_SCROLL_PAGE);
break;
case WXK_HOME:
if ( ctrlDown )
break;
case WXK_END:
if ( ctrlDown )
- {
- // TODO VZ: how to move the cursor to the last line of the text?
- m_llist->MoveCursorVertically(1000);
- }
- m_llist->MoveCursorToEndOfLine();
+ m_llist->MoveCursorTo(m_llist->GetSize());
+ else
+ m_llist->MoveCursorToEndOfLine();
break;
+
default:
if(keyCode == 'c' && ctrlDown)
{
&& (keyCode < 256 && keyCode >= 32)
)
{
- wxString tmp;
- tmp += keyCode;
if(m_WrapMargin > 0 && isspace(keyCode))
m_llist->WrapLine(m_WrapMargin);
- m_llist->Insert(tmp);
+ m_llist->Insert((char)keyCode);
}
break;
}
}
}
+ // we must call ResizeScrollbars() before ScrollToCursor(), otherwise the
+ // ne cursor position might be outside the current scrolllbar range
+ ResizeScrollbars();
ScrollToCursor();
- wxRect r = *m_llist->GetUpdateRect();
- DoPaint(&r);
+
+ // refresh the screen
+ DoPaint(m_llist->GetUpdateRect());
}
void
// Get the size of the visible window:
GetClientSize(&x1,&y1);
- wxASSERT(x1 > 0);
- wxASSERT(y1 > 0);
- // As we have the values anyway, use them to avoid unnecessary
- // scrollbar updates.
+
+ // notice that the client size may be (0, 0)...
+ wxASSERT(x1 >= 0 && y1 >= 0);
+
+ // VZ: I think this is false - if you do it here, ResizeScrollbars() won't
+ // call SetScrollbars() later
+#if 0
+ // As we have the values anyway, use them to avoid unnecessary scrollbar
+ // updates.
if(x1 > m_maxx) m_maxx = x1;
if(y1 > m_maxy) m_maxy = y1;
- /* Make sure that the scrollbars are at a position so that the
- cursor is visible if we are editing. */
- /** Scroll so that cursor is visible! */
+#endif // 0
+
+ // Make sure that the scrollbars are at a position so that the cursor is
+ // visible if we are editing
WXLO_DEBUG(("m_ScrollToCursor = %d", (int) m_ScrollToCursor));
wxPoint cc = m_llist->GetCursorScreenPos(*m_memDC);
- if(cc.x < x0 || cc.y < y0
- || cc.x >= x0+(9*x1)/10 || cc.y >= y0+(9*y1/10)) // (9*x)/10 == 90%
+
+ // the cursor should be completely visible in both directions
+ wxPoint cs(m_llist->GetCursorSize());
+ int nx = -1,
+ ny = -1;
+ if ( cc.x < x0 || cc.x >= x0 + x1 - cs.x )
+ {
+ nx = cc.x - x1/2;
+ if ( nx < 0 )
+ nx = 0;
+ }
+
+ if ( cc.y < y0 || cc.y >= y0 + y1 - cs.y )
{
- int nx, ny;
- nx = cc.x - x1/2; if(nx < 0) nx = 0;
- ny = cc.y - y1/2; if(ny < 0) ny = 0;
- Scroll(nx/dx,ny/dy); // new view start
- x0 = nx; y0 = ny;
- m_ScrollToCursor = false; // avoid recursion
+ ny = cc.y - y1/2;
+ if ( ny < 0)
+ ny = 0;
+ }
+
+ if ( nx != -1 || ny != -1 )
+ {
+ // set new view start
+ Scroll(nx == -1 ? -1 : (nx+dx-1)/dx, ny == -1 ? -1 : (ny+dy-1)/dy);
+
+ // avoid recursion
+ m_ScrollToCursor = false;
}
}
void
-wxLayoutWindow::OnPaint( wxPaintEvent &WXUNUSED(event)) // or: OnDraw(wxDC& dc)
+wxLayoutWindow::OnPaint( wxPaintEvent &WXUNUSED(event))
{
wxRect region = GetUpdateRegion().GetBox();
- InternalPaint(& region);
+ InternalPaint(®ion);
}
void
// Get the size of the visible window:
GetClientSize(&x1,&y1);
- wxASSERT(x1 > 0);
- wxASSERT(y1 > 0);
- // As we have the values anyway, use them to avoid unnecessary
- // scrollbar updates.
- if(x1 > m_maxx) m_maxx = x1;
- if(y1 > m_maxy) m_maxy = y1;
-
+ wxASSERT(x1 >= 0);
+ wxASSERT(y1 >= 0);
if(updateRect)
{
// needed to erase it):
m_llist->InvalidateUpdateRect();
if(m_CursorVisibility != 0)
+ {
+ m_llist->UpdateCursorScreenPos(dc, true, offset);
m_llist->DrawCursor(*m_memDC,
m_HaveFocus && IsEditable(), // draw a thick
// cursor for editable windows with focus
offset);
+ }
// Now copy everything to the screen:
#if 0
ResetDirty();
m_ScrollToCursor = false;
- if(m_StatusBar && m_StatusFieldCursor != -1)
+
+ if ( m_StatusBar && m_StatusFieldCursor != -1 )
{
- wxString label;
- label.Printf(_("Ln:%d Col:%d"),
- m_llist->GetCursorPos().y+1,
- m_llist->GetCursorPos().x+1);
- m_StatusBar->SetStatusText(label, m_StatusFieldCursor);
+ static wxPoint s_oldCursorPos(-1, -1);
+
+ wxPoint pos(m_llist->GetCursorPos());
+
+ // avoid unnecessary status bar refreshes
+ if ( pos != s_oldCursorPos )
+ {
+ s_oldCursorPos = pos;
+
+ wxString label;
+ label.Printf(_("Ln:%d Col:%d"), pos.y + 1, pos.x + 1);
+ m_StatusBar->SetStatusText(label, m_StatusFieldCursor);
+ }
}
}
{
wxPoint max = m_llist->GetSize();
- WXLO_DEBUG(("ResizeScrollbars: GetSize: %ld, %ld", (long int)max.x,
- (long int) max.y));
- if(max.x > m_maxx || max.y > m_maxy
- || max.x > m_maxx-WXLO_ROFFSET || max.y > m_maxy-WXLO_BOFFSET
- || exact)
+ WXLO_DEBUG(("ResizeScrollbars: max size = (%ld, %ld)",
+ (long int)max.x, (long int) max.y));
+
+ if( max.x > m_maxx - WXLO_ROFFSET || max.y > m_maxy - WXLO_BOFFSET || exact )
{
- if(! exact)
+ if ( !exact )
{
// add an extra bit to the sizes to avoid future updates
- max.x = max.x+WXLO_ROFFSET;
- max.y = max.y+WXLO_BOFFSET;
+ max.x += WXLO_ROFFSET;
+ max.y += WXLO_BOFFSET;
}
+
ViewStart(&m_ViewStartX, &m_ViewStartY);
- SetScrollbars(10, 20, max.x/10+1,max.y/20+1,m_ViewStartX,m_ViewStartY,true);
- m_maxx = max.x; m_maxy = max.y;
+ SetScrollbars(X_SCROLL_PAGE, Y_SCROLL_PAGE,
+ max.x / X_SCROLL_PAGE + 1, max.y / Y_SCROLL_PAGE + 1,
+ m_ViewStartX, m_ViewStartY,
+ true);
+
+ m_maxx = max.x + X_SCROLL_PAGE;
+ m_maxy = max.y + Y_SCROLL_PAGE;
}
}