PageRenderTime 90ms CodeModel.GetById 11ms app.highlight 73ms RepoModel.GetById 2ms app.codeStats 0ms

/binding/win32/gdiplusbrush.d

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