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.

338 lines
7.7 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999.
  5. //
  6. // File: brview.hxx
  7. //
  8. // Contents:
  9. //
  10. // History: 15 Aug 1996 DLee Created
  11. //
  12. //--------------------------------------------------------------------------
  13. #pragma once
  14. struct CharDim
  15. {
  16. int cxChar;
  17. int cyChar;
  18. };
  19. // Use for painting other than WM_PAINT
  20. class DeviceContext
  21. {
  22. public:
  23. DeviceContext ( HWND hwnd ): _hwnd(hwnd)
  24. {
  25. _hdc = GetDC ( hwnd );
  26. }
  27. ~DeviceContext ()
  28. {
  29. ReleaseDC ( _hwnd, _hdc );
  30. }
  31. protected:
  32. HWND _hwnd;
  33. HDC _hdc;
  34. };
  35. class TextMetrics: public DeviceContext
  36. {
  37. public:
  38. TextMetrics ( HWND hwnd, HFONT hFont ): DeviceContext(hwnd)
  39. {
  40. HFONT hOldFont = (HFONT) SelectObject ( _hdc, hFont );
  41. GetTextMetrics ( _hdc, &_tm );
  42. SelectObject( _hdc, hOldFont );
  43. }
  44. void GetSizes ( CharDim& dim );
  45. private:
  46. TEXTMETRIC _tm;
  47. };
  48. class Layout
  49. {
  50. public:
  51. Layout () {}
  52. ~Layout () {}
  53. // Screen layout methods
  54. void Init ( Model *pModel )
  55. {
  56. _pModel = pModel;
  57. }
  58. void SetCharSizes (TextMetrics& tm, HFONT hFont)
  59. {
  60. _hFont = hFont;
  61. tm.GetSizes ( _dim );
  62. }
  63. int CxChar() const { return _dim.cxChar; }
  64. int CyChar() const { return _dim.cyChar; }
  65. int XBegin () const { return _xBegin; }
  66. int MaxLenChar () const { return _cCharWidth; }
  67. int MaxLines () const { return _cLine; }
  68. int Y (int line ) const;
  69. // Paragraph layout methods
  70. void SetParaRange (int cParas);
  71. void Adjust (int cx, int cy, int& paraVScroll, int& paLineVScroll, int nHsrcollPos);
  72. void Locate (int para, int paOff, int& paLine, int& paOffBeg, int& paOffEnd ) const;
  73. BOOL GetLineOffsets (int para, int paLine, int& paOffBeg, int& paOffEnd) const;
  74. int LinesInPara (int para) const;
  75. int LastLineInPara (int para) { return LinesInPara(para) - 1; }
  76. int LastPara() const;
  77. int FirstPara() const { return _paraFirst; }
  78. int FirstLineInPara() const { return _paLineFirst; }
  79. int LineNumber (int para, int paLine) const;
  80. HFONT Font() { return _hFont; }
  81. Model & GetModel() { return * _pModel; }
  82. private:
  83. CharDim _dim;
  84. int _xBegin;
  85. int _cCharWidth; // window width in chars
  86. int _cLine;
  87. // Paragraph layout
  88. int _cParas;
  89. // Top of the window
  90. int _paraFirst;
  91. int _paLineFirst;
  92. HFONT _hFont;
  93. Model * _pModel;
  94. ParaLine const* _aParaLine;
  95. };
  96. class Selection
  97. {
  98. public:
  99. Selection() :
  100. _paraStart( -1 ),
  101. _oStart( 0 ),
  102. _paraEnd( -1 ),
  103. _oEnd( 0 ) {}
  104. Selection( Selection &s ) :
  105. _paraStart( s._paraStart ),
  106. _oStart( s._oStart ),
  107. _paraEnd( s._paraEnd ),
  108. _oEnd( s._oEnd ) {}
  109. void None() { _paraStart = -1; _paraEnd = -1; }
  110. BOOL SelectionExists() { return _paraStart != -1 && _paraEnd != -1; }
  111. BOOL IsNull() { return _paraStart == _paraEnd &&
  112. _oStart == _oEnd; }
  113. BOOL IsInOnePara() { return _paraStart == _paraEnd; }
  114. BOOL IsInSelection( int para )
  115. {
  116. return ( para >= _paraStart &&
  117. para <= _paraEnd );
  118. }
  119. BOOL IsInSelection( int para, int c )
  120. {
  121. BOOL fSel = FALSE;
  122. if ( para == _paraStart && para == _paraEnd )
  123. fSel = ( c >= _oStart && c < _oEnd );
  124. else if ( para == _paraStart )
  125. fSel = ( c >= _oStart );
  126. else if ( para == _paraEnd )
  127. fSel = ( c < _oEnd );
  128. return fSel;
  129. }
  130. void Set( int ps, int os, int pe, int oe )
  131. {
  132. _paraStart = ps;
  133. _oStart = os;
  134. _paraEnd = pe;
  135. _oEnd = oe;
  136. }
  137. void SetStart( int ps, int os )
  138. {
  139. _paraStart = ps;
  140. _oStart = os;
  141. }
  142. void SetEnd( int pe, int oe )
  143. {
  144. _paraEnd = pe;
  145. _oEnd = oe;
  146. }
  147. int ParaStart() { return _paraStart; }
  148. int OffStart() { return _oStart; }
  149. int ParaEnd() { return _paraEnd; }
  150. int OffEnd() { return _oEnd; }
  151. private:
  152. int _paraStart;
  153. int _oStart;
  154. int _paraEnd;
  155. int _oEnd;
  156. };
  157. class View
  158. {
  159. public:
  160. View() : _cyClient( 0 ), _fDblClk( 0 ), _fSelecting(FALSE) {}
  161. void Init ( HWND hwnd, Model *pModel, HFONT hFont )
  162. {
  163. _hwnd = hwnd;
  164. _nHScrollPos = 0;
  165. _pModel = pModel;
  166. _layout.Init( pModel );
  167. FontChange ( hwnd, hFont );
  168. }
  169. void ButtonUp( WPARAM wParam, LPARAM lParam );
  170. void ButtonDown( WPARAM wParam, LPARAM lParam );
  171. void DblClk( WPARAM wParam, LPARAM lParam );
  172. void MouseMove( WPARAM wParam, LPARAM lParam );
  173. void EditCopy( HWND hwnd, WPARAM wParam );
  174. void FontChange ( HWND hwnd, HFONT hFont )
  175. {
  176. TextMetrics tm(hwnd, hFont);
  177. _layout.SetCharSizes( tm, hFont );
  178. }
  179. void Paint (HWND hwnd);
  180. void Size ( int cx, int cy );
  181. void Home ()
  182. {
  183. _fFullSelRepaint = TRUE;
  184. _paraVScroll = 0;
  185. _paLineVScroll = 0;
  186. }
  187. void End ()
  188. {
  189. _fFullSelRepaint = TRUE;
  190. _paraVScroll = _paraVScrollMax;
  191. _paLineVScroll = _paLineVScrollMax;
  192. }
  193. int LineHeight () const { return _layout.CyChar(); }
  194. int CharWidth () const { return _layout.CxChar(); }
  195. int VScrollPara () const { return _paraVScroll; }
  196. int HScrollPos () const { return _nHScrollPos; }
  197. int VScrollMax () const { return _paraVScrollMax; }
  198. int HScrollMax () const { return _nHScrollMax; }
  199. void SetRange ( int maxParaLen, int maxParas );
  200. void SetScroll( Position & pos );
  201. int VisibleLines () const { return _cyClient / _layout.CyChar(); }
  202. int FirstPara() const { return _layout.FirstPara(); }
  203. int IncVScrollPos ( int delta );
  204. int JumpToPara ( int para );
  205. int IncHScrollPos ( int delta );
  206. void SetScrollMax ();
  207. Selection & GetSelection() { return _Selection; }
  208. int ParaFromY( int y );
  209. private:
  210. void _NoSelection();
  211. void GetParaAndOffset( int x,
  212. int y,
  213. int & para,
  214. int & offset );
  215. void _UpdateSelection( LPARAM lParam );
  216. // dimensions of client window
  217. int _cxClient;
  218. int _cyClient;
  219. // scroll position
  220. int _paraVScroll; // paragraph
  221. int _paLineVScroll; // line within paragraph
  222. int _paraVScrollMax;
  223. int _paLineVScrollMax;
  224. int _nHScrollPos;
  225. int _nHScrollMax;
  226. Layout _layout;
  227. Model * _pModel;
  228. Selection _Selection;
  229. BOOL _fDblClk;
  230. BOOL _fStartIsAnchor;
  231. BOOL _fSelecting;
  232. BOOL _fFullSelRepaint;
  233. int _cpLastSelY;
  234. HWND _hwnd;
  235. };
  236. // Use for painting after WM_PAINT
  237. class Paint: public tagPAINTSTRUCT
  238. {
  239. public:
  240. Paint ( HWND hwnd );
  241. ~Paint ();
  242. protected:
  243. HWND _hwnd;
  244. };
  245. inline Paint::Paint ( HWND hwnd ): _hwnd(hwnd)
  246. {
  247. BeginPaint ( _hwnd, this );
  248. }
  249. inline Paint::~Paint()
  250. {
  251. EndPaint( _hwnd, this );
  252. }
  253. class PaintText: public Paint
  254. {
  255. public:
  256. PaintText( HWND hwnd, int paraFirst, int paLineFirst, Layout& layout,
  257. Selection & Selection );
  258. ~PaintText();
  259. void PrintLines ();
  260. void HiliteHits ();
  261. void HiliteSelection ();
  262. private:
  263. void PrintCurrentHit( BOOL fCurrent );
  264. int _paraFirst;
  265. int _paLineFirst;
  266. Layout& _layout;
  267. HFONT _hOldFont;
  268. Selection & _Selection;
  269. };