1 // Written by Christopher E. Miller 2 // See the included license.txt for copyright and license details. 3 4 5 /// 6 module dfl.picturebox; 7 8 private import dfl.control, dfl.base, dfl.drawing, dfl.event; 9 private import dfl.internal.winapi; 10 11 12 /// 13 enum PictureBoxSizeMode: ubyte 14 { 15 /// 16 NORMAL, // Image at upper left of control. 17 /// ditto 18 AUTO_SIZE, // Control sizes to fit image size. 19 /// ditto 20 CENTER_IMAGE, // Image at center of control. 21 /// ditto 22 STRETCH_IMAGE, // Image stretched to fit control. 23 } 24 25 26 /// 27 class PictureBox: Control // docmain 28 { 29 this() 30 { 31 //resizeRedraw = true; // Redrawn manually in onResize() when necessary. 32 } 33 34 35 /// 36 final @property void image(Image img) // setter 37 { 38 if(this.img is img) 39 return; 40 41 if(_mode == PictureBoxSizeMode.AUTO_SIZE) 42 { 43 if(img) 44 clientSize = img.size; 45 else 46 clientSize = Size(0, 0); 47 } 48 49 this.img = img; 50 51 if(created) 52 invalidate(); 53 54 onImageChanged(EventArgs.empty); 55 } 56 57 /// ditto 58 final @property Image image() // getter 59 { 60 return img; 61 } 62 63 64 /// 65 final @property void sizeMode(PictureBoxSizeMode sm) // setter 66 { 67 if(_mode == sm) 68 return; 69 70 final switch(sm) 71 { 72 case PictureBoxSizeMode.AUTO_SIZE: 73 if(img) 74 clientSize = img.size; 75 else 76 clientSize = Size(0, 0); 77 break; 78 79 case PictureBoxSizeMode.NORMAL: 80 break; 81 82 case PictureBoxSizeMode.CENTER_IMAGE: 83 break; 84 85 case PictureBoxSizeMode.STRETCH_IMAGE: 86 break; 87 } 88 89 _mode = sm; 90 91 if(created) 92 invalidate(); 93 94 onSizeModeChanged(EventArgs.empty); 95 } 96 97 /// ditto 98 final @property PictureBoxSizeMode sizeMode() // getter 99 { 100 return _mode; 101 } 102 103 104 /// 105 @property void borderStyle(BorderStyle bs) // setter 106 { 107 final switch(bs) 108 { 109 case BorderStyle.FIXED_3D: 110 _style(_style() & ~WS_BORDER); 111 _exStyle(_exStyle() | WS_EX_CLIENTEDGE); 112 break; 113 114 case BorderStyle.FIXED_SINGLE: 115 _exStyle(_exStyle() & ~WS_EX_CLIENTEDGE); 116 _style(_style() | WS_BORDER); 117 break; 118 119 case BorderStyle.NONE: 120 _style(_style() & ~WS_BORDER); 121 _exStyle(_exStyle() & ~WS_EX_CLIENTEDGE); 122 break; 123 } 124 125 if(created) 126 { 127 redrawEntire(); 128 } 129 } 130 131 /// ditto 132 @property BorderStyle borderStyle() // getter 133 { 134 if(_exStyle() & WS_EX_CLIENTEDGE) 135 return BorderStyle.FIXED_3D; 136 else if(_style() & WS_BORDER) 137 return BorderStyle.FIXED_SINGLE; 138 return BorderStyle.NONE; 139 } 140 141 142 //EventHandler sizeModeChanged; 143 Event!(PictureBox, EventArgs) sizeModeChanged; /// 144 //EventHandler imageChanged; 145 Event!(PictureBox, EventArgs) imageChanged; /// 146 147 148 protected: 149 150 /// 151 void onSizeModeChanged(EventArgs ea) 152 { 153 sizeModeChanged(this, ea); 154 } 155 156 157 /// 158 void onImageChanged(EventArgs ea) 159 { 160 imageChanged(this, ea); 161 } 162 163 164 override void onPaint(PaintEventArgs ea) 165 { 166 if(img) 167 { 168 final switch(_mode) 169 { 170 case PictureBoxSizeMode.NORMAL: 171 case PictureBoxSizeMode.AUTO_SIZE: // Drawn the same as normal. 172 img.draw(ea.graphics, Point(0, 0)); 173 break; 174 175 case PictureBoxSizeMode.CENTER_IMAGE: 176 { 177 Size isz; 178 isz = img.size; 179 img.draw(ea.graphics, Point((clientSize.width - isz.width) / 2, 180 (clientSize.height - isz.height) / 2)); 181 } 182 break; 183 184 case PictureBoxSizeMode.STRETCH_IMAGE: 185 img.drawStretched(ea.graphics, Rect(0, 0, clientSize.width, clientSize.height)); 186 break; 187 } 188 } 189 190 super.onPaint(ea); 191 } 192 193 194 override void onResize(EventArgs ea) 195 { 196 if(PictureBoxSizeMode.CENTER_IMAGE == _mode || PictureBoxSizeMode.STRETCH_IMAGE == _mode) 197 invalidate(); 198 199 super.onResize(ea); 200 } 201 202 203 private: 204 PictureBoxSizeMode _mode = PictureBoxSizeMode.NORMAL; 205 Image img = null; 206 } 207