1 /** DGui project file. 2 3 Copyright: Trogu Antonio Davide 2011-2013 4 5 License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0). 6 7 Authors: Trogu Antonio Davide 8 */ 9 module dgui.picturebox; 10 11 import dgui.core.controls.control; 12 import dgui.canvas; 13 14 enum SizeMode 15 { 16 normal = 0, 17 autoSize = 1, 18 } 19 20 class PictureBox: Control 21 { 22 private SizeMode _sm = SizeMode.normal; 23 private Image _img; 24 25 public override void dispose() 26 { 27 if(this._img) 28 { 29 this._img.dispose(); 30 this._img = null; 31 } 32 33 super.dispose(); 34 } 35 36 alias @property Control.bounds bounds; 37 38 @property public override void bounds(Rect r) 39 { 40 if(this._img && this._sm is SizeMode.autoSize) 41 { 42 // Ignora 'r.size' e usa la dimensione dell'immagine 43 Size sz = r.size; 44 super.bounds = Rect(r.x, r.y, sz.width, sz.height); 45 46 } 47 else 48 { 49 super.bounds = r; 50 } 51 } 52 53 @property public final SizeMode sizeMode() 54 { 55 return this._sm; 56 } 57 58 @property public final void sizeMode(SizeMode sm) 59 { 60 this._sm = sm; 61 62 if(this.created) 63 { 64 this.redraw(); 65 } 66 } 67 68 @property public final Image image() 69 { 70 return this._img; 71 } 72 73 @property public final void image(Image img) 74 { 75 if(this._img) 76 { 77 this._img.dispose(); // Destroy the previous image 78 } 79 80 this._img = img; 81 82 if(this.created) 83 { 84 this.redraw(); 85 } 86 } 87 88 protected override void createControlParams(ref CreateControlParams ccp) 89 { 90 ccp.className = WC_DPICTUREBOX; 91 ccp.defaultCursor = SystemCursors.arrow; 92 ccp.classStyle = ClassStyles.parentDC; 93 94 super.createControlParams(ccp); 95 } 96 97 protected override void onPaint(PaintEventArgs e) 98 { 99 if(this._img) 100 { 101 Canvas c = e.canvas; 102 103 switch(this._sm) 104 { 105 case SizeMode.autoSize: 106 c.drawImage(this._img, Rect(nullPoint, this.size)); 107 break; 108 109 default: 110 c.drawImage(this._img, 0, 0); 111 break; 112 } 113 } 114 115 super.onPaint(e); 116 } 117 }