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.

840 lines
27 KiB

  1. /**************************************************************************\
  2. *
  3. * Copyright (c) 1998-2001, Microsoft Corp. All Rights Reserved.
  4. *
  5. * Module Name:
  6. *
  7. * GdiplusBrush.h
  8. *
  9. * Abstract:
  10. *
  11. * GDI+ Brush class
  12. *
  13. \**************************************************************************/
  14. #ifndef _GDIPLUSBRUSH_H
  15. #define _GDIPLUSBRUSH_H
  16. class GraphicsPath;
  17. //--------------------------------------------------------------------------
  18. // Abstract base class for various brush types
  19. //--------------------------------------------------------------------------
  20. class Brush : public GdiplusBase
  21. {
  22. public:
  23. friend class Pen;
  24. friend class Graphics;
  25. virtual ~Brush()
  26. {
  27. DllExports::GdipDeleteBrush(nativeBrush);
  28. }
  29. virtual Brush* Clone() const
  30. {
  31. GpBrush *brush = NULL;
  32. SetStatus(DllExports::GdipCloneBrush(nativeBrush, &brush));
  33. Brush *newBrush = new Brush(brush, lastResult);
  34. if (newBrush == NULL)
  35. {
  36. DllExports::GdipDeleteBrush(brush);
  37. }
  38. return newBrush;
  39. }
  40. BrushType GetType() const
  41. {
  42. BrushType type = static_cast<BrushType>(-1);
  43. SetStatus(DllExports::GdipGetBrushType(nativeBrush, &type));
  44. return type;
  45. }
  46. Status GetLastStatus() const
  47. {
  48. Status lastStatus = lastResult;
  49. lastResult = Ok;
  50. return lastStatus;
  51. }
  52. protected:
  53. Brush()
  54. {
  55. SetStatus(NotImplemented);
  56. }
  57. private:
  58. Brush(const Brush& brush);
  59. Brush& operator=(const Brush& brush);
  60. protected:
  61. Brush(GpBrush* nativeBrush, Status status)
  62. {
  63. lastResult = status;
  64. SetNativeBrush(nativeBrush);
  65. }
  66. VOID SetNativeBrush(GpBrush* nativeBrush)
  67. {
  68. this->nativeBrush = nativeBrush;
  69. }
  70. Status SetStatus(Status status) const
  71. {
  72. if (status != Ok)
  73. return (lastResult = status);
  74. else
  75. return status;
  76. }
  77. GpBrush* nativeBrush;
  78. mutable Status lastResult;
  79. };
  80. //--------------------------------------------------------------------------
  81. // Solid Fill Brush Object
  82. //--------------------------------------------------------------------------
  83. class SolidBrush : public Brush
  84. {
  85. public:
  86. friend class Pen;
  87. SolidBrush(IN const Color& color)
  88. {
  89. GpSolidFill *brush = NULL;
  90. lastResult = DllExports::GdipCreateSolidFill(color.GetValue(), &brush);
  91. SetNativeBrush(brush);
  92. }
  93. Status GetColor(OUT Color* color) const
  94. {
  95. ARGB argb;
  96. if (color == NULL)
  97. {
  98. return SetStatus(InvalidParameter);
  99. }
  100. SetStatus(DllExports::GdipGetSolidFillColor((GpSolidFill*)nativeBrush,
  101. &argb));
  102. *color = Color(argb);
  103. return lastResult;
  104. }
  105. Status SetColor(IN const Color& color)
  106. {
  107. return SetStatus(DllExports::GdipSetSolidFillColor((GpSolidFill*)nativeBrush,
  108. color.GetValue()));
  109. }
  110. private:
  111. SolidBrush(const SolidBrush &);
  112. SolidBrush& operator=(const SolidBrush &);
  113. protected:
  114. SolidBrush()
  115. {
  116. }
  117. };
  118. //--------------------------------------------------------------------------
  119. // Texture Brush Fill Object
  120. //--------------------------------------------------------------------------
  121. class TextureBrush : public Brush
  122. {
  123. public:
  124. friend class Pen;
  125. TextureBrush(IN Image* image,
  126. IN WrapMode wrapMode = WrapModeTile)
  127. {
  128. GpTexture *texture = NULL;
  129. lastResult = DllExports::GdipCreateTexture(
  130. image->nativeImage,
  131. wrapMode, &texture);
  132. SetNativeBrush(texture);
  133. }
  134. // When creating a texture brush from a metafile image, the dstRect
  135. // is used to specify the size that the metafile image should be
  136. // rendered at in the device units of the destination graphics.
  137. // It is NOT used to crop the metafile image, so only the width
  138. // and height values matter for metafiles.
  139. TextureBrush(IN Image* image,
  140. IN WrapMode wrapMode,
  141. IN const RectF &dstRect)
  142. {
  143. GpTexture *texture = NULL;
  144. lastResult = DllExports::GdipCreateTexture2(
  145. image->nativeImage,
  146. wrapMode,
  147. dstRect.X,
  148. dstRect.Y,
  149. dstRect.Width,
  150. dstRect.Height,
  151. &texture);
  152. SetNativeBrush(texture);
  153. }
  154. TextureBrush(IN Image *image,
  155. IN const RectF &dstRect,
  156. IN const ImageAttributes *imageAttributes = NULL)
  157. {
  158. GpTexture *texture = NULL;
  159. lastResult = DllExports::GdipCreateTextureIA(
  160. image->nativeImage,
  161. (imageAttributes)?imageAttributes->nativeImageAttr:NULL,
  162. dstRect.X,
  163. dstRect.Y,
  164. dstRect.Width,
  165. dstRect.Height,
  166. &texture
  167. );
  168. SetNativeBrush(texture);
  169. }
  170. TextureBrush(IN Image *image,
  171. IN const Rect &dstRect,
  172. IN const ImageAttributes *imageAttributes = NULL)
  173. {
  174. GpTexture *texture = NULL;
  175. lastResult = DllExports::GdipCreateTextureIAI(
  176. image->nativeImage,
  177. (imageAttributes)?imageAttributes->nativeImageAttr:NULL,
  178. dstRect.X,
  179. dstRect.Y,
  180. dstRect.Width,
  181. dstRect.Height,
  182. &texture
  183. );
  184. SetNativeBrush(texture);
  185. }
  186. TextureBrush(
  187. IN Image* image,
  188. IN WrapMode wrapMode,
  189. const IN Rect &dstRect
  190. )
  191. {
  192. GpTexture *texture = NULL;
  193. lastResult = DllExports::GdipCreateTexture2I(
  194. image->nativeImage,
  195. wrapMode,
  196. dstRect.X,
  197. dstRect.Y,
  198. dstRect.Width,
  199. dstRect.Height,
  200. &texture);
  201. SetNativeBrush(texture);
  202. }
  203. TextureBrush(IN Image* image,
  204. IN WrapMode wrapMode,
  205. IN REAL dstX,
  206. IN REAL dstY,
  207. IN REAL dstWidth,
  208. IN REAL dstHeight)
  209. {
  210. GpTexture *texture = NULL;
  211. lastResult = DllExports::GdipCreateTexture2(
  212. image->nativeImage,
  213. wrapMode,
  214. dstX,
  215. dstY,
  216. dstWidth,
  217. dstHeight,
  218. &texture);
  219. SetNativeBrush(texture);
  220. }
  221. TextureBrush(IN Image* image,
  222. IN WrapMode wrapMode,
  223. IN INT dstX,
  224. IN INT dstY,
  225. IN INT dstWidth,
  226. IN INT dstHeight)
  227. {
  228. GpTexture *texture = NULL;
  229. lastResult = DllExports::GdipCreateTexture2I(
  230. image->nativeImage,
  231. wrapMode,
  232. dstX,
  233. dstY,
  234. dstWidth,
  235. dstHeight,
  236. &texture);
  237. SetNativeBrush(texture);
  238. }
  239. Status SetTransform(IN const Matrix* matrix)
  240. {
  241. return SetStatus(DllExports::GdipSetTextureTransform((GpTexture*)nativeBrush,
  242. matrix->nativeMatrix));
  243. }
  244. Status GetTransform(OUT Matrix* matrix) const
  245. {
  246. return SetStatus(DllExports::GdipGetTextureTransform((GpTexture*)nativeBrush,
  247. matrix->nativeMatrix));
  248. }
  249. Status ResetTransform()
  250. {
  251. return SetStatus(DllExports::GdipResetTextureTransform((GpTexture*)nativeBrush));
  252. }
  253. Status MultiplyTransform(IN const Matrix* matrix,
  254. IN MatrixOrder order = MatrixOrderPrepend)
  255. {
  256. return SetStatus(DllExports::GdipMultiplyTextureTransform((GpTexture*)nativeBrush,
  257. matrix->nativeMatrix,
  258. order));
  259. }
  260. Status TranslateTransform(IN REAL dx,
  261. IN REAL dy,
  262. IN MatrixOrder order = MatrixOrderPrepend)
  263. {
  264. return SetStatus(DllExports::GdipTranslateTextureTransform((GpTexture*)nativeBrush,
  265. dx, dy, order));
  266. }
  267. Status ScaleTransform(IN REAL sx,
  268. IN REAL sy,
  269. IN MatrixOrder order = MatrixOrderPrepend)
  270. {
  271. return SetStatus(DllExports::GdipScaleTextureTransform((GpTexture*)nativeBrush,
  272. sx, sy, order));
  273. }
  274. Status RotateTransform(IN REAL angle,
  275. IN MatrixOrder order = MatrixOrderPrepend)
  276. {
  277. return SetStatus(DllExports::GdipRotateTextureTransform((GpTexture*)nativeBrush,
  278. angle, order));
  279. }
  280. Status SetWrapMode(IN WrapMode wrapMode)
  281. {
  282. return SetStatus(DllExports::GdipSetTextureWrapMode((GpTexture*)nativeBrush,
  283. wrapMode));
  284. }
  285. WrapMode GetWrapMode() const
  286. {
  287. WrapMode wrapMode;
  288. SetStatus(DllExports::GdipGetTextureWrapMode((GpTexture*)nativeBrush,
  289. &wrapMode));
  290. return wrapMode;
  291. }
  292. Image *GetImage() const
  293. {
  294. GpImage *image;
  295. SetStatus(DllExports::GdipGetTextureImage((GpTexture *)nativeBrush,
  296. &image));
  297. Image *retimage = new Image(image, lastResult);
  298. if (retimage == NULL)
  299. {
  300. DllExports::GdipDisposeImage(image);
  301. }
  302. return retimage;
  303. }
  304. private:
  305. TextureBrush(const TextureBrush &);
  306. TextureBrush& operator=(const TextureBrush &);
  307. protected:
  308. TextureBrush()
  309. {
  310. }
  311. };
  312. //--------------------------------------------------------------------------
  313. // Linear Gradient Brush Object
  314. //--------------------------------------------------------------------------
  315. class LinearGradientBrush : public Brush
  316. {
  317. public:
  318. friend class Pen;
  319. LinearGradientBrush(IN const PointF& point1,
  320. IN const PointF& point2,
  321. IN const Color& color1,
  322. IN const Color& color2)
  323. {
  324. GpLineGradient *brush = NULL;
  325. lastResult = DllExports::GdipCreateLineBrush(&point1,
  326. &point2,
  327. color1.GetValue(),
  328. color2.GetValue(),
  329. WrapModeTile,
  330. &brush);
  331. SetNativeBrush(brush);
  332. }
  333. LinearGradientBrush(IN const Point& point1,
  334. IN const Point& point2,
  335. IN const Color& color1,
  336. IN const Color& color2)
  337. {
  338. GpLineGradient *brush = NULL;
  339. lastResult = DllExports::GdipCreateLineBrushI(&point1,
  340. &point2,
  341. color1.GetValue(),
  342. color2.GetValue(),
  343. WrapModeTile,
  344. &brush);
  345. SetNativeBrush(brush);
  346. }
  347. LinearGradientBrush(IN const RectF& rect,
  348. IN const Color& color1,
  349. IN const Color& color2,
  350. IN LinearGradientMode mode)
  351. {
  352. GpLineGradient *brush = NULL;
  353. lastResult = DllExports::GdipCreateLineBrushFromRect(&rect,
  354. color1.GetValue(),
  355. color2.GetValue(),
  356. mode,
  357. WrapModeTile,
  358. &brush);
  359. SetNativeBrush(brush);
  360. }
  361. LinearGradientBrush(IN const Rect& rect,
  362. IN const Color& color1,
  363. IN const Color& color2,
  364. IN LinearGradientMode mode)
  365. {
  366. GpLineGradient *brush = NULL;
  367. lastResult = DllExports::GdipCreateLineBrushFromRectI(&rect,
  368. color1.GetValue(),
  369. color2.GetValue(),
  370. mode,
  371. WrapModeTile,
  372. &brush);
  373. SetNativeBrush(brush);
  374. }
  375. LinearGradientBrush(IN const RectF& rect,
  376. IN const Color& color1,
  377. IN const Color& color2,
  378. IN REAL angle,
  379. IN BOOL isAngleScalable = FALSE)
  380. {
  381. GpLineGradient *brush = NULL;
  382. lastResult = DllExports::GdipCreateLineBrushFromRectWithAngle(&rect,
  383. color1.GetValue(),
  384. color2.GetValue(),
  385. angle,
  386. isAngleScalable,
  387. WrapModeTile,
  388. &brush);
  389. SetNativeBrush(brush);
  390. }
  391. LinearGradientBrush(IN const Rect& rect,
  392. IN const Color& color1,
  393. IN const Color& color2,
  394. IN REAL angle,
  395. IN BOOL isAngleScalable = FALSE)
  396. {
  397. GpLineGradient *brush = NULL;
  398. lastResult = DllExports::GdipCreateLineBrushFromRectWithAngleI(&rect,
  399. color1.GetValue(),
  400. color2.GetValue(),
  401. angle,
  402. isAngleScalable,
  403. WrapModeTile,
  404. &brush);
  405. SetNativeBrush(brush);
  406. }
  407. Status SetLinearColors(IN const Color& color1,
  408. IN const Color& color2)
  409. {
  410. return SetStatus(DllExports::GdipSetLineColors((GpLineGradient*)nativeBrush,
  411. color1.GetValue(),
  412. color2.GetValue()));
  413. }
  414. Status GetLinearColors(OUT Color* colors) const
  415. {
  416. ARGB argb[2];
  417. if (colors == NULL)
  418. {
  419. return SetStatus(InvalidParameter);
  420. }
  421. SetStatus(DllExports::GdipGetLineColors((GpLineGradient*) nativeBrush, argb));
  422. if (lastResult == Ok)
  423. {
  424. // use bitwise copy operator for Color copy
  425. colors[0] = Color(argb[0]);
  426. colors[1] = Color(argb[1]);
  427. }
  428. return lastResult;
  429. }
  430. Status GetRectangle(OUT RectF* rect) const
  431. {
  432. return SetStatus(DllExports::GdipGetLineRect((GpLineGradient*)nativeBrush, rect));
  433. }
  434. Status GetRectangle(OUT Rect* rect) const
  435. {
  436. return SetStatus(DllExports::GdipGetLineRectI((GpLineGradient*)nativeBrush, rect));
  437. }
  438. Status SetGammaCorrection(IN BOOL useGammaCorrection)
  439. {
  440. return SetStatus(DllExports::GdipSetLineGammaCorrection((GpLineGradient*)nativeBrush,
  441. useGammaCorrection));
  442. }
  443. BOOL GetGammaCorrection() const
  444. {
  445. BOOL useGammaCorrection;
  446. SetStatus(DllExports::GdipGetLineGammaCorrection((GpLineGradient*)nativeBrush,
  447. &useGammaCorrection));
  448. return useGammaCorrection;
  449. }
  450. INT GetBlendCount() const
  451. {
  452. INT count = 0;
  453. SetStatus(DllExports::GdipGetLineBlendCount((GpLineGradient*)
  454. nativeBrush,
  455. &count));
  456. return count;
  457. }
  458. Status SetBlend(IN const REAL* blendFactors,
  459. IN const REAL* blendPositions,
  460. IN INT count)
  461. {
  462. return SetStatus(DllExports::GdipSetLineBlend((GpLineGradient*)
  463. nativeBrush,
  464. blendFactors,
  465. blendPositions,
  466. count));
  467. }
  468. Status GetBlend(OUT REAL* blendFactors,
  469. OUT REAL* blendPositions,
  470. IN INT count) const
  471. {
  472. return SetStatus(DllExports::GdipGetLineBlend((GpLineGradient*)nativeBrush,
  473. blendFactors,
  474. blendPositions,
  475. count));
  476. }
  477. INT GetInterpolationColorCount() const
  478. {
  479. INT count = 0;
  480. SetStatus(DllExports::GdipGetLinePresetBlendCount((GpLineGradient*)
  481. nativeBrush,
  482. &count));
  483. return count;
  484. }
  485. Status SetInterpolationColors(IN const Color* presetColors,
  486. IN const REAL* blendPositions,
  487. IN INT count)
  488. {
  489. if ((count <= 0) || !presetColors)
  490. return SetStatus(InvalidParameter);
  491. ARGB *argbs = (ARGB*) new BYTE[count*sizeof(ARGB)];
  492. if (argbs)
  493. {
  494. for (INT i = 0; i < count; i++)
  495. {
  496. argbs[i] = presetColors[i].GetValue();
  497. }
  498. Status status = SetStatus(DllExports::GdipSetLinePresetBlend(
  499. (GpLineGradient*) nativeBrush,
  500. argbs,
  501. blendPositions,
  502. count));
  503. delete [] argbs;
  504. return status;
  505. }
  506. else
  507. {
  508. return SetStatus(OutOfMemory);
  509. }
  510. }
  511. Status GetInterpolationColors(OUT Color* presetColors,
  512. OUT REAL* blendPositions,
  513. IN INT count) const
  514. {
  515. if ((count <= 0) || !presetColors)
  516. return SetStatus(InvalidParameter);
  517. ARGB* argbs = (ARGB*) new BYTE[count*sizeof(ARGB)];
  518. if (!argbs)
  519. {
  520. return SetStatus(OutOfMemory);
  521. }
  522. Status status = SetStatus(DllExports::GdipGetLinePresetBlend((GpLineGradient*)nativeBrush,
  523. argbs,
  524. blendPositions,
  525. count));
  526. if (status == Ok)
  527. {
  528. for (INT i = 0; i < count; i++)
  529. {
  530. presetColors[i] = Color(argbs[i]);
  531. }
  532. }
  533. delete [] argbs;
  534. return status;
  535. }
  536. Status SetBlendBellShape(IN REAL focus,
  537. IN REAL scale = 1.0f)
  538. {
  539. return SetStatus(DllExports::GdipSetLineSigmaBlend((GpLineGradient*)nativeBrush, focus, scale));
  540. }
  541. Status SetBlendTriangularShape(
  542. IN REAL focus,
  543. IN REAL scale = 1.0f
  544. )
  545. {
  546. return SetStatus(DllExports::GdipSetLineLinearBlend((GpLineGradient*)nativeBrush, focus, scale));
  547. }
  548. Status SetTransform(IN const Matrix* matrix)
  549. {
  550. return SetStatus(DllExports::GdipSetLineTransform((GpLineGradient*)nativeBrush,
  551. matrix->nativeMatrix));
  552. }
  553. Status GetTransform(OUT Matrix *matrix) const
  554. {
  555. return SetStatus(DllExports::GdipGetLineTransform((GpLineGradient*)nativeBrush,
  556. matrix->nativeMatrix));
  557. }
  558. Status ResetTransform()
  559. {
  560. return SetStatus(DllExports::GdipResetLineTransform((GpLineGradient*)nativeBrush));
  561. }
  562. Status MultiplyTransform(IN const Matrix* matrix,
  563. IN MatrixOrder order = MatrixOrderPrepend)
  564. {
  565. return SetStatus(DllExports::GdipMultiplyLineTransform((GpLineGradient*)nativeBrush,
  566. matrix->nativeMatrix,
  567. order));
  568. }
  569. Status TranslateTransform(IN REAL dx,
  570. IN REAL dy,
  571. IN MatrixOrder order = MatrixOrderPrepend)
  572. {
  573. return SetStatus(DllExports::GdipTranslateLineTransform((GpLineGradient*)nativeBrush,
  574. dx, dy, order));
  575. }
  576. Status ScaleTransform(IN REAL sx,
  577. IN REAL sy,
  578. IN MatrixOrder order = MatrixOrderPrepend)
  579. {
  580. return SetStatus(DllExports::GdipScaleLineTransform((GpLineGradient*)nativeBrush,
  581. sx, sy, order));
  582. }
  583. Status RotateTransform(IN REAL angle,
  584. IN MatrixOrder order = MatrixOrderPrepend)
  585. {
  586. return SetStatus(DllExports::GdipRotateLineTransform((GpLineGradient*)nativeBrush,
  587. angle, order));
  588. }
  589. Status SetWrapMode(IN WrapMode wrapMode)
  590. {
  591. return SetStatus(DllExports::GdipSetLineWrapMode((GpLineGradient*)nativeBrush,
  592. wrapMode));
  593. }
  594. WrapMode GetWrapMode() const
  595. {
  596. WrapMode wrapMode;
  597. SetStatus(DllExports::GdipGetLineWrapMode((GpLineGradient*)
  598. nativeBrush,
  599. &wrapMode));
  600. return wrapMode;
  601. }
  602. private:
  603. LinearGradientBrush(const LinearGradientBrush &);
  604. LinearGradientBrush& operator=(const LinearGradientBrush &);
  605. protected:
  606. LinearGradientBrush()
  607. {
  608. }
  609. };
  610. //--------------------------------------------------------------------------
  611. // PathGradientBrush object is defined
  612. // in gdipluspath.h.
  613. //--------------------------------------------------------------------------
  614. //--------------------------------------------------------------------------
  615. // Hatch Brush Object
  616. //--------------------------------------------------------------------------
  617. class HatchBrush : public Brush
  618. {
  619. public:
  620. friend class Pen;
  621. HatchBrush(IN HatchStyle hatchStyle,
  622. IN const Color& foreColor,
  623. IN const Color& backColor = Color())
  624. {
  625. GpHatch *brush = NULL;
  626. lastResult = DllExports::GdipCreateHatchBrush(hatchStyle,
  627. foreColor.GetValue(),
  628. backColor.GetValue(),
  629. &brush);
  630. SetNativeBrush(brush);
  631. }
  632. HatchStyle GetHatchStyle() const
  633. {
  634. HatchStyle hatchStyle;
  635. SetStatus(DllExports::GdipGetHatchStyle((GpHatch*)nativeBrush,
  636. &hatchStyle));
  637. return hatchStyle;
  638. }
  639. Status GetForegroundColor(OUT Color* color) const
  640. {
  641. ARGB argb;
  642. if (color == NULL)
  643. {
  644. return SetStatus(InvalidParameter);
  645. }
  646. Status status = SetStatus(DllExports::GdipGetHatchForegroundColor(
  647. (GpHatch*)nativeBrush,
  648. &argb));
  649. color->SetValue(argb);
  650. return status;
  651. }
  652. Status GetBackgroundColor(OUT Color *color) const
  653. {
  654. ARGB argb;
  655. if (color == NULL)
  656. {
  657. return SetStatus(InvalidParameter);
  658. }
  659. Status status = SetStatus(DllExports::GdipGetHatchBackgroundColor(
  660. (GpHatch*)nativeBrush,
  661. &argb));
  662. color->SetValue(argb);
  663. return status;
  664. }
  665. private:
  666. HatchBrush(const HatchBrush &);
  667. HatchBrush& operator=(const HatchBrush &);
  668. protected:
  669. HatchBrush()
  670. {
  671. }
  672. };
  673. #endif