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