1 jls.loader.provide('jls.win32.ImageElement');
  2 
  3 jls.loader.require('jls.win32.WindowElement');
  4 jls.loader.require('jls.win32.DeviceContext');
  5 jls.loader.require('jls.io.File');
  6 jls.loader.require('jls.io.FileChannel');
  7 jls.loader.require('jls.util.Image');
  8 
  9 jls.win32.ImageElement = jls.lang.Class.create(jls.win32.WindowElement,
 10 {
 11     initialize : function($super, parameters, parent) {
 12         this._dc = null;
 13         this._image = null;
 14         this._width = null; //'100%';
 15         this._height = null; //'100%';
 16         $super(parameters, parent);
 17         this.observe('unload', this.onUnload.bind(this));
 18         // TODO Fix this workaround, we miss creation messages
 19         this.onWindowMessage(jls.win32.Window.WM_PAINT);
 20     },
 21     onUnload : function() {
 22         if (this._dc != null) {
 23             this._dc.release();
 24         }
 25     },
 26     onCreate : function() {
 27         this._window = new jls.win32.Window(jls.win32.ImageElement.classname, undefined, this.getWindowStyle(),
 28                 this.getX(), this.getY(), this.getW(), this.getH(),
 29                 this.getParentWindow(true), this.getWindowId(true), this.getWindowExStyle());
 30         this._dc = new jls.win32.DeviceContext(this._window);
 31     },
 32     onWindowMessage: function($super, message, wParam, lParam) {
 33     	//jls.logger.warn('onWindowMessage(' + jls.win32.Window.getMessageName(message) + ', ' + wParam + ', ' + lParam + ')');
 34     	$super(message, wParam, lParam);
 35         switch(message) {
 36         case jls.win32.Window.WM_PAINT:
 37             if ((this._dc != null) && (this._image != null)) {
 38             	if ((this.getStyle().getPropertyValue('width') != null) && (this.getStyle().getPropertyValue('height') != null)) {
 39                     this._dc.stretchDIBits(this._image, this.getStyle().getPropertyValue('width'), this.getStyle().getPropertyValue('height'));
 40             	} else {
 41                     this._dc.setDIBits(this._image, 0, 0);
 42             	}
 43             }
 44             this._window.validate(); // validates the whole client area 
 45             return false; // do not use default message handler
 46         }
 47     },
 48     getWidth : function() {
 49         return this._width;
 50     },
 51     setWidth : function(width) {
 52         this._width = width;
 53     },
 54     getHeight : function() {
 55         return this._height;
 56     },
 57     setHeight : function(height) {
 58         this._height = height;
 59         // TODO Update
 60     },
 61     getImage : function() {
 62         return this._image;
 63     },
 64     _updateSize : function() {
 65         if (this._image == null) {
 66             // default
 67             this.getStyle().setProperty('width', 32);
 68             this.getStyle().setProperty('height', 32);
 69             return;
 70         }
 71         var width = null;
 72         var height = null;
 73         if (this._width != null) {
 74             width = jls.win32.ImageElement.computeSize(this._width, this._image.getWidth());
 75         }
 76         if (this._height != null) {
 77             height = jls.win32.ImageElement.computeSize(this._height, this._image.getHeight());
 78         }
 79         if ((width == null) && (height == null)) {
 80             width = this._image.getWidth();
 81             height = this._image.getHeight();
 82         } else if (width == null) {
 83             width = Math.round(this._image.getWidth() * height / this._image.getHeight());
 84         } else if (height == null) {
 85             height = Math.round(this._image.getHeight() * width / this._image.getWidth());
 86         }
 87         this.getStyle().setProperty('width', width);
 88         this.getStyle().setProperty('height', height);
 89         this.getParent().update(); // if size changed
 90         // TODO Fix this workaround
 91         this.onWindowMessage(jls.win32.Window.WM_PAINT);
 92         //jls.logger.warn('ImageElement._updateSize() => ' + width + 'x' + height + ' (' + this._width.toString() + 'x' + this._height + ')');
 93     },
 94     setImage : function(name) {
 95         var f = new jls.io.File(name);
 96         if (! f.exists()) {
 97             this._updateSize();
 98             return this;
 99         }
100         //jls.logger.warn('ImageElement: loading "' + name + '"...');
101         var fc = new jls.io.FileChannel(f);
102         var img = null;
103         if (name.endsWith('.bmp')) {
104             img = jls.util.Image.readBMP(fc);
105         } else if (name.endsWith('.jpg')) {
106             img = jls.util.Image.readJPEG(fc);
107         }
108         fc.close();
109         this._image = img;
110         this._updateSize();
111         return this;
112     }
113 });
114 
115 Object.extend(jls.win32.ImageElement,
116 {
117     classname : 'JLSImageClass',
118     computeSize : function(value, size) {
119         if (value == null) {
120             return size;
121         }
122         if (typeof value == 'number') {
123             return value;
124         }
125         if ((typeof value != 'string') || (value.length == 0)) {
126             throw new jls.lang.Exception('Illegal type of value "' + (typeof value) + '"');
127         }
128         value = value.match(/([0-9]+)(.*)/);
129         var numberValue = value[1];
130         var typeValue = value[2] || 'px';
131         switch (typeValue) {
132         case 'px':
133             return Math.round(numberValue);
134         case '%':
135             return Math.round(numberValue * size / 100);
136         default:
137             throw new jls.lang.Exception('Illegal size type "' + typeValue + '"');
138         }
139     }
140 });
141 
142 jls.win32.Window.registerClass(jls.win32.ImageElement.classname);
143 
144