Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
7.6 KiB

  1. /*
  2. * @doc INTERNAL
  3. *
  4. * @module _RUNPTR.H -- Text run and run pointer class defintion |
  5. *
  6. * Original Author: <nl>
  7. * Christian Fortini
  8. *
  9. * History: <nl>
  10. * 6/25/95 alexgo Commenting and Cleanup
  11. *
  12. * Copyright (c) 1995-2000 Microsoft Corporation. All rights reserved.
  13. */
  14. #ifndef _RUNPTR_H
  15. #define _RUNPTR_H
  16. #include "_array.h"
  17. #include "_doc.h"
  18. typedef CArray<CTxtRun> CRunArray;
  19. /*
  20. * CRunPtrBase
  21. *
  22. * @class Base run pointer functionality. Keeps a position within an array
  23. * of text runs.
  24. *
  25. * @devnote Run pointers go through three different possible states :
  26. *
  27. * NULL: there is no data and no array (frequently a startup condition) <nl>
  28. * <mf CRunPtrBase::SetRunArray> will transition from this state to
  29. * the Empty state. It is typically up to the derived class to
  30. * define when that method should be called. IsValid() fails. <nl>
  31. *
  32. * <md CRunPtrBase::_pRuns> == NULL <nl>
  33. * <md CRunPtrBase::_iRun> == 0 <nl>
  34. * <md CRunPtrBase::_ich> == 0 <nl>
  35. *
  36. * Empty: an array class exists, but there is no data (can happen if all
  37. * of the elements in the array are deleted). IsValid() fails.<nl>
  38. * <md CRunPtrBase::_pRuns> != NULL <nl>
  39. * <md CRunPtrBase::_iRun> == 0 <nl>
  40. * <md CRunPtrBase::_ich> <gt>= 0 <nl>
  41. * <md CRunPtrBase::_pRuns-<gt>Count()> == 0 <nl>
  42. *
  43. * Normal: the array class exists and has data; IsValid() succeeds and
  44. * <md CRunPtrBase::_pRuns-<gt>Elem[] is defined <nl>
  45. * <md CRunPtrBase::_pRuns> != NULL <nl>
  46. * <md CRunPtrBase::_iRun> >= 0 <nl>
  47. * <md CRunPtrBase::_ich> >= 0 <nl>
  48. * <md _pRuns>-<gt>Count() > 0 <nl>
  49. *
  50. * Note that in order to support the empty and normal states, the actual
  51. * array element at <md CRunPtrBase::_iRun> must be explicitly fetched in
  52. * any method that may need it.
  53. *
  54. * Currently, there is no way to transition to the NULL state from any of
  55. * the other states. If we needed to, we could support that by explicitly
  56. * fetching the array from the document on demand.
  57. *
  58. * Note that only <md CRunPtrBase::_iRun> is kept. We could also keep
  59. * a pointer to the actual run (i.e. _pRun). Earlier versions of this
  60. * engine did in fact do this. I've opted to not do this for several
  61. * reasons: <nl>
  62. * 1. If IsValid(), _pRun is *always* available by calling Elem(_iRun).
  63. * Therefore, there is nominally no need to keep both _iRun and _pRun.<nl>
  64. * 2. Run pointers are typically used to either just move around
  65. * and then fetch data or move and fetch data every time (like during
  66. * a measuring loop). In the former case, there is no need to always
  67. * bind _pRun; you can just do it on demand. In the latter case, the
  68. * two models are equivalent.
  69. */
  70. class CRunPtrBase
  71. {
  72. friend class CDisplayML;
  73. friend class CDisplaySL;
  74. //@access Public methods
  75. public:
  76. #ifdef DEBUG
  77. BOOL Invariant() const; //@cmember Invariant tests
  78. void ValidatePtr(void *pRun) const; //@cmember Validate <p pRun>
  79. LONG CalcTextLength() const; //@cmember Get total cch in runs
  80. #define VALIDATE_PTR(pRun) ValidatePtr(pRun)
  81. #else
  82. #define VALIDATE_PTR(pRun)
  83. #endif // DEBUG
  84. CRunPtrBase(CRunArray *pRuns); //@cmember Constructor
  85. CRunPtrBase(CRunPtrBase& rp); //@cmember Constructor
  86. // Run Control
  87. void SetRunArray(CRunArray *pRuns) //@cmember Set run array for this
  88. { // run ptr
  89. _pRuns = pRuns;
  90. }
  91. BOOL SetRun(LONG iRun, LONG ich); //@cmember Set this runptr to run
  92. // <p iRun> & char offset <p ich>
  93. BOOL NextRun(); //@cmember Advance to next run
  94. BOOL PrevRun(); //@cmember Go back to prev run
  95. BOOL ChgRun(LONG cRun) //@cmember Move <p cRun> runs
  96. { // returning TRUE if successful
  97. return SetRun(_iRun + cRun, 0);
  98. }
  99. //@cmember Count <p cRun> runs
  100. LONG CountRuns(LONG &cRun, // returning cch counted and
  101. LONG cchMax, // updating <p cRun>
  102. LONG cp,
  103. LONG cchText) const;
  104. //@cmember Find run range limits
  105. void FindRun (LONG *pcpMin,
  106. LONG *pcpMost, LONG cpMin, LONG cch, LONG cchText) const;
  107. CTxtRun * GetRun(LONG cRun) const; //@cmember Retrieve run element at
  108. // offset <p cRun> from this run
  109. LONG Count() const //@cmember Get count of runs
  110. {
  111. return _pRuns->Count();
  112. }
  113. BOOL SameRuns(CRunPtrBase *prp) //@cmember Return TRUE iff same runs
  114. {
  115. return _pRuns == prp->_pRuns;
  116. }
  117. BOOL SameRun(CRunPtrBase *prp)
  118. {
  119. return SameRuns(prp) && _iRun == prp->_iRun;
  120. }
  121. // Character position control
  122. //@cmember Set cp for this run ptr = <p cp>
  123. LONG BindToCp(LONG cp, LONG cchText = tomForward);
  124. LONG CalculateCp() const;//@cmember Add _cch's up to _iRun, _ich
  125. LONG Move(LONG cch); //@cmember Move cp by <p cch> chars
  126. void AdjustBackward(); //@cmember If on the edge of two runs,
  127. // adjust to end of left (previous) run
  128. void AdjustForward(); //@cmember If at the edge of two runs,
  129. // adjust to start of right (next) run
  130. LONG GetIch() const //@cmember Return <md CRunPtrBase::_ich>
  131. {Assert(IsValid()); return _ich;}
  132. LONG GetIRun() const //@cmember Return <md CRunPtrBase::_iRun>
  133. {Assert(IsValid()); return _iRun;}
  134. void SetIch(LONG ich) //@cmember Set <md CRunPtrBase::_ich>
  135. {Assert(IsValid()); _ich = ich;}
  136. LONG GetCchLeft() const; //@cmember Return GetRun(0)->_cch - GetIch()
  137. inline BOOL IsValid() const //@cmember Return FALSE if run ptr is in
  138. { // empty or NULL states. TRUE otherwise
  139. return _pRuns && _pRuns->Count();
  140. }
  141. void SetToNull(); //@cmember Clears data from run pointer
  142. //@access Protected Data
  143. protected:
  144. CRunArray * _pRuns; //@cmember Pointer to CTxtRun array
  145. LONG _iRun; //@cmember Index of current run in array
  146. LONG _ich; //@cmember Char offset inside current run
  147. };
  148. /*
  149. * CRunPtr (template)
  150. *
  151. * @class a template over CRunPtrBase allowing for type-safe versions of
  152. * run pointers
  153. *
  154. * @tcarg class | CElem | run array class to be used
  155. *
  156. * @base public | CRunPtrBase
  157. */
  158. template <class CElem>
  159. class CRunPtr : public CRunPtrBase
  160. {
  161. public:
  162. CRunPtr (void) //@cmember Constructor
  163. : CRunPtrBase (0) {}
  164. CRunPtr (CRunArray *pRuns) //@cmember Constructor
  165. : CRunPtrBase (pRuns) {}
  166. CRunPtr (CRunPtrBase& rp) //@cmember Constructor
  167. : CRunPtrBase (rp) {}
  168. // Array management
  169. CElem * Add (LONG cRun, LONG *pielIns) //@cmember Add <p cRun>
  170. { // elements at end of array
  171. return (CElem *)_pRuns->Add(cRun, pielIns);
  172. }
  173. CElem * Insert (LONG cRun) //@cmember Insert <p cRun>
  174. { // elements at current pos
  175. return (CElem *)_pRuns->Insert(_iRun, cRun);
  176. }
  177. void Remove (LONG cRun) //@cmember Remove <p cRun>
  178. { // elements at current pos
  179. _pRuns->Remove (_iRun, cRun);
  180. }
  181. //@cmember Replace <p cRun> elements
  182. // at current position with those
  183. // from <p parRun>
  184. BOOL Replace (LONG cRun, CArrayBase *parRun)
  185. {
  186. return _pRuns->Replace(_iRun, cRun, parRun);
  187. }
  188. CElem * Elem(LONG iRun) const //@cmember Get ptr to run <p iRun>
  189. {
  190. return (CElem *)_pRuns->Elem(iRun);
  191. }
  192. CElem * GetRun(LONG cRun) const //@cmember Get ptr <p cRun> runs
  193. { // away from current run
  194. return Elem(_iRun + cRun);
  195. }
  196. void IncPtr(CElem *&pRun) const //@cmember Increment ptr <p pRun>
  197. {
  198. VALIDATE_PTR(pRun); // Allow invalid ptr after ++ for
  199. pRun++; // for loops
  200. }
  201. CElem * GetPtr(CElem *pRun, LONG cRun) const//@cmember Get ptr <p cRun>
  202. { // runs away from ptr <p pRun>
  203. VALIDATE_PTR(pRun + cRun);
  204. return pRun + cRun;
  205. }
  206. };
  207. #endif