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 dguihub.picturebox; 10 11 import dguihub.core.controls.control; 12 import dguihub.canvas; 13 14 enum SizeMode { 15 normal = 0, 16 autoSize = 1, 17 } 18 19 class PictureBox : Control { 20 private SizeMode _sm = SizeMode.normal; 21 private Image _img; 22 23 public override void dispose() { 24 if (this._img) { 25 this._img.dispose(); 26 this._img = null; 27 } 28 29 super.dispose(); 30 } 31 32 alias @property Control.bounds bounds; 33 34 @property public override void bounds(Rect r) { 35 if (this._img && this._sm is SizeMode.autoSize) { 36 // Ignora 'r.size' e usa la dimensione dell'immagine 37 Size sz = r.size; 38 super.bounds = Rect(r.x, r.y, sz.width, sz.height); 39 40 } else { 41 super.bounds = r; 42 } 43 } 44 45 @property public final SizeMode sizeMode() { 46 return this._sm; 47 } 48 49 @property public final void sizeMode(SizeMode sm) { 50 this._sm = sm; 51 52 if (this.created) { 53 this.redraw(); 54 } 55 } 56 57 @property public final Image image() { 58 return this._img; 59 } 60 61 @property public final void image(Image img) { 62 if (this._img) { 63 this._img.dispose(); // Destroy the previous image 64 } 65 66 this._img = img; 67 68 if (this.created) { 69 this.redraw(); 70 } 71 } 72 73 protected override void createControlParams(ref CreateControlParams ccp) { 74 ccp.className = WC_DPICTUREBOX; 75 ccp.defaultCursor = SystemCursors.arrow; 76 ccp.classStyle = ClassStyles.parentDC; 77 78 super.createControlParams(ccp); 79 } 80 81 protected override void onPaint(PaintEventArgs e) { 82 if (this._img) { 83 Canvas c = e.canvas; 84 85 switch (this._sm) { 86 case SizeMode.autoSize: 87 c.drawImage(this._img, Rect(nullPoint, this.size)); 88 break; 89 90 default: 91 c.drawImage(this._img, 0, 0); 92 break; 93 } 94 } 95 96 super.onPaint(e); 97 } 98 }