X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c59f6793fb6c116e6b9abdaca4de0c4a08a0e5b0..cc57f388d769cba09debd611d1e5e5f6d28aee41:/src/richtext/richtextctrl.cpp?ds=sidebyside diff --git a/src/richtext/richtextctrl.cpp b/src/richtext/richtextctrl.cpp index 732a30dd8e..b414fa7bb5 100644 --- a/src/richtext/richtextctrl.cpp +++ b/src/richtext/richtextctrl.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: richtext/richeditctrl.cpp +// Name: src/richtext/richeditctrl.cpp // Purpose: A rich edit control // Author: Julian Smart // Modified by: @@ -13,24 +13,22 @@ #include "wx/wxprec.h" #ifdef __BORLANDC__ - #pragma hdrstop + #pragma hdrstop #endif -#ifndef WX_PRECOMP - #include "wx/wx.h" -#endif +#if wxUSE_RICHTEXT -#include "wx/image.h" +#include "wx/richtext/richtextctrl.h" -#if wxUSE_RICHTEXT +#ifndef WX_PRECOMP + #include "wx/wx.h" + #include "wx/settings.h" +#endif #include "wx/textfile.h" #include "wx/ffile.h" -#include "wx/settings.h" #include "wx/filename.h" #include "wx/dcbuffer.h" - -#include "wx/richtext/richtextctrl.h" #include "wx/arrimpl.cpp" DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED) @@ -187,14 +185,14 @@ void wxRichTextCtrl::Freeze() } /// Call Thaw to refresh -void wxRichTextCtrl::Thaw(bool refresh) +void wxRichTextCtrl::Thaw() { m_freezeCount --; - if (m_freezeCount == 0 && refresh) + if (m_freezeCount == 0) { SetupScrollbars(); - Refresh(); + Refresh(false); } } @@ -210,7 +208,7 @@ void wxRichTextCtrl::Clear() if (m_freezeCount == 0) { SetupScrollbars(); - Refresh(); + Refresh(false); } SendUpdateEvent(); } @@ -218,22 +216,25 @@ void wxRichTextCtrl::Clear() /// Painting void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event)) { + if (GetCaret()) + GetCaret()->Hide(); + { wxBufferedPaintDC dc(this, m_bufferBitmap); //wxLogDebug(wxT("OnPaint")); - + PrepareDC(dc); - + if (m_freezeCount > 0) return; - + dc.SetFont(GetFont()); - + // Paint the background PaintBackground(dc); - + wxRegion dirtyRegion = GetUpdateRegion(); - + wxRect drawingArea(GetLogicalPoint(wxPoint(0, 0)), GetClientSize()); wxRect availableSpace(GetClientSize()); if (GetBuffer().GetDirty()) @@ -242,9 +243,13 @@ void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event)) GetBuffer().SetDirty(false); SetupScrollbars(); } - + GetBuffer().Draw(dc, GetBuffer().GetRange(), GetSelectionRange(), drawingArea, 0 /* descent */, 0 /* flags */); } + + if (GetCaret()) + GetCaret()->Show(); + PositionCaret(); } @@ -261,7 +266,7 @@ void wxRichTextCtrl::OnSetFocus(wxFocusEvent& WXUNUSED(event)) PositionCaret(); if (!IsFrozen()) - Refresh(); + Refresh(false); } void wxRichTextCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event)) @@ -269,7 +274,7 @@ void wxRichTextCtrl::OnKillFocus(wxFocusEvent& WXUNUSED(event)) SetCaret(NULL); if (!IsFrozen()) - Refresh(); + Refresh(false); } /// Left-click @@ -382,7 +387,7 @@ void wxRichTextCtrl::OnMoveMouse(wxMouseEvent& event) SetDefaultStyleToCursorStyle(); if (extendSel) - Refresh(); + Refresh(false); } } } @@ -424,13 +429,21 @@ void wxRichTextCtrl::OnChar(wxKeyEvent& event) event.GetKeyCode() == WXK_HOME || event.GetKeyCode() == WXK_PAGEUP || event.GetKeyCode() == WXK_PAGEDOWN || - event.GetKeyCode() == WXK_PRIOR || - event.GetKeyCode() == WXK_NEXT || event.GetKeyCode() == WXK_END) { - Navigate(event.GetKeyCode(), flags); + KeyboardNavigate(event.GetKeyCode(), flags); + return; } - else if (event.GetKeyCode() == WXK_RETURN) + + // all the other keys modify the controls contents which shouldn't be + // possible if we're read-only + if ( !IsEditable() ) + { + event.Skip(); + return; + } + + if (event.GetKeyCode() == WXK_RETURN) { BeginBatchUndo(_("Insert Text")); @@ -449,6 +462,8 @@ void wxRichTextCtrl::OnChar(wxKeyEvent& event) EndBatchUndo(); SetDefaultStyleToCursorStyle(); + + ScrollIntoView(m_caretPosition, WXK_RIGHT); } else if (event.GetKeyCode() == WXK_BACK) { @@ -478,6 +493,7 @@ void wxRichTextCtrl::OnChar(wxKeyEvent& event) SetDefaultStyleToCursorStyle(); } + ScrollIntoView(m_caretPosition, WXK_LEFT); } else if (event.GetKeyCode() == WXK_DELETE) { @@ -519,11 +535,8 @@ void wxRichTextCtrl::OnChar(wxKeyEvent& event) EndBatchUndo(); SetDefaultStyleToCursorStyle(); + ScrollIntoView(m_caretPosition, WXK_RIGHT); } -#if 0 - else - event.Skip(); -#endif } /// Delete content if there is a selection, e.g. when pressing a key. @@ -577,10 +590,9 @@ Adding Shift does the above but starts/extends selection. */ -bool wxRichTextCtrl::Navigate(int keyCode, int flags) +bool wxRichTextCtrl::KeyboardNavigate(int keyCode, int flags) { bool success = false; - Freeze(); if (keyCode == WXK_RIGHT) { @@ -610,11 +622,11 @@ bool wxRichTextCtrl::Navigate(int keyCode, int flags) else success = MoveDown(1, flags); } - else if (keyCode == WXK_PAGEUP || keyCode == WXK_PRIOR) + else if (keyCode == WXK_PAGEUP) { success = PageUp(1, flags); } - else if (keyCode == WXK_PAGEDOWN || keyCode == WXK_NEXT) + else if (keyCode == WXK_PAGEDOWN) { success = PageDown(1, flags); } @@ -639,8 +651,6 @@ bool wxRichTextCtrl::Navigate(int keyCode, int flags) SetDefaultStyleToCursorStyle(); } - Thaw(false); - return success; } @@ -697,7 +707,7 @@ bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode) startX = 0; startY = startY * ppuY; - int sx, sy; + int sx = 0, sy = 0; GetVirtualSize(& sx, & sy); sx = 0; if (ppuY != 0) @@ -710,7 +720,7 @@ bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode) wxSize clientSize = GetClientSize(); // Going down - if (keyCode == WXK_DOWN || keyCode == WXK_RIGHT || keyCode == WXK_END || keyCode == WXK_NEXT || keyCode == WXK_PAGEDOWN) + if (keyCode == WXK_DOWN || keyCode == WXK_RIGHT || keyCode == WXK_END || keyCode == WXK_PAGEDOWN) { if ((rect.y + rect.height) > (clientSize.y + startY)) { @@ -740,7 +750,7 @@ bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode) } } // Going up - else if (keyCode == WXK_UP || keyCode == WXK_LEFT || keyCode == WXK_HOME || keyCode == WXK_PRIOR || keyCode == WXK_PAGEUP) + else if (keyCode == WXK_UP || keyCode == WXK_LEFT || keyCode == WXK_HOME || keyCode == WXK_PAGEUP ) { if (rect.y < startY) { @@ -790,7 +800,7 @@ bool wxRichTextCtrl::IsPositionVisible(long pos) const startX = 0; startY = startY * ppuY; - int sx, sy; + int sx = 0, sy = 0; GetVirtualSize(& sx, & sy); sx = 0; if (ppuY != 0) @@ -932,7 +942,7 @@ bool wxRichTextCtrl::MoveRight(int noPositions, int flags) SetDefaultStyleToCursorStyle(); if (extendSel) - Refresh(); + Refresh(false); return true; } else @@ -961,7 +971,7 @@ bool wxRichTextCtrl::MoveLeft(int noPositions, int flags) SetDefaultStyleToCursorStyle(); if (extendSel) - Refresh(); + Refresh(false); return true; } else @@ -1052,11 +1062,11 @@ bool wxRichTextCtrl::MoveDown(int noLines, int flags) SetDefaultStyleToCursorStyle(); if (extendSel) - Refresh(); + Refresh(false); return true; } - else - return false; + + return false; } /// Move to the end of the paragraph @@ -1075,7 +1085,7 @@ bool wxRichTextCtrl::MoveToParagraphEnd(int flags) SetDefaultStyleToCursorStyle(); if (extendSel) - Refresh(); + Refresh(false); return true; } @@ -1098,7 +1108,7 @@ bool wxRichTextCtrl::MoveToParagraphStart(int flags) SetDefaultStyleToCursorStyle(); if (extendSel) - Refresh(); + Refresh(false); return true; } @@ -1123,7 +1133,7 @@ bool wxRichTextCtrl::MoveToLineEnd(int flags) SetDefaultStyleToCursorStyle(); if (extendSel) - Refresh(); + Refresh(false); return true; } @@ -1150,7 +1160,7 @@ bool wxRichTextCtrl::MoveToLineStart(int flags) SetDefaultStyleToCursorStyle(); if (extendSel) - Refresh(); + Refresh(false); return true; } @@ -1171,7 +1181,7 @@ bool wxRichTextCtrl::MoveHome(int flags) SetDefaultStyleToCursorStyle(); if (extendSel) - Refresh(); + Refresh(false); return true; } else @@ -1194,7 +1204,7 @@ bool wxRichTextCtrl::MoveEnd(int flags) SetDefaultStyleToCursorStyle(); if (extendSel) - Refresh(); + Refresh(false); return true; } else @@ -1235,7 +1245,7 @@ bool wxRichTextCtrl::PageDown(int noPages, int flags) SetDefaultStyleToCursorStyle(); if (extendSel) - Refresh(); + Refresh(false); return true; } } @@ -1334,7 +1344,7 @@ bool wxRichTextCtrl::WordLeft(int WXUNUSED(n), int flags) SetDefaultStyleToCursorStyle(); if (extendSel) - Refresh(); + Refresh(false); return true; } @@ -1358,7 +1368,7 @@ bool wxRichTextCtrl::WordRight(int WXUNUSED(n), int flags) SetDefaultStyleToCursorStyle(); if (extendSel) - Refresh(); + Refresh(false); return true; } @@ -1374,7 +1384,7 @@ void wxRichTextCtrl::OnSize(wxSizeEvent& event) m_fullLayoutRequired = true; m_fullLayoutTime = wxGetLocalTimeMillis(); m_fullLayoutSavedPosition = GetFirstVisiblePosition(); - Layout(true /* onlyVisibleRect */); + LayoutContent(true /* onlyVisibleRect */); } else GetBuffer().Invalidate(wxRICHTEXT_ALL); @@ -1396,7 +1406,7 @@ void wxRichTextCtrl::OnIdle(wxIdleEvent& event) m_fullLayoutTime = 0; GetBuffer().Invalidate(wxRICHTEXT_ALL); ShowPosition(m_fullLayoutSavedPosition); - Refresh(); + Refresh(false); } event.Skip(); } @@ -1490,10 +1500,10 @@ bool wxRichTextCtrl::LoadFile(const wxString& filename, int type) DiscardEdits(); SetInsertionPoint(0); - Layout(); + LayoutContent(); PositionCaret(); SetupScrollbars(true); - Refresh(); + Refresh(false); SendUpdateEvent(); if (success) @@ -1575,17 +1585,20 @@ wxString wxRichTextCtrl::GetStringSelection() const } // do the window-specific processing after processing the update event +#if !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE void wxRichTextCtrl::DoUpdateWindowUI(wxUpdateUIEvent& event) { - if ( event.GetSetEnabled() ) - Enable(event.GetEnabled()); + // call inherited + wxWindowBase::DoUpdateWindowUI(event); + // update text if ( event.GetSetText() ) { if ( event.GetText() != GetValue() ) SetValue(event.GetText()); } } +#endif // !wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE // ---------------------------------------------------------------------------- // hit testing @@ -1616,14 +1629,20 @@ wxRichTextCtrl::HitTest(const wxPoint& pt, ((wxRichTextCtrl*)this)->PrepareDC(dc); int hit = ((wxRichTextCtrl*)this)->GetBuffer().HitTest(dc, pt, *pos); - if (hit == wxRICHTEXT_HITTEST_BEFORE) - return wxTE_HT_BEFORE; - else if (hit == wxRICHTEXT_HITTEST_AFTER) - return wxTE_HT_BEYOND; - else if (hit == wxRICHTEXT_HITTEST_ON) - return wxTE_HT_ON_TEXT; - else - return wxTE_HT_UNKNOWN; + + switch ( hit ) + { + case wxRICHTEXT_HITTEST_BEFORE: + return wxTE_HT_BEFORE; + + case wxRICHTEXT_HITTEST_AFTER: + return wxTE_HT_BEYOND; + + case wxRICHTEXT_HITTEST_ON: + return wxTE_HT_ON_TEXT; + } + + return wxTE_HT_UNKNOWN; } // ---------------------------------------------------------------------------- @@ -1693,8 +1712,8 @@ bool wxRichTextCtrl::WriteImage(const wxImage& image, int bitmapType) wxImage image2 = image; if (imageBlock.MakeImageBlock(image2, bitmapType)) return WriteImage(imageBlock); - else - return false; + + return false; } bool wxRichTextCtrl::WriteImage(const wxString& filename, int bitmapType) @@ -1704,8 +1723,8 @@ bool wxRichTextCtrl::WriteImage(const wxString& filename, int bitmapType) wxImage image; if (imageBlock.MakeImageBlock(filename, bitmapType, image, false)) return WriteImage(imageBlock); - else - return false; + + return false; } bool wxRichTextCtrl::WriteImage(const wxRichTextImageBlock& imageBlock) @@ -1722,9 +1741,8 @@ bool wxRichTextCtrl::WriteImage(const wxBitmap& bitmap, int bitmapType) wxImage image = bitmap.ConvertToImage(); if (image.Ok() && imageBlock.MakeImageBlock(image, bitmapType)) return WriteImage(imageBlock); - else - return false; } + return false; } @@ -1756,8 +1774,8 @@ void wxRichTextCtrl::Cut() GetBuffer().CopyToClipboard(range); DeleteSelectedContent(); - Layout(); - Refresh(); + LayoutContent(); + Refresh(false); } } @@ -1880,7 +1898,7 @@ void wxRichTextCtrl::DoSetSelection(long from, long to, bool WXUNUSED(scrollCare { m_selectionAnchor = from; m_selectionRange.SetRange(from, to); - Refresh(); + Refresh(false); PositionCaret(); } @@ -1908,9 +1926,9 @@ void wxRichTextCtrl::Remove(long from, long to) from, // New caret position this); - Layout(); + LayoutContent(); if (!IsFrozen()) - Refresh(); + Refresh(false); } bool wxRichTextCtrl::IsModified() const @@ -2252,8 +2270,8 @@ bool wxRichTextCtrl::GetCaretPositionForIndex(long position, wxRect& rect) rect = wxRect(pt, wxSize(wxRICHTEXT_DEFAULT_CARET_WIDTH, height)); return true; } - else - return false; + + return false; } /// Gets the line for the visible caret position. If the caret is @@ -2282,7 +2300,7 @@ wxRichTextLine* wxRichTextCtrl::GetVisibleLineForCaretPosition(long caretPositio bool wxRichTextCtrl::MoveCaret(long pos, bool showAtLineStart) { if (GetBuffer().GetDirty()) - Layout(); + LayoutContent(); if (pos <= GetBuffer().GetRange().GetEnd()) { @@ -2298,7 +2316,7 @@ bool wxRichTextCtrl::MoveCaret(long pos, bool showAtLineStart) /// Layout the buffer: which we must do before certain operations, such as /// setting the caret position. -bool wxRichTextCtrl::Layout(bool onlyVisibleRect) +bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect) { if (GetBuffer().GetDirty() || onlyVisibleRect) { @@ -2314,17 +2332,17 @@ bool wxRichTextCtrl::Layout(bool onlyVisibleRect) flags |= wxRICHTEXT_LAYOUT_SPECIFIED_RECT; availableSpace.SetPosition(GetLogicalPoint(wxPoint(0, 0))); } - + wxClientDC dc(this); dc.SetFont(GetFont()); - + PrepareDC(dc); - + GetBuffer().Defragment(); GetBuffer().UpdateRanges(); // If items were deleted, ranges need recalculation GetBuffer().Layout(dc, availableSpace, flags); GetBuffer().SetDirty(false); - + if (!IsFrozen()) SetupScrollbars(); } @@ -2503,8 +2521,8 @@ bool wxRichTextCtrl::SetDefaultStyleToCursorStyle() SetDefaultStyle(attr); return true; } - else - return false; + + return false; } /// Returns the first visible position in the current view @@ -2519,4 +2537,3 @@ long wxRichTextCtrl::GetFirstVisiblePosition() const #endif // wxUSE_RICHTEXT -