richedit富文本控件开发思路与编码设计

[置顶] richedit富文本控件开发思路与编码设计




限于一直使用微软的richedit控件很多东西有问题都摸不着头脑,而工程使用中其实需要用到的并不高级需求也不高,需求就是快速简单的实现一个图文并茂的富文本显示工具;为此 ,我开始尝试设计一个简单易用的富文本显示和编辑工具,现在初步核心框架已经完成,为此分享给各位我的设计思路,欢迎拍砖~.~。 源代码在文章的结束处可下载。
首先,我们开始思考富文本控件特点:多行、图文并茂、样式自定义、光标命中、可拷贝、可黏贴、文本格式化(换行、缩进,这部分暂无考虑,因为这部分可以通过简易的 编辑实现);此时,我们开始思考这个富文本控件的整体框架,从我们列出的需求中进行核心数据结构抽取和搭建:第一,必须有一存放字符串的容器(data);第二, 样式自定义,那么就需要一个style容器(stylelist),图片其实也是样式的一种,那么我们就把它归类为style;第三,文本格式化,最重要的就是换行,那么我们就需要一个专门 来管理每一行状态的类;完成了基本的核心数据结构的设计以后,下一步我们要做的就是对每一种核心数据都需要有一个专门的类进行管理。
当前我们已经设计好了核心数据结构,让我们从基本的零部件设计开始做起:
一,字符串管理,我们需要的只是简单插入、删除、获取功能,我借鉴nopad++开源项目中的底层数组类SplitVector,是一个性能相当好的数组管理类,里面已经集成我们所需要的几个 基本功能,所以我就直接用它来存放数据;我们把一个char看成是此类中最小的数据单元;
二,样式管理,同样我们需要数组的基本功能,但是我们对样式的管理需求更复杂一点,所以我们需要专门创建一个管理类,里面包含一个样式数组,我们需要有样式的position定位查找功能等, 比如光标命中和绘画,我们暂且想到这些,为其封装一个行为集合类;我们把每一个style看成​​为最小数据单元,并定义出基本属性:

  1. struct StyleObj  
  2. {  
  3.     enum StyleType  
  4.     {  
  5.         ST_CHAR = 0,   
  6.         ST_OBJ  
  7.     };  
  8.   
  9.     enum StyleRemoveType  
  10.     {  
  11.         SRT_SUCCEED = 0,  
  12.         SRT_ALLDELETE,  
  13.         SRT_NOINRANGE  
  14.     };  
  15.         int type_;//style樣式,字符串樣式或者obj樣式  
  16.     int charPos_;//樣式在字符串中起點位置  
  17.     int charLength_;//樣式覆蓋字符串長度  
  18.     CharFormat cf_;//樣式  
  19.     IObj* obj_;//擴展對象  
  20.     LinkObj* link_;//鏈接對象  
  21.     HFONT font_;//樣式對應字體  
  22.   
  23.     int nRef_;//引用計數器  
  24. };  
鉴于篇幅,我在此只列出我们需要用到的基本属性;
三,行管理,行与样式一样,需要的功能集合差不多,所以我们单独给他封装一个管理类;我们把每一个line看成为最小数据单元,并定义出基本属性:

  1. struct LineObj : public IBaseObj  
  2. {  
  3.   
  4.     enum PosType{  
  5.         low = 0,  
  6.         in,  
  7.         beyond  
  8.     };  
  9.         int beginPos_;//行在字符串中起點位置  
  10.     int endPos_;//行在字符串中終點位置  
  11.     int width_;//行寬  
  12.     int lineIndex_;//行索引  
  13.   
  14.     RowObjDetail Details;//行詳細屬性,最高高度,最高高度位置  
  15.   
  16.     int indent_;//for paragraph,段落  
  17.     int yPos_;//行頂部在視口Y軸座標  
  18.   
  19.     int nRef_;//對象引用計數  
  20. };  
所有涉及到的辅组数据结构我将在文章结束的时候附上;
好了,走到这一步我们已经把基本的核心数据结构做好了,我们下一步要做的就是核心数据处理整合管理,我们所有的逻辑将是围绕这三个数据结构扩展和搭建的。 现在我们开始做的就是整体控件框架的搭建,从最外层呈现看这个控件可以分为以下几个部分:消息处理模块、核心数据操作模块、绘画模块;这其实就是一个控件的基本逻辑,收到 消息=》处理数据=》更新设备DC。
显然,我们必须要独立开控件的三个模块的东西,消息处理模块与系统打交道,它相当于一个外壳,得到外界的指令后将其传给我们的数据处理中心,即是核心数据操作模块, 核心数据处理模块处理完以后再消息通知外壳,外壳将绘画指令传给绘画模块,绘画模块根据数据中心数据属性进行相对于的呈现丢到外壳上面去。 分析完这三个模块,我们开始模块类封装:

  1. /*消息處理類,負責最外層收到消息後的分配和相關響應處理*/  
  2. class RichEditor  
  3. {  
  4. public:  
  5.     RichEditor();  
  6.   
  7. protected:  
  8.     bool    bTraceMouse_;  
  9.     bool    bLBButtonDown_;  
  10.     Selection selectRange_;  
  11.     Document document_;  
  12.     DocumentDetails details_;  
  13.     Window  window_;  
  14.     CHARFORMAT cfDefault_;  
  15.     CaretPosition caretPosition_;  
  16. };  
  17.   
  18. /*文檔操作類,負責文檔的插入刪除,更新,查找等功能*/  
  19.   
  20. class Document  
  21. {  
  22. public:  
  23.     Document();  
  24. protected:  
  25.       
  26.     SplitVector<MY_CHAR> data;//if is obj, use the '%' as symbol  
  27.     StyleObjManager styleList_;  
  28.     LineObjManager LineObjList_;  
  29.     DocumentDetails* details_;  
  30.     int             ident_;  
  31.     Window*         window_;  
  32.     Selection*      selectionRange_;  
  33. };  
  34.   
  35. /*窗口類,負責最終繪畫和長度計算,窗口基本屬性存儲*/  
  36. class Window  
  37. {  
  38.   
  39. public:  
  40.     Window();  
  41.     ~Window();  
  42.   
  43.     void Attach(HWND hWnd);  
  44.     void Detch();  
  45.     int GetCharLenght(MY_CHAR c, CHARFORMAT& cf, HFONT font);  
  46.     int GetTextLenght(MY_CHAR* c, int length, CHARFORMAT& cf, HFONT font);  
  47.     void PaintText(MY_CHAR* c, int length, RECT& rcText, CHARFORMAT& cf, HDC hdcPaint, HFONT font);  
  48.     void PaintObj(IObj* obj, RECT& rcObj, HDC hdcPaint, bool bSelect = false);  
  49.     void ReDraw();  
  50.     HWND GetWnd();  
  51.       
  52. protected:  
  53.     HWND    hWnd_;  
  54.     CHARFORMAT  cfCur_;  
  55.     bool    bSeletionMode_;  
  56.     bool    bDoubleBuffering_;  
  57.   
  58.     HBRUSH  m_brushFrame;  
  59.     HBRUSH  m_brushSelect;  
  60. };  

到此,我们控件的基本框架已经搭建完成,而下一步要做的就是实现核心逻辑,就是插入删除一个字符串或者插入一个扩展obj要走的流程,在这里我就不一一把代码放出来 ,限于代码篇幅,我只提供下思路哈:
插入:以'\n'为分隔符分别插入,style更新,data更新,line更新
  1. void Document::InsertString(const MY_CHAR* c, int pos, int length, CHARFORMAT& cf, MY_CHAR* hyperLinkData)  
  2. {  
  3.     int begin = 0;  
  4.     int begin1 = 0;  
  5.     int end = length;  
  6.     const MY_CHAR* cTemp = c;  
  7.     const MY_CHAR* cTemp1 = c;  
  8.     styleList_.Log();  
  9.     styleList_.InsertStyle(pos, length, cf, hyperLinkData);  
  10.     styleList_.Log();  
  11.     while(begin1 < end)  
  12.     {  
  13.         if (*cTemp == '\n')  
  14.         {  
  15.             int insertLenght = begin1-begin;  
  16.             data.InsertFromArray(pos, cTemp1, 0, insertLenght);  
  17.             UpdateLineManager_insert(pos, insertLenght);  
  18.             pos += begin1;  
  19.             begin = begin1;  
  20.             cTemp1 = cTemp;  
  21.         }  
  22.           
  23.         cTemp++;  
  24.         begin1++;  
  25.     }  
  26.   
  27.     if (begin < begin1)  
  28.     {  
  29.         int insertLenght = begin1-begin;  
  30.         data.InsertFromArray(pos, cTemp1, 0, insertLenght);  
  31.         UpdateLineManager_insert(pos, insertLenght);  
  32.         pos += begin1;  
  33.         begin = begin1;  
  34.         cTemp1 = cTemp;  
  35.     }  
  36.       
  37.     ReDraw();  
  38. }  


刪除:data更新,style更新,line更新
  1. void Document::DeleteRange(int begin, int end)  
  2. {  
  3.     data.DeleteRange(begin, end - begin + 1);  
  4.     Log();  
  5.     UpdateStyleManager_delete(begin, end);  
  6.     UpdateLineManager_delete(begin, end);  
  7. }  

这样,一个完整的富文本控件就被我们拆分成一个一个小块的零部件,然后又将每个零部件组装后合成一个完成的控件类,当然里面会用到的拷贝黏贴需要用到IDataObj组 件相关知识,我目前也只是实现了一个比较简单的、能用的clipboard data类,并实现了相关逻辑,需要更深入的研究才能搞到图文拷贝黏贴哇。 最后附上一个比较撮的介面和相关interface:
  1. struct Selection  
  2. {  
  3.     Selection():Begin_(-1), End_(-1)  
  4.     {}  
  5.   
  6.     Selection(int nBegin, int nEnd):Begin_(nBegin), End_(nEnd)  
  7.     {}  
  8.   
  9.     int Begin()  
  10.     {  
  11.         return Begin_;  
  12.     }  
  13.   
  14.     int End()  
  15.     {  
  16.         return End_;  
  17.     }  
  18.   
  19.     int GetSelectionLenght()  
  20.     {  
  21.         return (End_ - Begin_ + 1);  
  22.     }  
  23.   
  24.     bool IsSelected() const  
  25.     {  
  26.         if ( Begin_ >= 0 && End_ >= 0 && Begin_ <= End_ )  
  27.             return true;  
  28.         return false;  
  29.     }  
  30.   
  31.     bool IsBeSelect(int pos) const  
  32.     {  
  33.         if (pos <= End_ && pos >= Begin_)  
  34.             return true;  
  35.         return false;  
  36.     }  
  37.   
  38.     bool IsBeSelect(int begin, int end) const  
  39.     {  
  40.         if (begin <= End_ && end >= Begin_)  
  41.             return true;  
  42.         return false;  
  43.     }  
  44.   
  45.     void UpdateSelection(int pos)  
  46.     {  
  47.         if (pos <= Begin_)  
  48.         {  
  49.             End_ = Begin_;  
  50.             Begin_ = pos;  
  51.         }  
  52.         else  
  53.         {  
  54.             End_ = pos;  
  55.         }  
  56.     }  
  57.   
  58.     void ClearSelection()  
  59.     {  
  60.         Begin_ = -1;  
  61.         End_ = -1;  
  62.     }  
  63.   
  64.     void Log(MY_CHAR* name)  
  65.     {  
  66.         ATLTRACE("\n%s: Begin_:%d ; End_:%d;\n"  
  67.             , name  
  68.             , Begin_  
  69.             , End_);  
  70.     }  
  71.   
  72.     int Begin_;  
  73.     int End_;  
  74. };  
  75.   
  76. struct IBaseObj  
  77. {  
  78.     virtual int AddRef() = 0;  
  79.     virtual int Release() = 0;  
  80. };  
  81.   
  82. struct IObj : public IBaseObj  
  83. {  
  84.     virtual const SIZE* GetSize() const = 0;  
  85.     virtual void OnDraw(HDC dc, RECT& rcObj)  = 0;  
  86. };  
  87.   
  88. class ContextObj  
  89.     : public IObj  
  90. {  
  91. public:  
  92.     virtual const SIZE* GetSize() const  
  93.     {  
  94.         return &sz_;  
  95.     }  
  96. protected:  
  97.     SIZE    sz_;  
  98.   
  99. };  
  100.   
  101. class ImageObj  
  102.     : public ContextObj  
  103. {  
  104. public:  
  105.     ImageObj():obj_(NULL){}  
  106.   
  107.     void UpdatePos(int pos, int line)  
  108.     {  
  109.         posInLine_ = pos;  
  110.         ownLine_ = line;  
  111.     }  
  112.   
  113. protected:  
  114.     int posInLine_;  
  115.     int ownLine_;  
  116.     void* obj_;  
  117. };  
  118.   
  119. struct RowObjDetail  
  120. {  
  121.     RowObjDetail()  
  122.     {  
  123.         maxHeight_ = 0;  
  124.         pos_ = -1;  
  125.     }  
  126.     int maxHeight_;  
  127.     int pos_;  
  128. };  
  129.   
  130.   
  131. struct CaretDetail  
  132. {  
  133.     CaretDetail()  
  134.     {  
  135.         ZeroMemory(&pt, sizeof(pt));  
  136.         charPos_ = -1;  
  137.         line_ = -1;  
  138.         isLeft = true;  
  139.     }  
  140.   
  141.     CaretDetail& operator = (const CaretDetail& other)  
  142.     {  
  143.         charPos_ = other.charPos_;  
  144.         line_ = other.line_;  
  145.         pt = other.pt;  
  146.         isLeft = other.isLeft;  
  147.         return *this;  
  148.     }  
  149.   
  150.     bool IsEqual(const CaretDetail& other)  
  151.     {  
  152.         if (charPos_ == other.charPos_   
  153.             && line_ == other.line_  
  154.             && isLeft == other.isLeft)  
  155.         {  
  156.             return true;  
  157.         }  
  158.   
  159.         return false;  
  160.     }  
  161.     void Log(MY_CHAR* name)  
  162.     {  
  163.         ATLTRACE("\n%s: charPos_:%d ; line_:%d; pt:[x:%d, y:%d] isLeft:%d \n"  
  164.             , name  
  165.             , charPos_  
  166.             , line_  
  167.             , pt.x  
  168.             , pt.y  
  169.             , isLeft ? 1 : 0);  
  170.     }  
  171.   
  172.     int charPos_;  
  173.     int line_;  
  174.     POINT pt;  
  175.     bool isLeft;  
  176. };  
  177.   
  178. struct LinkObj  
  179.     : public IBaseObj  
  180. {  
  181.     int charPos_;  
  182.     int nRef_;  
  183.     MY_CHAR* context_;  
  184.   
  185.   
  186.     LinkObj(int charPos, MY_CHAR* context)  
  187.     {  
  188.         int len = lstrlen(context);  
  189.         context_ = new MY_CHAR[len + 1];  
  190.         lstrcpy(context_, context);  
  191.         charPos_ = charPos;  
  192.         nRef_ = 0;  
  193.         AddRef();  
  194.     }  
  195.   
  196.     ~LinkObj()  
  197.     {  
  198.         ClearContext();  
  199.     }  
  200.   
  201.     void ResetContext(MY_CHAR* context)  
  202.     {  
  203.         ClearContext();  
  204.         context_ = context;  
  205.     }  
  206.   
  207.     virtual int AddRef()  
  208.     {  
  209.         nRef_++;  
  210.         return nRef_;  
  211.     }  
  212.   
  213.     virtual int Release()  
  214.     {  
  215.         nRef_--;  
  216.         if (nRef_ == 0)  
  217.             delete this;  
  218.         return nRef_;  
  219.     }  
  220.   
  221. protected:  
  222.     void ClearContext()  
  223.     {  
  224.         if(context_)  
  225.             delete context_;  
  226.     }  
  227. };  


初步测算了一下,在本人的机子,相同条件下的插入34万个字符,notepad差不多用了5秒,word差不多用了3秒,这个用了9.203秒,慢慢优化估计可以接近notepad的水平。

附:优快云的图片上传太挫鸟,上传N次找不到图片到底上传了没有,这鸟设计太垃圾了,郁闷了我半天。  。  。 
PS:语文水平不行,写得让自己都感觉惭愧呀,欢迎拍砖交流~


源代码下载地址http://www.oschina.net/code/snippet_564149_10792


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值