PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/TeCom/src/Graphics/Source Files/TdkGdiLine.cpp

http://terra-printer.googlecode.com/
C++ | 328 lines | 194 code | 33 blank | 101 comment | 28 complexity | f45f7045c34df340ff8d84aa062a1888 MD5 | raw file
  1. #include <TdkGdiPoint.h>
  2. #include <TdkGdiLine.h>
  3. ///////////////////////////////////////////////////////////////////////////////
  4. //Constructor
  5. //Author : Rui Mauricio Gregório
  6. //Date : 08/2009
  7. ///////////////////////////////////////////////////////////////////////////////
  8. TdkGdiLine::TdkGdiLine()
  9. {
  10. _pointStyle=NULL;
  11. _lineDecorator=NULL;
  12. _pen=new Pen(Color(255,0,0));
  13. }
  14. ///////////////////////////////////////////////////////////////////////////////
  15. //Constructor
  16. //Author : Rui Mauricio Gregório
  17. //Date : 08/2009
  18. ///////////////////////////////////////////////////////////////////////////////
  19. TdkGdiLine::TdkGdiLine(TdkGdiPoint *pointStyle)
  20. {
  21. _pointStyle=pointStyle;
  22. _lineDecorator=NULL;
  23. _pen=new Pen(Color(255,0,0));
  24. }
  25. ///////////////////////////////////////////////////////////////////////////////
  26. //Destructor
  27. //Author : Rui Mauricio Gregório
  28. //Date : 08/2009
  29. ///////////////////////////////////////////////////////////////////////////////
  30. TdkGdiLine::~TdkGdiLine()
  31. {
  32. if(_pointStyle!=NULL) delete _pointStyle;
  33. if(_lineDecorator!=NULL) delete _lineDecorator;
  34. if(_pen!=NULL) delete _pen;
  35. }
  36. ///////////////////////////////////////////////////////////////////////////////
  37. //Sets Line Color
  38. //Author : Rui Mauricio Gregório
  39. //Date : 08/2009
  40. ///////////////////////////////////////////////////////////////////////////////
  41. void TdkGdiLine::setColor(const Color &color)
  42. {
  43. if(_pen!=NULL)
  44. {
  45. _pen->SetColor(color);
  46. }
  47. }
  48. ///////////////////////////////////////////////////////////////////////////////
  49. //Sets line type
  50. //Author : Rui Mauricio Gregório
  51. //Date : 08/2009
  52. ///////////////////////////////////////////////////////////////////////////////
  53. void TdkGdiLine::setType(const LineSettings::DashStyle &type)
  54. {
  55. _type=type;
  56. if(_pen!=NULL)
  57. {
  58. _pen->SetDashStyle((Gdiplus::DashStyle)_type);
  59. }
  60. }
  61. ///////////////////////////////////////////////////////////////////////////////
  62. //Sets line width
  63. //Author : Rui Mauricio Gregório
  64. //Date : 08/2009
  65. ///////////////////////////////////////////////////////////////////////////////
  66. void TdkGdiLine::setWidth(const REAL &width)
  67. {
  68. if(_pen!=NULL)
  69. {
  70. _pen->SetWidth(width);
  71. }
  72. }
  73. ///////////////////////////////////////////////////////////////////////////////
  74. //Sets Line Dash
  75. //Author : Rui Mauricio Gregório
  76. //Date : 08/2009
  77. ///////////////////////////////////////////////////////////////////////////////
  78. void TdkGdiLine::dash(double *dashArray, const int &size)
  79. {
  80. float *dash;
  81. int i;
  82. if(_pen!=NULL)
  83. {
  84. dash=new float[size];
  85. for(i=0;i<size;i++) dash[i]=(REAL)dashArray[i];
  86. _pen->SetDashStyle(Gdiplus::DashStyleCustom);
  87. _pen->SetDashPattern(dash,size);
  88. delete dash;
  89. }
  90. }
  91. ///////////////////////////////////////////////////////////////////////////////
  92. //Add Line Decorator
  93. //Author : Rui Mauricio Gregório
  94. //Date : 08/2009
  95. ///////////////////////////////////////////////////////////////////////////////
  96. void TdkGdiLine::add(TdkGdiLine* decorator)
  97. {
  98. if(_lineDecorator) delete _lineDecorator;
  99. _lineDecorator=decorator;
  100. }
  101. ///////////////////////////////////////////////////////////////////////////////
  102. //Draw a line
  103. //Author : Rui Mauricio Gregório
  104. //Date : 08/2009
  105. ///////////////////////////////////////////////////////////////////////////////
  106. void TdkGdiLine::draw(PointF *line, const int &size, Graphics *graphic,const bool &xorOption)
  107. {
  108. if((graphic!=NULL) && (_pen!=NULL))
  109. {
  110. if(xorOption) drawXorLine(graphic,line,size);
  111. else
  112. {
  113. graphic->DrawLines(_pen,line,size);
  114. if(_pointStyle)
  115. {
  116. int i;
  117. for(i=0;i<size;i++)
  118. {
  119. _pointStyle->draw(line[i].X,line[i].Y,graphic);
  120. }
  121. }
  122. }
  123. }
  124. }
  125. ///////////////////////////////////////////////////////////////////////////////
  126. //Draw a Line Path
  127. //Author : Rui Mauricio Gregório
  128. //Date : 08/2009
  129. ///////////////////////////////////////////////////////////////////////////////
  130. void TdkGdiLine::draw(GraphicsPath* path, Graphics* graphic)
  131. {
  132. if((graphic!=NULL) && (_pen!=NULL))
  133. {
  134. graphic->DrawPath(_pen,path);
  135. if(_pointStyle)
  136. {
  137. int pointCount;
  138. int i;
  139. PointF *points;
  140. pointCount=path->GetPointCount();
  141. points=new PointF[pointCount];
  142. path->GetPathPoints(points,pointCount);
  143. for(i=0;i<pointCount;i++)
  144. {
  145. _pointStyle->draw(points[i].X,points[i].Y,graphic);
  146. }
  147. delete []points;
  148. }
  149. }
  150. }
  151. ///////////////////////////////////////////////////////////////////////////////
  152. //Set Line Point Style
  153. //Author : Rui Mauricio Gregório
  154. //Date : 08/2009
  155. ///////////////////////////////////////////////////////////////////////////////
  156. void TdkGdiLine::setPointStyle(TdkGdiPoint *pointStyle)
  157. {
  158. if(_pointStyle) delete _pointStyle;
  159. _pointStyle=pointStyle;
  160. }
  161. ///////////////////////////////////////////////////////////////////////////////
  162. //Clear Line Point Style
  163. //Author : Rui Mauricio Gregório
  164. //Date : 08/2009
  165. ///////////////////////////////////////////////////////////////////////////////
  166. void TdkGdiLine::clearPointStyle()
  167. {
  168. if(_pointStyle!=NULL)
  169. {
  170. delete _pointStyle;
  171. _pointStyle=NULL;
  172. }
  173. }
  174. ///////////////////////////////////////////////////////////////////////////////
  175. //Clear Line Decorator
  176. //Author : Rui Mauricio Gregório
  177. //Date : 08/2009
  178. ///////////////////////////////////////////////////////////////////////////////
  179. void TdkGdiLine::clearDecorator()
  180. {
  181. if(_lineDecorator!=NULL)
  182. {
  183. delete _lineDecorator;
  184. _lineDecorator=NULL;
  185. }
  186. }
  187. ///////////////////////////////////////////////////////////////////////////////
  188. //Sets Line alpha
  189. //Author : Rui Mauricio Gregório
  190. //Date : 08/2009
  191. ///////////////////////////////////////////////////////////////////////////////
  192. void TdkGdiLine::setAlpha(const int &alpha)
  193. {
  194. if(_pen!=NULL)
  195. {
  196. Color color;
  197. _pen->GetColor(&color);
  198. _pen->SetColor(Color(color.GetR(),color.GetG(),color.GetB(),alpha));
  199. }
  200. }
  201. ///////////////////////////////////////////////////////////////////////////////
  202. //Draw a fill square
  203. //Author : Rui Mauricio Gregório
  204. //Date : 08/2009
  205. ///////////////////////////////////////////////////////////////////////////////
  206. void TdkGdiLine::draw(const double &x1, const double &y1, const double &x2, const double &y2,Graphics *graphic)
  207. {
  208. graphic->DrawLine(_pen,(REAL)x1,(REAL)y1,(REAL)x2,(REAL)y2);
  209. if(_pointStyle)
  210. {
  211. _pointStyle->draw(x1,y1,graphic);
  212. _pointStyle->draw(x2,y2,graphic);
  213. }
  214. }
  215. ///////////////////////////////////////////////////////////////////////////////
  216. //Draw a fill square
  217. //Author : Rui Mauricio Gregório
  218. //Date : 08/2009
  219. ///////////////////////////////////////////////////////////////////////////////
  220. Pen* TdkGdiLine::getPen()
  221. {
  222. return _pen;
  223. }
  224. ///////////////////////////////////////////////////////////////////////////////
  225. //Draw a fill square
  226. //Author : Rui Mauricio Gregório
  227. //Date : 08/2009
  228. ///////////////////////////////////////////////////////////////////////////////
  229. void TdkGdiLine::showPointLine(const bool &value)
  230. {
  231. if(value==true)
  232. {
  233. if(_pointStyle==NULL)
  234. {
  235. Color color;
  236. if(_pen) _pen->GetColor(&color);
  237. _pointStyle=new TdkGdiPoint();
  238. _pointStyle->setType(PointSettings::FILL_CIRCLE);
  239. _pointStyle->setSize(5);
  240. _pointStyle->setColor(color);
  241. }
  242. }else
  243. {
  244. if(_pointStyle!=NULL)
  245. {
  246. delete _pointStyle;
  247. _pointStyle=NULL;
  248. }
  249. }
  250. }
  251. ///////////////////////////////////////////////////////////////////////////////
  252. //Draw A Xor Line
  253. //Author : Rui Mauricio Gregório
  254. //Date : 08/2009
  255. ///////////////////////////////////////////////////////////////////////////////
  256. void TdkGdiLine::drawXorLine(Graphics* graphic, PointF *line, const int &size )
  257. {
  258. POINT *points=new POINT[size];
  259. int i;
  260. Gdiplus::Region region;
  261. HRGN rgn;
  262. graphic->GetClip(&region);
  263. rgn = region.GetHRGN(graphic);
  264. // Extract the Win32 HDC from the Graphics object supplied.
  265. HDC hdc = graphic->GetHDC();
  266. // Create a pen with a dotted style to draw the border of the
  267. // rectangle.
  268. HPEN gdiPen = CreatePen(PS_SOLID,1,RGB(128,128,128));
  269. SelectClipRgn(hdc,rgn);
  270. // Set the ROP cdrawint mode to XOR.
  271. SetROP2( hdc, R2_XORPEN );
  272. // Select the pen into the device context.
  273. HPEN oldPen =(HPEN) SelectObject( hdc, gdiPen );
  274. // Create a stock NULL_BRUSH brush and select it into the device
  275. // context so that the rectangle isn't filled.
  276. HBRUSH oldBrush =(HBRUSH) SelectObject( hdc, GetStockObject( NULL_BRUSH ) );
  277. for(i=0;i<size;i++)
  278. {
  279. points[i].x=(long)line[i].X;
  280. points[i].y=(long)line[i].Y;
  281. }
  282. // Now XOR the hollow rectangle on the Graphics object with
  283. // a dotted outline.
  284. MoveToEx(hdc,points[0].x,points[0].y,(LPPOINT) NULL);
  285. PolylineTo(hdc,points,size);
  286. delete points;
  287. // Put the old stuff back where it was.
  288. SelectObject( hdc, oldBrush ); // no need to delete a stock object
  289. SelectObject( hdc, oldPen );
  290. DeleteObject( gdiPen ); // but we do need to delete the pen
  291. DeleteObject(rgn);
  292. SelectClipRgn(hdc,0);
  293. // Return the device context to Windows.
  294. graphic->ReleaseHDC(hdc);
  295. }