X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/0bab774ba337ba8740082e45c98d48f85eb1c381..955e46d25d6ac0f9fa55ec9fe11f0c21da3a494d:/src/richtext/richtextctrl.cpp diff --git a/src/richtext/richtextctrl.cpp b/src/richtext/richtextctrl.cpp index ea7c33ba18..c312aed560 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,26 +13,31 @@ #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/settings.h" + #include "wx/menu.h" + #include "wx/intl.h" + #include "wx/log.h" + #include "wx/stopwatch.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" +// DLL options compatibility check: +#include "wx/app.h" +WX_CHECK_BUILD_OPTIONS("wxRichTextCtrl") + DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_SELECTED) DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_ITEM_DESELECTED) DEFINE_EVENT_TYPE(wxEVT_COMMAND_RICHTEXT_LEFT_CLICK) @@ -104,17 +109,17 @@ wxRichTextCtrl::wxRichTextCtrl() Init(); } -wxRichTextCtrl::wxRichTextCtrl( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style) +wxRichTextCtrl::wxRichTextCtrl( wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style) #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE : wxScrollHelper(this) #endif { Init(); - Create(parent, id, pos, size, style); + Create(parent, id, value, pos, size, style); } /// Creation -bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style) +bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, long style) { #if wxRICHTEXT_DERIVES_FROM_TEXTCTRLBASE if (!wxTextCtrlBase::Create(parent, id, pos, size, style|wxFULL_REPAINT_ON_RESIZE @@ -154,6 +159,9 @@ bool wxRichTextCtrl::Create( wxWindow* parent, wxWindowID id, const wxPoint& pos SetCursor(wxCursor(wxCURSOR_IBEAM)); + if (!value.IsEmpty()) + SetValue(value); + return true; } @@ -207,6 +215,8 @@ void wxRichTextCtrl::Clear() m_caretAtLineStart = false; m_selectionRange.SetRange(-2, -2); + SetScrollbars(0, 0, 0, 0, 0, 0); + if (m_freezeCount == 0) { SetupScrollbars(); @@ -224,19 +234,19 @@ void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event)) { 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()) @@ -245,7 +255,7 @@ void wxRichTextCtrl::OnPaint(wxPaintEvent& WXUNUSED(event)) GetBuffer().SetDirty(false); SetupScrollbars(); } - + GetBuffer().Draw(dc, GetBuffer().GetRange(), GetSelectionRange(), drawingArea, 0 /* descent */, 0 /* flags */); } @@ -431,13 +441,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) { 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")); @@ -456,6 +474,8 @@ void wxRichTextCtrl::OnChar(wxKeyEvent& event) EndBatchUndo(); SetDefaultStyleToCursorStyle(); + + ScrollIntoView(m_caretPosition, WXK_RIGHT); } else if (event.GetKeyCode() == WXK_BACK) { @@ -485,6 +505,7 @@ void wxRichTextCtrl::OnChar(wxKeyEvent& event) SetDefaultStyleToCursorStyle(); } + ScrollIntoView(m_caretPosition, WXK_LEFT); } else if (event.GetKeyCode() == WXK_DELETE) { @@ -526,11 +547,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. @@ -616,11 +634,11 @@ bool wxRichTextCtrl::KeyboardNavigate(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); } @@ -696,16 +714,16 @@ bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode) int ppuX, ppuY; GetScrollPixelsPerUnit(& ppuX, & ppuY); - int startX, startY; - GetViewStart(& startX, & startY); - startX = 0; - startY = startY * ppuY; + int startXUnits, startYUnits; + GetViewStart(& startXUnits, & startYUnits); + int startY = startYUnits * ppuY; - int sx, sy; + int sx = 0, sy = 0; GetVirtualSize(& sx, & sy); - sx = 0; + int sxUnits = 0; + int syUnits = 0; if (ppuY != 0) - sy = sy/ppuY; + syUnits = sy/ppuY; wxRect rect = line->GetRect(); @@ -714,18 +732,22 @@ 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)) { // Make it scroll so this item is at the bottom // of the window int y = rect.y - (clientSize.y - rect.height); - y = (int) (0.5 + y/ppuY); + int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); + + // If we're still off the screen, scroll another line down + if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY))) + yUnits ++; - if (startY != y) + if (startYUnits != yUnits) { - SetScrollbars(ppuX, ppuY, sx, sy, 0, y); + SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits); scrolled = true; } } @@ -734,28 +756,28 @@ bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode) // Make it scroll so this item is at the top // of the window int y = rect.y ; - y = (int) (0.5 + y/ppuY); + int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); - if (startY != y) + if (startYUnits != yUnits) { - SetScrollbars(ppuX, ppuY, sx, sy, 0, y); + SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits); scrolled = true; } } } // 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) { // Make it scroll so this item is at the top // of the window int y = rect.y ; - y = (int) (0.5 + y/ppuY); + int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); - if (startY != y) + if (startYUnits != yUnits) { - SetScrollbars(ppuX, ppuY, sx, sy, 0, y); + SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits); scrolled = true; } } @@ -764,11 +786,15 @@ bool wxRichTextCtrl::ScrollIntoView(long position, int keyCode) // Make it scroll so this item is at the bottom // of the window int y = rect.y - (clientSize.y - rect.height); - y = (int) (0.5 + y/ppuY); + int yUnits = (int) (0.5 + ((float) y)/(float) ppuY); - if (startY != y) + // If we're still off the screen, scroll another line down + if ((rect.y + rect.height) > (clientSize.y + (yUnits*ppuY))) + yUnits ++; + + if (startYUnits != yUnits) { - SetScrollbars(ppuX, ppuY, sx, sy, 0, y); + SetScrollbars(ppuX, ppuY, sxUnits, syUnits, 0, yUnits); scrolled = true; } } @@ -794,7 +820,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) @@ -1059,8 +1085,8 @@ bool wxRichTextCtrl::MoveDown(int noLines, int flags) Refresh(false); return true; } - else - return false; + + return false; } /// Move to the end of the paragraph @@ -1426,19 +1452,20 @@ void wxRichTextCtrl::SetupScrollbars(bool atTop) // TODO: reimplement scrolling so we scroll by line, not by fixed number // of pixels. See e.g. wxVScrolledWindow for ideas. - int pixelsPerUnit = 5; // 10; + int pixelsPerUnit = 5; wxSize clientSize = GetClientSize(); int maxHeight = GetBuffer().GetCachedSize().y; - int unitsY = maxHeight/pixelsPerUnit; + // Round up so we have at least maxHeight pixels + int unitsY = (int) (((float)maxHeight/(float)pixelsPerUnit) + 0.5); int startX = 0, startY = 0; if (!atTop) GetViewStart(& startX, & startY); int maxPositionX = 0; // wxMax(sz.x - clientSize.x, 0); - int maxPositionY = (wxMax(maxHeight - clientSize.y, 0))/pixelsPerUnit; + int maxPositionY = (int) ((((float)(wxMax((unitsY*pixelsPerUnit) - clientSize.y, 0)))/((float)pixelsPerUnit)) + 0.5); // Move to previous scroll position if // possible @@ -1579,17 +1606,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 @@ -1619,15 +1649,25 @@ wxRichTextCtrl::HitTest(const wxPoint& pt, wxClientDC dc((wxRichTextCtrl*) this); ((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; + // Buffer uses logical position (relative to start of buffer) + // so convert + wxPoint pt2 = GetLogicalPoint(pt); + + int hit = ((wxRichTextCtrl*)this)->GetBuffer().HitTest(dc, pt2, *pos); + + 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; } // ---------------------------------------------------------------------------- @@ -1697,8 +1737,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) @@ -1708,8 +1748,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) @@ -1726,9 +1766,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; } @@ -2003,10 +2042,10 @@ bool wxRichTextCtrl::CanRedo() const } // ---------------------------------------------------------------------------- -// implemenation details +// implementation details // ---------------------------------------------------------------------------- -void wxRichTextCtrl::Command(wxCommandEvent & event) +void wxRichTextCtrl::Command(wxCommandEvent& event) { SetValue(event.GetString()); GetEventHandler()->ProcessEvent(event); @@ -2159,6 +2198,11 @@ bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttrEx& style) return GetBuffer().SetStyle(wxRichTextRange(start, end), style); } +bool wxRichTextCtrl::SetStyle(long start, long end, const wxTextAttr& style) +{ + return GetBuffer().SetStyle(wxRichTextRange(start, end), wxTextAttrEx(style)); +} + bool wxRichTextCtrl::SetStyle(const wxRichTextRange& range, const wxRichTextAttr& style) { return GetBuffer().SetStyle(range, style); @@ -2169,11 +2213,33 @@ bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttrEx& style) return GetBuffer().SetDefaultStyle(style); } +bool wxRichTextCtrl::SetDefaultStyle(const wxTextAttr& style) +{ + return GetBuffer().SetDefaultStyle(wxTextAttrEx(style)); +} + const wxTextAttrEx& wxRichTextCtrl::GetDefaultStyleEx() const { return GetBuffer().GetDefaultStyle(); } +const wxTextAttr& wxRichTextCtrl::GetDefaultStyle() const +{ + return GetBuffer().GetDefaultStyle(); +} + +bool wxRichTextCtrl::GetStyle(long position, wxTextAttr& style) const +{ + wxTextAttrEx attr; + if (GetBuffer().GetStyle(position, attr)) + { + style = attr; + return true; + } + else + return false; +} + bool wxRichTextCtrl::GetStyle(long position, wxTextAttrEx& style) const { return GetBuffer().GetStyle(position, style); @@ -2256,8 +2322,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 @@ -2318,17 +2384,17 @@ bool wxRichTextCtrl::LayoutContent(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(); } @@ -2507,8 +2573,8 @@ bool wxRichTextCtrl::SetDefaultStyleToCursorStyle() SetDefaultStyle(attr); return true; } - else - return false; + + return false; } /// Returns the first visible position in the current view @@ -2523,4 +2589,3 @@ long wxRichTextCtrl::GetFirstVisiblePosition() const #endif // wxUSE_RICHTEXT -