+// Refresh the area affected by a selection change
+bool wxRichTextCtrl::RefreshForSelectionChange(const wxRichTextSelection& oldSelection, const wxRichTextSelection& newSelection)
+{
+ // If the selection is not part of the focus object, or we have multiple ranges, then the chances are that
+ // the selection contains whole containers rather than just text, so refresh everything
+ // for now as it would be hard to compute the rectangle bounding all selections.
+ // TODO: improve on this.
+ if ((oldSelection.IsValid() && (oldSelection.GetContainer() != GetFocusObject() || oldSelection.GetCount() > 1)) ||
+ (newSelection.IsValid() && (newSelection.GetContainer() != GetFocusObject() || newSelection.GetCount() > 1)))
+ {
+ Refresh(false);
+ return true;
+ }
+
+ wxRichTextRange oldRange, newRange;
+ if (oldSelection.IsValid())
+ oldRange = oldSelection.GetRange();
+ else
+ oldRange = wxRICHTEXT_NO_SELECTION;
+ if (newSelection.IsValid())
+ newRange = newSelection.GetRange();
+ else
+ newRange = wxRICHTEXT_NO_SELECTION;
+
+ // Calculate the refresh rectangle - just the affected lines
+ long firstPos, lastPos;
+ if (oldRange.GetStart() == -2 && newRange.GetStart() != -2)
+ {
+ firstPos = newRange.GetStart();
+ lastPos = newRange.GetEnd();
+ }
+ else if (oldRange.GetStart() != -2 && newRange.GetStart() == -2)
+ {
+ firstPos = oldRange.GetStart();
+ lastPos = oldRange.GetEnd();
+ }
+ else if (oldRange.GetStart() == -2 && newRange.GetStart() == -2)
+ {
+ return false;
+ }
+ else
+ {
+ firstPos = wxMin(oldRange.GetStart(), newRange.GetStart());
+ lastPos = wxMax(oldRange.GetEnd(), newRange.GetEnd());
+ }
+
+ wxRichTextLine* firstLine = GetFocusObject()->GetLineAtPosition(firstPos);
+ wxRichTextLine* lastLine = GetFocusObject()->GetLineAtPosition(lastPos);
+
+ if (firstLine && lastLine)
+ {
+ wxSize clientSize = GetClientSize();
+ wxPoint pt1 = GetPhysicalPoint(firstLine->GetAbsolutePosition());
+ wxPoint pt2 = GetPhysicalPoint(lastLine->GetAbsolutePosition()) + wxPoint(0, lastLine->GetSize().y);
+
+ pt1.x = 0;
+ pt1.y = wxMax(0, pt1.y);
+ pt2.x = 0;
+ pt2.y = wxMin(clientSize.y, pt2.y);
+
+ wxRect rect(pt1, wxSize(clientSize.x, pt2.y - pt1.y));
+ RefreshRect(rect, false);
+ }
+ else
+ Refresh(false);
+
+ return true;
+}
+
+// margins functions
+bool wxRichTextCtrl::DoSetMargins(const wxPoint& pt)
+{
+ GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().SetValue(pt.x, wxTEXT_ATTR_UNITS_PIXELS);
+ GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetRight().SetValue(pt.x, wxTEXT_ATTR_UNITS_PIXELS);
+ GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().SetValue(pt.y, wxTEXT_ATTR_UNITS_PIXELS);
+ GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetBottom().SetValue(pt.y, wxTEXT_ATTR_UNITS_PIXELS);
+
+ return true;
+}
+
+wxPoint wxRichTextCtrl::DoGetMargins() const
+{
+ return wxPoint(GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetLeft().GetValue(),
+ GetBuffer().GetAttributes().GetTextBoxAttr().GetMargins().GetTop().GetValue());
+}
+
+bool wxRichTextCtrl::SetFocusObject(wxRichTextParagraphLayoutBox* obj, bool setCaretPosition)
+{
+ if (obj && !obj->AcceptsFocus())
+ return false;
+
+ wxRichTextParagraphLayoutBox* oldContainer = GetFocusObject();
+ bool changingContainer = (m_focusObject != obj);
+
+ if (changingContainer && HasSelection())
+ SelectNone();
+
+ m_focusObject = obj;
+
+ if (!obj)
+ m_focusObject = & m_buffer;
+
+ if (setCaretPosition && changingContainer)
+ {
+ m_selection.Reset();
+ m_selectionAnchor = -2;
+ m_selectionAnchorObject = NULL;
+ m_selectionState = wxRichTextCtrlSelectionState_Normal;
+
+ long pos = -1;
+
+ m_caretAtLineStart = false;
+ MoveCaret(pos, m_caretAtLineStart);
+ SetDefaultStyleToCursorStyle();
+
+ wxRichTextEvent cmdEvent(
+ wxEVT_COMMAND_RICHTEXT_FOCUS_OBJECT_CHANGED,
+ GetId());
+ cmdEvent.SetEventObject(this);
+ cmdEvent.SetPosition(m_caretPosition+1);
+ cmdEvent.SetOldContainer(oldContainer);
+ cmdEvent.SetContainer(m_focusObject);
+
+ GetEventHandler()->ProcessEvent(cmdEvent);
+ }
+ return true;
+}
+
+#if wxUSE_DRAG_AND_DROP
+void wxRichTextCtrl::OnDrop(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), wxDragResult def, wxDataObject* DataObj)
+{
+ m_preDrag = false;
+
+ if ((def != wxDragCopy) && (def != wxDragMove))
+ {
+ return;
+ }
+
+ if (!GetSelection().IsValid())
+ {
+ return;
+ }
+
+ wxRichTextParagraphLayoutBox* originContainer = GetSelection().GetContainer();
+ wxRichTextParagraphLayoutBox* destContainer = GetFocusObject(); // This will be the drop container, not necessarily the same as the origin one
+
+ wxRichTextBuffer* richTextBuffer = ((wxRichTextBufferDataObject*)DataObj)->GetRichTextBuffer();
+ if (richTextBuffer)
+ {
+ long position = GetCaretPosition();
+ wxRichTextRange selectionrange = GetInternalSelectionRange();
+ if (selectionrange.Contains(position) && (def == wxDragMove))
+ {
+ // It doesn't make sense to move onto itself
+ return;
+ }
+
+ // If we're moving, and the data is being moved forward, we need to drop first, then delete the selection
+ // If moving backwards, we need to delete then drop. If we're copying (or doing nothing) we don't delete anyway
+ bool DeleteAfter = (def == wxDragMove) && (position > selectionrange.GetEnd());
+ if ((def == wxDragMove) && !DeleteAfter)
+ {
+ // We can't use e.g. DeleteSelectedContent() as it uses the focus container
+ originContainer->DeleteRangeWithUndo(selectionrange, this, &GetBuffer());
+ }
+
+ destContainer->InsertParagraphsWithUndo(&GetBuffer(), position+1, *richTextBuffer, this, 0);
+ ShowPosition(position + richTextBuffer->GetOwnRange().GetEnd());
+
+ delete richTextBuffer;
+
+ if (DeleteAfter)
+ {
+ // We can't use e.g. DeleteSelectedContent() as it uses the focus container
+ originContainer->DeleteRangeWithUndo(selectionrange, this, &GetBuffer());
+ }
+
+ SelectNone();
+ Refresh();
+ }
+}
+#endif // wxUSE_DRAG_AND_DROP
+
+
+#if wxUSE_DRAG_AND_DROP
+bool wxRichTextDropSource::GiveFeedback(wxDragResult WXUNUSED(effect))
+{
+ wxCHECK_MSG(m_rtc, false, wxT("NULL m_rtc"));
+
+ long position = 0;
+ int hit = 0;
+ wxRichTextObject* hitObj = NULL;
+ wxRichTextParagraphLayoutBox* container = m_rtc->FindContainerAtPoint(m_rtc->ScreenToClient(wxGetMousePosition()), position, hit, hitObj);
+
+ if (!(hit & wxRICHTEXT_HITTEST_NONE) && container && container->AcceptsFocus())
+ {
+ m_rtc->StoreFocusObject(container);
+ m_rtc->SetCaretPositionAfterClick(container, position, hit);
+ }
+
+ return false; // so that the base-class sets a cursor
+}
+#endif // wxUSE_DRAG_AND_DROP
+
+bool wxRichTextCtrl::CanDeleteRange(wxRichTextParagraphLayoutBox& WXUNUSED(container), const wxRichTextRange& WXUNUSED(range)) const
+{
+ return true;
+}
+
+bool wxRichTextCtrl::CanInsertContent(wxRichTextParagraphLayoutBox& WXUNUSED(container), long WXUNUSED(pos)) const
+{
+ return true;
+}
+
+
+#if wxRICHTEXT_USE_OWN_CARET
+
+// ----------------------------------------------------------------------------
+// initialization and destruction
+// ----------------------------------------------------------------------------
+
+void wxRichTextCaret::Init()
+{
+ m_hasFocus = true;
+ m_refreshEnabled = true;
+
+ m_xOld =
+ m_yOld = -1;
+ m_richTextCtrl = NULL;
+ m_needsUpdate = false;
+ m_flashOn = true;
+}
+
+wxRichTextCaret::~wxRichTextCaret()
+{
+ if (m_timer.IsRunning())
+ m_timer.Stop();
+}
+
+// ----------------------------------------------------------------------------
+// showing/hiding/moving the caret (base class interface)
+// ----------------------------------------------------------------------------
+
+void wxRichTextCaret::DoShow()
+{
+ m_flashOn = true;
+
+ if (!m_timer.IsRunning())
+ m_timer.Start(GetBlinkTime());
+
+ Refresh();
+}
+
+void wxRichTextCaret::DoHide()
+{
+ if (m_timer.IsRunning())
+ m_timer.Stop();
+
+ Refresh();
+}
+
+void wxRichTextCaret::DoMove()
+{
+ if (IsVisible())
+ {
+ Refresh();
+
+ if (m_xOld != -1 && m_yOld != -1)
+ {
+ if (m_richTextCtrl && m_refreshEnabled)
+ {
+ wxRect rect(GetPosition(), GetSize());
+ m_richTextCtrl->RefreshRect(rect, false);
+ }
+ }
+ }
+
+ m_xOld = m_x;
+ m_yOld = m_y;
+}
+
+void wxRichTextCaret::DoSize()
+{
+ int countVisible = m_countVisible;
+ if (countVisible > 0)
+ {
+ m_countVisible = 0;
+ DoHide();
+ }
+
+ if (countVisible > 0)
+ {
+ m_countVisible = countVisible;
+ DoShow();
+ }
+}
+
+// ----------------------------------------------------------------------------
+// handling the focus
+// ----------------------------------------------------------------------------
+
+void wxRichTextCaret::OnSetFocus()
+{
+ m_hasFocus = true;
+
+ if ( IsVisible() )
+ Refresh();
+}
+
+void wxRichTextCaret::OnKillFocus()
+{
+ m_hasFocus = false;
+}
+
+// ----------------------------------------------------------------------------
+// drawing the caret
+// ----------------------------------------------------------------------------
+
+void wxRichTextCaret::Refresh()
+{
+ if (m_richTextCtrl && m_refreshEnabled)
+ {
+ wxRect rect(GetPosition(), GetSize());
+ m_richTextCtrl->RefreshRect(rect, false);
+ }
+}
+
+void wxRichTextCaret::DoDraw(wxDC *dc)
+{
+ dc->SetPen( *wxBLACK_PEN );
+
+ dc->SetBrush(*(m_hasFocus ? wxBLACK_BRUSH : wxTRANSPARENT_BRUSH));
+ dc->SetPen(*wxBLACK_PEN);
+
+ wxPoint pt(m_x, m_y);
+
+ if (m_richTextCtrl)
+ {
+ pt = m_richTextCtrl->GetLogicalPoint(pt);
+ }
+ if (IsVisible() && m_flashOn)
+ dc->DrawRectangle(pt.x, pt.y, m_width, m_height);
+}
+
+void wxRichTextCaret::Notify()
+{
+ m_flashOn = !m_flashOn;
+ Refresh();
+}
+
+void wxRichTextCaretTimer::Notify()
+{
+ m_caret->Notify();
+}
+#endif
+ // wxRICHTEXT_USE_OWN_CARET
+
+// Add an item
+bool wxRichTextContextMenuPropertiesInfo::AddItem(const wxString& label, wxRichTextObject* obj)
+{
+ if (GetCount() < 3)
+ {
+ m_labels.Add(label);
+ m_objects.Add(obj);
+ return true;
+ }
+ else
+ return false;
+}
+
+// Returns number of menu items were added.
+int wxRichTextContextMenuPropertiesInfo::AddMenuItems(wxMenu* menu, int startCmd) const
+{
+ wxMenuItem* item = menu->FindItem(startCmd);
+ // If none of the standard properties identifiers are in the menu, add them if necessary.
+ // If no items to add, just set the text to something generic
+ if (GetCount() == 0)
+ {
+ if (item)
+ {
+ menu->SetLabel(startCmd, _("&Properties"));
+
+ // Delete the others if necessary
+ int i;
+ for (i = startCmd+1; i < startCmd+3; i++)
+ {
+ if (menu->FindItem(i))
+ {
+ menu->Delete(i);
+ }
+ }
+ }
+ }
+ else
+ {
+ int i;
+ int pos = -1;
+ // Find the position of the first properties item
+ for (i = 0; i < (int) menu->GetMenuItemCount(); i++)
+ {
+ wxMenuItem* item = menu->FindItemByPosition(i);
+ if (item && item->GetId() == startCmd)
+ {
+ pos = i;
+ break;
+ }
+ }
+
+ if (pos != -1)
+ {
+ int insertBefore = pos+1;
+ for (i = startCmd; i < startCmd+GetCount(); i++)
+ {
+ if (menu->FindItem(i))
+ {
+ menu->SetLabel(i, m_labels[i - startCmd]);
+ }
+ else
+ {
+ if (insertBefore >= (int) menu->GetMenuItemCount())
+ menu->Append(i, m_labels[i - startCmd]);
+ else
+ menu->Insert(insertBefore, i, m_labels[i - startCmd]);
+ }
+ insertBefore ++;
+ }
+
+ // Delete any old items still left on the menu
+ for (i = startCmd + GetCount(); i < startCmd+3; i++)
+ {
+ if (menu->FindItem(i))
+ {
+ menu->Delete(i);
+ }
+ }
+ }
+ else
+ {
+ // No existing property identifiers were found, so append to the end of the menu.
+ menu->AppendSeparator();
+ for (i = startCmd; i < startCmd+GetCount(); i++)
+ {
+ menu->Append(i, m_labels[i - startCmd]);
+ }
+ }
+ }
+
+ return GetCount();
+}
+
+// Add appropriate menu items for the current container and clicked on object
+// (and container's parent, if appropriate).
+int wxRichTextContextMenuPropertiesInfo::AddItems(wxRichTextCtrl* ctrl, wxRichTextObject* container, wxRichTextObject* obj)
+{
+ Clear();
+ if (obj && ctrl->CanEditProperties(obj))
+ AddItem(ctrl->GetPropertiesMenuLabel(obj), obj);
+
+ if (container && container != obj && ctrl->CanEditProperties(container) && m_labels.Index(ctrl->GetPropertiesMenuLabel(container)) == wxNOT_FOUND)
+ AddItem(ctrl->GetPropertiesMenuLabel(container), container);
+
+ if (container && container->GetParent() && ctrl->CanEditProperties(container->GetParent()) && m_labels.Index(ctrl->GetPropertiesMenuLabel(container->GetParent())) == wxNOT_FOUND)
+ AddItem(ctrl->GetPropertiesMenuLabel(container->GetParent()), container->GetParent());
+
+ return GetCount();
+}
+