Applied patch #13534: wxRichTextCtrl: Make it easier to use TextEffects flags

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@69512 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Julian Smart
2011-10-22 13:19:40 +00:00
parent 23d11f0eb4
commit 75936ec600
7 changed files with 155 additions and 11 deletions

View File

@@ -1490,6 +1490,11 @@ public:
*/
virtual bool IsSelectionUnderlined();
/**
Returns @true if all of the selection, or the current caret position, has the supplied wxTextAttrEffects flag(s).
*/
virtual bool DoesSelectionHaveTextEffectFlag(int flag);
/**
Returns @true if all of the selection is aligned according to the specified flag.
*/
@@ -1510,6 +1515,12 @@ public:
*/
virtual bool ApplyUnderlineToSelection();
/**
Applies one or more wxTextAttrEffects flags to the selection (undoable).
If there's no selection, it's is applied to the current caret position
*/
virtual bool ApplyTextEffectToSelection(int flags);
/**
Applies the given alignment to the selection (undoable).
For alignment values, see wxTextAttr.

View File

@@ -1463,6 +1463,11 @@ public:
*/
virtual bool IsSelectionUnderlined();
/**
Returns @true if all of the selection, or the current caret position, has the supplied wxTextAttrEffects flag(s).
*/
virtual bool DoesSelectionHaveTextEffectFlag(int flag);
/**
Returns @true if all of the selection is aligned according to the specified flag.
*/
@@ -1483,6 +1488,12 @@ public:
*/
virtual bool ApplyUnderlineToSelection();
/**
Applies one or more wxTextAttrEffects flags to the selection (undoable).
If there's no selection, it's applied to the current caret position
*/
virtual bool ApplyTextEffectToSelection(int flags);
/**
Applies the given alignment to the selection (undoable).
For alignment values, see wxTextAttr.

View File

@@ -127,7 +127,8 @@ enum wxTextAttrBulletStyle
/**
Styles for wxTextAttr::SetTextEffects(). They can be combined together as a bitlist.
Of these, only wxTEXT_ATTR_EFFECT_CAPITALS and wxTEXT_ATTR_EFFECT_STRIKETHROUGH are implemented.
Of these, only wxTEXT_ATTR_EFFECT_CAPITALS, wxTEXT_ATTR_EFFECT_STRIKETHROUGH,
wxTEXT_ATTR_EFFECT_SUPERSCRIPT and wxTEXT_ATTR_EFFECT_SUBSCRIPT are implemented.
*/
enum wxTextAttrEffects
{
@@ -807,8 +808,8 @@ public:
Sets the text effects, a bit list of styles.
The ::wxTextAttrEffects enumeration values can be used.
Of these, only wxTEXT_ATTR_EFFECT_CAPITALS and wxTEXT_ATTR_EFFECT_STRIKETHROUGH
are implemented.
Of these, only wxTEXT_ATTR_EFFECT_CAPITALS, wxTEXT_ATTR_EFFECT_STRIKETHROUGH,
wxTEXT_ATTR_EFFECT_SUPERSCRIPT and wxTEXT_ATTR_EFFECT_SUBSCRIPT are implemented.
wxTEXT_ATTR_EFFECT_CAPITALS capitalises text when displayed (leaving the case
of the actual buffer text unchanged), and wxTEXT_ATTR_EFFECT_STRIKETHROUGH draws

View File

@@ -131,9 +131,16 @@ public:
void OnItalic(wxCommandEvent& event);
void OnUnderline(wxCommandEvent& event);
void OnStrikethrough(wxCommandEvent& event);
void OnSuperscript(wxCommandEvent& event);
void OnSubscript(wxCommandEvent& event);
void OnUpdateBold(wxUpdateUIEvent& event);
void OnUpdateItalic(wxUpdateUIEvent& event);
void OnUpdateUnderline(wxUpdateUIEvent& event);
void OnUpdateStrikethrough(wxUpdateUIEvent& event);
void OnUpdateSuperscript(wxUpdateUIEvent& event);
void OnUpdateSubscript(wxUpdateUIEvent& event);
void OnAlignLeft(wxCommandEvent& event);
void OnAlignCentre(wxCommandEvent& event);
@@ -215,6 +222,9 @@ enum
ID_FORMAT_BOLD = 100,
ID_FORMAT_ITALIC,
ID_FORMAT_UNDERLINE,
ID_FORMAT_STRIKETHROUGH,
ID_FORMAT_SUPERSCRIPT,
ID_FORMAT_SUBSCRIPT,
ID_FORMAT_FONT,
ID_FORMAT_IMAGE,
ID_FORMAT_PARAGRAPH,
@@ -280,10 +290,18 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_FORMAT_ITALIC, MyFrame::OnItalic)
EVT_MENU(ID_FORMAT_UNDERLINE, MyFrame::OnUnderline)
EVT_MENU(ID_FORMAT_STRIKETHROUGH, MyFrame::OnStrikethrough)
EVT_MENU(ID_FORMAT_SUPERSCRIPT, MyFrame::OnSuperscript)
EVT_MENU(ID_FORMAT_SUBSCRIPT, MyFrame::OnSubscript)
EVT_UPDATE_UI(ID_FORMAT_BOLD, MyFrame::OnUpdateBold)
EVT_UPDATE_UI(ID_FORMAT_ITALIC, MyFrame::OnUpdateItalic)
EVT_UPDATE_UI(ID_FORMAT_UNDERLINE, MyFrame::OnUpdateUnderline)
EVT_UPDATE_UI(ID_FORMAT_STRIKETHROUGH, MyFrame::OnUpdateStrikethrough)
EVT_UPDATE_UI(ID_FORMAT_SUPERSCRIPT, MyFrame::OnUpdateSuperscript)
EVT_UPDATE_UI(ID_FORMAT_SUBSCRIPT, MyFrame::OnUpdateSubscript)
EVT_MENU(ID_FORMAT_ALIGN_LEFT, MyFrame::OnAlignLeft)
EVT_MENU(ID_FORMAT_ALIGN_CENTRE, MyFrame::OnAlignCentre)
EVT_MENU(ID_FORMAT_ALIGN_RIGHT, MyFrame::OnAlignRight)
@@ -616,6 +634,10 @@ MyFrame::MyFrame(const wxString& title, wxWindowID id, const wxPoint& pos,
formatMenu->AppendCheckItem(ID_FORMAT_ITALIC, _("&Italic\tCtrl+I"));
formatMenu->AppendCheckItem(ID_FORMAT_UNDERLINE, _("&Underline\tCtrl+U"));
formatMenu->AppendSeparator();
formatMenu->AppendCheckItem(ID_FORMAT_STRIKETHROUGH, _("Stri&kethrough"));
formatMenu->AppendCheckItem(ID_FORMAT_SUPERSCRIPT, _("Superscrip&t"));
formatMenu->AppendCheckItem(ID_FORMAT_SUBSCRIPT, _("Subscrip&t"));
formatMenu->AppendSeparator();
formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_LEFT, _("L&eft Align"));
formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_RIGHT, _("&Right Align"));
formatMenu->AppendCheckItem(ID_FORMAT_ALIGN_CENTRE, _("&Centre"));
@@ -759,7 +781,6 @@ void MyFrame::WriteInitialText()
r.Freeze();
#if 1
r.BeginParagraphSpacing(0, 20);
r.BeginAlignment(wxTEXT_ALIGNMENT_CENTRE);
@@ -791,6 +812,7 @@ void MyFrame::WriteInitialText()
imageAttr.GetTextBoxAttr().SetFloatMode(wxTEXT_BOX_ATTR_FLOAT_LEFT);
r.WriteText(wxString(wxT("This is a simple test for a floating left image test. The zebra image should be placed at the left side of the current buffer and all the text should flow around it at the right side. This is a simple test for a floating left image test. The zebra image should be placed at the left side of the current buffer and all the text should flow around it at the right side. This is a simple test for a floating left image test. The zebra image should be placed at the left side of the current buffer and all the text should flow around it at the right side.")));
r.WriteImage(wxBitmap(zebra_xpm), wxBITMAP_TYPE_PNG, imageAttr);
imageAttr.GetTextBoxAttr().GetTop().SetValue(200);
imageAttr.GetTextBoxAttr().GetTop().SetUnits(wxTEXT_ATTR_UNITS_PIXELS);
imageAttr.GetTextBoxAttr().SetFloatMode(wxTEXT_BOX_ATTR_FLOAT_RIGHT);
@@ -953,7 +975,6 @@ void MyFrame::WriteInitialText()
r.WriteText(wxT("Note: this sample content was generated programmatically from within the MyFrame constructor in the demo. The images were loaded from inline XPMs. Enjoy wxRichTextCtrl!\n"));
r.EndParagraphSpacing();
#endif
#if 1
{
@@ -1016,6 +1037,7 @@ void MyFrame::WriteInitialText()
r.SetInsertionPointEnd();
}
#endif
r.Thaw();
r.EndSuppressUndo();
@@ -1164,6 +1186,21 @@ void MyFrame::OnUnderline(wxCommandEvent& WXUNUSED(event))
m_richTextCtrl->ApplyUnderlineToSelection();
}
void MyFrame::OnStrikethrough(wxCommandEvent& WXUNUSED(event))
{
m_richTextCtrl->ApplyTextEffectToSelection(wxTEXT_ATTR_EFFECT_STRIKETHROUGH);
}
void MyFrame::OnSuperscript(wxCommandEvent& WXUNUSED(event))
{
m_richTextCtrl->ApplyTextEffectToSelection(wxTEXT_ATTR_EFFECT_SUPERSCRIPT);
}
void MyFrame::OnSubscript(wxCommandEvent& WXUNUSED(event))
{
m_richTextCtrl->ApplyTextEffectToSelection(wxTEXT_ATTR_EFFECT_SUBSCRIPT);
}
void MyFrame::OnUpdateBold(wxUpdateUIEvent& event)
{
@@ -1180,6 +1217,21 @@ void MyFrame::OnUpdateUnderline(wxUpdateUIEvent& event)
event.Check(m_richTextCtrl->IsSelectionUnderlined());
}
void MyFrame::OnUpdateStrikethrough(wxUpdateUIEvent& event)
{
event.Check(m_richTextCtrl->DoesSelectionHaveTextEffectFlag(wxTEXT_ATTR_EFFECT_STRIKETHROUGH));
}
void MyFrame::OnUpdateSuperscript(wxUpdateUIEvent& event)
{
event.Check(m_richTextCtrl->DoesSelectionHaveTextEffectFlag(wxTEXT_ATTR_EFFECT_SUPERSCRIPT));
}
void MyFrame::OnUpdateSubscript(wxUpdateUIEvent& event)
{
event.Check(m_richTextCtrl->DoesSelectionHaveTextEffectFlag(wxTEXT_ATTR_EFFECT_SUBSCRIPT));
}
void MyFrame::OnAlignLeft(wxCommandEvent& WXUNUSED(event))
{
m_richTextCtrl->ApplyAlignmentToSelection(wxTEXT_ALIGNMENT_LEFT);

View File

@@ -107,7 +107,7 @@ TAG_HANDLER_BEGIN(FONT, "FONT" )
TAG_HANDLER_END(FONT)
TAG_HANDLER_BEGIN(FACES_U, "U,STRIKE")
TAG_HANDLER_BEGIN(FACES_U, "U,STRIKE,DEL")
TAG_HANDLER_CONSTR(FACES_U) { }

View File

@@ -3510,7 +3510,7 @@ bool wxRichTextCtrl::LayoutContent(bool onlyVisibleRect)
return true;
}
/// Is all of the selection bold?
/// Is all of the selection, or the current caret position, bold?
bool wxRichTextCtrl::IsSelectionBold()
{
if (HasSelection())
@@ -3540,7 +3540,7 @@ bool wxRichTextCtrl::IsSelectionBold()
return false;
}
/// Is all of the selection italics?
/// Is all of the selection, or the current caret position, italics?
bool wxRichTextCtrl::IsSelectionItalics()
{
if (HasSelection())
@@ -3570,7 +3570,7 @@ bool wxRichTextCtrl::IsSelectionItalics()
return false;
}
/// Is all of the selection underlined?
/// Is all of the selection, or the current caret position, underlined?
bool wxRichTextCtrl::IsSelectionUnderlined()
{
if (HasSelection())
@@ -3600,6 +3600,33 @@ bool wxRichTextCtrl::IsSelectionUnderlined()
return false;
}
/// Does all of the selection, or the current caret position, have this wxTextAttrEffects flag(s)?
bool wxRichTextCtrl::DoesSelectionHaveTextEffectFlag(int flag)
{
wxRichTextAttr attr;
attr.SetFlags(wxTEXT_ATTR_EFFECTS);
attr.SetTextEffectFlags(flag);
attr.SetTextEffects(flag);
if (HasSelection())
{
return HasCharacterAttributes(GetSelectionRange(), attr);
}
else
{
// If no selection, then we need to combine current style with default style
// to see what the effect would be if we started typing.
long pos = GetAdjustedCaretPosition(GetCaretPosition());
if (GetStyle(pos, attr))
{
if (IsDefaultStyleShowing())
wxRichTextApplyStyle(attr, GetDefaultStyleEx());
return (attr.GetTextEffectFlags() & flag);
}
}
return false;
}
/// Apply bold to the selection
bool wxRichTextCtrl::ApplyBoldToSelection()
{
@@ -3654,6 +3681,28 @@ bool wxRichTextCtrl::ApplyUnderlineToSelection()
return true;
}
/// Apply the wxTextAttrEffects flag(s) to the selection, or the current caret position if there's no selection
bool wxRichTextCtrl::ApplyTextEffectToSelection(int flags)
{
wxRichTextAttr attr;
attr.SetFlags(wxTEXT_ATTR_EFFECTS);
attr.SetTextEffectFlags(flags);
if (!DoesSelectionHaveTextEffectFlag(flags))
attr.SetTextEffects(flags);
else
attr.SetTextEffects(attr.GetTextEffectFlags() & ~flags);
if (HasSelection())
return SetStyleEx(GetSelectionRange(), attr, wxRICHTEXT_SETSTYLE_WITH_UNDO|wxRICHTEXT_SETSTYLE_OPTIMIZE|wxRICHTEXT_SETSTYLE_CHARACTERS_ONLY);
else
{
wxRichTextAttr current = GetDefaultStyleEx();
current.Apply(attr);
SetAndShowDefaultStyle(current);
}
return true;
}
/// Is all of the selection aligned according to the specified flag?
bool wxRichTextCtrl::IsSelectionAligned(wxTextAttrAlignment alignment)
{

View File

@@ -234,6 +234,16 @@ void wxRichTextHTMLHandler::BeginCharacterFormatting(const wxRichTextAttr& curre
if (thisStyle.HasURL())
str << wxT("<a href=\"") << thisStyle.GetURL() << wxT("\">");
if (thisStyle.HasTextEffects())
{
if (thisStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH)
str << wxT("<del>");
if (thisStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT)
str << wxT("<sup>");
if (thisStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT)
str << wxT("<sub>");
}
}
void wxRichTextHTMLHandler::EndCharacterFormatting(const wxRichTextAttr& WXUNUSED(currentStyle), const wxRichTextAttr& thisStyle, const wxRichTextAttr& WXUNUSED(paraStyle), wxTextOutputStream& stream)
@@ -248,6 +258,16 @@ void wxRichTextHTMLHandler::EndCharacterFormatting(const wxRichTextAttr& WXUNUSE
if (thisStyle.GetFontWeight() == wxBOLD)
stream << wxT("</b>");
if (thisStyle.HasTextEffects())
{
if (thisStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_STRIKETHROUGH)
stream << wxT("</del>");
if (thisStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUPERSCRIPT)
stream << wxT("</sup>");
if (thisStyle.GetTextEffects() & wxTEXT_ATTR_EFFECT_SUBSCRIPT)
stream << wxT("</sub>");
}
if (m_font)
{
m_font = false;