📄 richtextbuffer.h
字号:
/////////////////////////////////////////////////////////////////////////////
// Name: wx/richtext/richtextbuffer.h
// Purpose: Buffer for wxRichTextCtrl
// Author: Julian Smart
// Modified by:
// Created: 2005-09-30
// RCS-ID: $Id: richtextbuffer.h,v 1.14 2006/07/03 21:13:14 JS Exp $
// Copyright: (c) Julian Smart
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
#ifndef _WX_RICHTEXTBUFFER_H_
#define _WX_RICHTEXTBUFFER_H_
/*
Data structures
===============
Data is represented by a hierarchy of objects, all derived from
wxRichTextObject.
The top of the hierarchy is the buffer, a kind of wxRichTextParagraphLayoutBox.
These boxes will allow flexible placement of text boxes on a page, but
for now there will be a single box representing the document,
and this box will a wxRichTextParagraphLayoutBox which contains further
wxRichTextParagraph objects, each of which can include text and images.
Each object maintains a range (start and end position) measured
from the start of the main parent box.
A paragraph object knows its range, and a text fragment knows its range
too. So, a character or image in a page has a position relative to the
start of the document, and a character in an embedded text box has
a position relative to that text box. For now, we will not be dealing with
embedded objects but it's something to bear in mind for later.
Layout
======
When Layout is called on an object, it is given a size which the object
must limit itself to, or one or more flexible directions (vertical
or horizontal). So for example a centered paragraph is given the page
width to play with (minus any margins), but can extend indefinitely
in the vertical direction. The implementation of Layout can then
cache the calculated size and position within the parent.
Note that position and size should be calculated separately, because
for example inserting a paragraph may result in the following paragraphs
moving down, but not changing in size.
Need to determine how objects specify their position. Absolute coordinates,
or relative to last object? May be hard to determine that. So should probably
be in absolute coordinates, in which case we'll need a Move virtual function
that allows quick movement of all elements without layout.
Let's work through a simple example of layout. Say we're laying out
a document with the buffer as the top box, with a wxRichTextParagraphLayoutBox
inside that that consists of wxRichTextParagraph objects.
We're in a mode whereby changes of window size change the width of the
page (useful for text editors, as opposed to word processors). The
window width is 600.
We pass (600, -1) to the top-level Layout (i.e. restrict size in horizontal
direction only). The wxRichTextBuffer box doesn't currently have
well-defined layout behaviour so we simply assume it has one child
that fills its parent (later we can define sizer-like box layout behaviour).
So it passes the same dimensions to the child, which is a wxRichTextParagraphLayoutBox.
This then looks at each child in turn (wxRichTextParagraph) and determines
the size the paragraph will take up, setting the cached size, and
splitting the paragraph into lines.
When the layout for one paragraph returns, the next paragraph is
fed the position of the previous, so it can position itself.
Each time Layout is called, the cached list of lines for each paragraph
is recreated, since it can change for example if the parent object width
changes.
Reporting size
==============
Each object can report its size for a given range. It's important that
it can report a partial size, so that wrapping can be implemented,
hit test calculations performed, etc. So GetRangeSize must be implemented
for each object.
*/
/*!
* Includes
*/
#include "wx/defs.h"
#if wxUSE_RICHTEXT
#include "wx/list.h"
#include "wx/textctrl.h"
#include "wx/bitmap.h"
#include "wx/image.h"
#include "wx/cmdproc.h"
#include "wx/txtstrm.h"
/*!
* File types
*/
#define wxRICHTEXT_TYPE_ANY 0
#define wxRICHTEXT_TYPE_TEXT 1
#define wxRICHTEXT_TYPE_XML 2
#define wxRICHTEXT_TYPE_HTML 3
#define wxRICHTEXT_TYPE_RTF 4
#define wxRICHTEXT_TYPE_PDF 5
/*!
* Forward declarations
*/
class WXDLLIMPEXP_RICHTEXT wxRichTextCtrl;
class WXDLLIMPEXP_RICHTEXT wxRichTextObject;
class WXDLLIMPEXP_RICHTEXT wxRichTextCacheObject;
class WXDLLIMPEXP_RICHTEXT wxRichTextObjectList;
class WXDLLIMPEXP_RICHTEXT wxRichTextLine;
class WXDLLIMPEXP_RICHTEXT wxRichTextParagraph;
class WXDLLIMPEXP_RICHTEXT wxRichTextFragment;
class WXDLLIMPEXP_RICHTEXT wxRichTextFileHandler;
class WXDLLIMPEXP_RICHTEXT wxRichTextStyleSheet;
class WXDLLIMPEXP_RICHTEXT wxTextAttrEx;
/*!
* Flags determining the available space, passed to Layout
*/
#define wxRICHTEXT_FIXED_WIDTH 0x01
#define wxRICHTEXT_FIXED_HEIGHT 0x02
#define wxRICHTEXT_VARIABLE_WIDTH 0x04
#define wxRICHTEXT_VARIABLE_HEIGHT 0x08
// Only lay out the part of the buffer that lies within
// the rect passed to Layout.
#define wxRICHTEXT_LAYOUT_SPECIFIED_RECT 0x10
/*!
* Flags returned from hit-testing
*/
// The point was not on this object
#define wxRICHTEXT_HITTEST_NONE 0x01
// The point was before the position returned from HitTest
#define wxRICHTEXT_HITTEST_BEFORE 0x02
// The point was after the position returned from HitTest
#define wxRICHTEXT_HITTEST_AFTER 0x04
// The point was on the position returned from HitTest
#define wxRICHTEXT_HITTEST_ON 0x08
/*!
* Flags for GetRangeSize
*/
#define wxRICHTEXT_FORMATTED 0x01
#define wxRICHTEXT_UNFORMATTED 0x02
/*!
* Extra formatting flags not in wxTextAttr
*/
#define wxTEXT_ATTR_PARA_SPACING_AFTER 0x00000800
#define wxTEXT_ATTR_PARA_SPACING_BEFORE 0x00001000
#define wxTEXT_ATTR_LINE_SPACING 0x00002000
#define wxTEXT_ATTR_CHARACTER_STYLE_NAME 0x00004000
#define wxTEXT_ATTR_PARAGRAPH_STYLE_NAME 0x00008000
#define wxTEXT_ATTR_BULLET_STYLE 0x00010000
#define wxTEXT_ATTR_BULLET_NUMBER 0x00020000
#define wxTEXT_ATTR_BULLET_SYMBOL 0x00040000
/*!
* Styles for wxTextAttrEx::SetBulletStyle
*/
#define wxTEXT_ATTR_BULLET_STYLE_NONE 0x0000
#define wxTEXT_ATTR_BULLET_STYLE_ARABIC 0x0001
#define wxTEXT_ATTR_BULLET_STYLE_LETTERS_UPPER 0x0002
#define wxTEXT_ATTR_BULLET_STYLE_LETTERS_LOWER 0x0004
#define wxTEXT_ATTR_BULLET_STYLE_ROMAN_UPPER 0x0008
#define wxTEXT_ATTR_BULLET_STYLE_ROMAN_LOWER 0x0010
#define wxTEXT_ATTR_BULLET_STYLE_SYMBOL 0x0020
#define wxTEXT_ATTR_BULLET_STYLE_BITMAP 0x0040
#define wxTEXT_ATTR_BULLET_STYLE_PARENTHESES 0x0080
#define wxTEXT_ATTR_BULLET_STYLE_PERIOD 0x0100
/*!
* Line spacing values
*/
#define wxTEXT_ATTR_LINE_SPACING_NORMAL 10
#define wxTEXT_ATTR_LINE_SPACING_HALF 15
#define wxTEXT_ATTR_LINE_SPACING_TWICE 20
/*!
* wxRichTextRange class declaration
* This stores beginning and end positions for a range of data.
*/
class WXDLLIMPEXP_RICHTEXT wxRichTextRange
{
public:
// Constructors
wxRichTextRange() { m_start = 0; m_end = 0; }
wxRichTextRange(long start, long end) { m_start = start; m_end = end; }
wxRichTextRange(const wxRichTextRange& range) { m_start = range.m_start; m_end = range.m_end; }
~wxRichTextRange() {}
void operator =(const wxRichTextRange& range) { m_start = range.m_start; m_end = range.m_end; }
bool operator ==(const wxRichTextRange& range) const { return (m_start == range.m_start && m_end == range.m_end); }
wxRichTextRange operator -(const wxRichTextRange& range) const { return wxRichTextRange(m_start - range.m_start, m_end - range.m_end); }
wxRichTextRange operator +(const wxRichTextRange& range) const { return wxRichTextRange(m_start + range.m_start, m_end + range.m_end); }
void SetRange(long start, long end) { m_start = start; m_end = end; }
void SetStart(long start) { m_start = start; }
long GetStart() const { return m_start; }
void SetEnd(long end) { m_end = end; }
long GetEnd() const { return m_end; }
/// Returns true if this range is completely outside 'range'
bool IsOutside(const wxRichTextRange& range) const { return range.m_start > m_end || range.m_end < m_start; }
/// Returns true if this range is completely within 'range'
bool IsWithin(const wxRichTextRange& range) const { return m_start >= range.m_start && m_end <= range.m_end; }
/// Returns true if the given position is within this range. Allow
/// for the possibility of an empty range - assume the position
/// is within this empty range. NO, I think we should not match with an empty range.
// bool Contains(long pos) const { return pos >= m_start && (pos <= m_end || GetLength() == 0); }
bool Contains(long pos) const { return pos >= m_start && pos <= m_end ; }
/// Limit this range to be within 'range'
bool LimitTo(const wxRichTextRange& range) ;
/// Gets the length of the range
long GetLength() const { return m_end - m_start + 1; }
/// Swaps the start and end
void Swap() { long tmp = m_start; m_start = m_end; m_end = tmp; }
protected:
long m_start;
long m_end;
};
#define wxRICHTEXT_ALL wxRichTextRange(-2, -2)
#define wxRICHTEXT_NONE wxRichTextRange(-1, -1)
/*!
* wxTextAttrEx is an extended version of wxTextAttr with more paragraph attributes.
*/
class WXDLLIMPEXP_RICHTEXT wxTextAttrEx: public wxTextAttr
{
public:
// ctors
wxTextAttrEx(const wxTextAttrEx& attr);
wxTextAttrEx(const wxTextAttr& attr) { Init(); (*this) = attr; }
wxTextAttrEx() { Init(); }
// Initialise this object.
void Init();
// Assignment from a wxTextAttrEx object
void operator= (const wxTextAttrEx& attr);
// Assignment from a wxTextAttr object.
void operator= (const wxTextAttr& attr);
// setters
void SetCharacterStyleName(const wxString& name) { m_characterStyleName = name; }
void SetParagraphStyleName(const wxString& name) { m_paragraphStyleName = name; }
void SetParagraphSpacingAfter(int spacing) { m_paragraphSpacingAfter = spacing; }
void SetParagraphSpacingBefore(int spacing) { m_paragraphSpacingBefore = spacing; }
void SetLineSpacing(int spacing) { m_lineSpacing = spacing; }
void SetBulletStyle(int style) { m_bulletStyle = style; }
void SetBulletNumber(int n) { m_bulletNumber = n; }
void SetBulletSymbol(wxChar symbol) { m_bulletSymbol = symbol; }
const wxString& GetCharacterStyleName() const { return m_characterStyleName; }
const wxString& GetParagraphStyleName() const { return m_paragraphStyleName; }
int GetParagraphSpacingAfter() const { return m_paragraphSpacingAfter; }
int GetParagraphSpacingBefore() const { return m_paragraphSpacingBefore; }
int GetLineSpacing() const { return m_lineSpacing; }
int GetBulletStyle() const { return m_bulletStyle; }
int GetBulletNumber() const { return m_bulletNumber; }
wxChar GetBulletSymbol() const { return m_bulletSymbol; }
bool HasWeight() const { return (GetFlags() & wxTEXT_ATTR_FONT_WEIGHT) != 0; }
bool HasSize() const { return (GetFlags() & wxTEXT_ATTR_FONT_SIZE) != 0; }
bool HasItalic() const { return (GetFlags() & wxTEXT_ATTR_FONT_ITALIC) != 0; }
bool HasUnderlined() const { return (GetFlags() & wxTEXT_ATTR_FONT_UNDERLINE) != 0; }
bool HasFaceName() const { return (GetFlags() & wxTEXT_ATTR_FONT_FACE) != 0; }
bool HasParagraphSpacingAfter() const { return HasFlag(wxTEXT_ATTR_PARA_SPACING_AFTER); }
bool HasParagraphSpacingBefore() const { return HasFlag(wxTEXT_ATTR_PARA_SPACING_BEFORE); }
bool HasLineSpacing() const { return HasFlag(wxTEXT_ATTR_LINE_SPACING); }
bool HasCharacterStyleName() const { return HasFlag(wxTEXT_ATTR_CHARACTER_STYLE_NAME); }
bool HasParagraphStyleName() const { return HasFlag(wxTEXT_ATTR_PARAGRAPH_STYLE_NAME); }
bool HasBulletStyle() const { return HasFlag(wxTEXT_ATTR_BULLET_STYLE); }
bool HasBulletNumber() const { return HasFlag(wxTEXT_ATTR_BULLET_NUMBER); }
bool HasBulletSymbol() const { return HasFlag(wxTEXT_ATTR_BULLET_SYMBOL); }
// Is this a character style?
bool IsCharacterStyle() const { return (0 != (GetFlags() & (wxTEXT_ATTR_FONT | wxTEXT_ATTR_BACKGROUND_COLOUR | wxTEXT_ATTR_TEXT_COLOUR))); }
bool IsParagraphStyle() const { return (0 != (GetFlags() & (wxTEXT_ATTR_ALIGNMENT|wxTEXT_ATTR_LEFT_INDENT|wxTEXT_ATTR_RIGHT_INDENT|wxTEXT_ATTR_TABS|
wxTEXT_ATTR_PARA_SPACING_BEFORE|wxTEXT_ATTR_PARA_SPACING_AFTER|wxTEXT_ATTR_LINE_SPACING|
wxTEXT_ATTR_BULLET_STYLE|wxTEXT_ATTR_BULLET_NUMBER))); }
// returns false if we have any attributes set, true otherwise
bool IsDefault() const
{
return !HasTextColour() && !HasBackgroundColour() && !HasFont() && !HasAlignment() &&
!HasTabs() && !HasLeftIndent() && !HasRightIndent() &&
!HasParagraphSpacingAfter() && !HasParagraphSpacingBefore() && !HasLineSpacing() &&
!HasCharacterStyleName() && !HasParagraphStyleName() && !HasBulletNumber() && !HasBulletStyle() && !HasBulletSymbol();
}
// return the attribute having the valid font and colours: it uses the
// attributes set in attr and falls back first to attrDefault and then to
// the text control font/colours for those attributes which are not set
static wxTextAttrEx CombineEx(const wxTextAttrEx& attr,
const wxTextAttrEx& attrDef,
const wxTextCtrlBase *text);
private:
// Paragraph styles
int m_paragraphSpacingAfter;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -