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.getBuffer(), this._image.getWidth(), this._image.getHeight(), 40 this.getStyle().getPropertyValue('width'), this.getStyle().getPropertyValue('height'), this._image.topDown); 41 } else { 42 this._dc.setDIBits(this._image.getBuffer(), this._image.getWidth(), this._image.getHeight(), 0, 0, this._image.topDown); 43 } 44 } 45 this._window.validate(); // validates the whole client area 46 return false; // do not use default message handler 47 } 48 }, 49 getWidth : function() { 50 return this._width; 51 }, 52 setWidth : function(width) { 53 this._width = width; 54 }, 55 getHeight : function() { 56 return this._height; 57 }, 58 setHeight : function(height) { 59 this._height = height; 60 // TODO Update 61 }, 62 getImage : function() { 63 return this._image; 64 }, 65 _updateSize : function() { 66 if (this._image == null) { 67 // default 68 this.getStyle().setProperty('width', 32); 69 this.getStyle().setProperty('height', 32); 70 return; 71 } 72 var width = null; 73 var height = null; 74 if (this._width != null) { 75 width = jls.win32.ImageElement.computeSize(this._width, this._image.getWidth()); 76 } 77 if (this._height != null) { 78 height = jls.win32.ImageElement.computeSize(this._height, this._image.getHeight()); 79 } 80 if ((width == null) && (height == null)) { 81 width = this._image.getWidth(); 82 height = this._image.getHeight(); 83 } else if (width == null) { 84 width = Math.round(this._image.getWidth() * height / this._image.getHeight()); 85 } else if (height == null) { 86 height = Math.round(this._image.getHeight() * width / this._image.getWidth()); 87 } 88 this.getStyle().setProperty('width', width); 89 this.getStyle().setProperty('height', height); 90 this.getParent().update(); // if size changed 91 // TODO Fix this workaround 92 this.onWindowMessage(jls.win32.Window.WM_PAINT); 93 //jls.logger.warn('ImageElement._updateSize() => ' + width + 'x' + height + ' (' + this._width.toString() + 'x' + this._height + ')'); 94 }, 95 setImage : function(name) { 96 var f = new jls.io.File(name); 97 if (! f.exists()) { 98 this._updateSize(); 99 return this; 100 } 101 //jls.logger.warn('ImageElement: loading "' + name + '"...'); 102 var fc = new jls.io.FileChannel(f); 103 var img = null; 104 if (name.endsWith('.bmp')) { 105 img = jls.util.Image.readBMP(fc); 106 img.topDown = false; 107 } else if (name.endsWith('.jpg')) { 108 img = jls.util.Image.readJPEG(fc); 109 img.topDown = true; 110 } 111 fc.close(); 112 this._image = img; 113 this._updateSize(); 114 return this; 115 } 116 }); 117 118 Object.extend(jls.win32.ImageElement, 119 { 120 classname : 'JLSImageClass', 121 computeSize : function(value, size) { 122 if (value == null) { 123 return size; 124 } 125 if (typeof value == 'number') { 126 return value; 127 } 128 if ((typeof value != 'string') || (value.length == 0)) { 129 throw new jls.lang.Exception('Illegal type of value "' + (typeof value) + '"'); 130 } 131 value = value.match(/([0-9]+)(.*)/); 132 var numberValue = value[1]; 133 var typeValue = value[2] || 'px'; 134 switch (typeValue) { 135 case 'px': 136 return Math.round(numberValue); 137 case '%': 138 return Math.round(numberValue * size / 100); 139 default: 140 throw new jls.lang.Exception('Illegal size type "' + typeValue + '"'); 141 } 142 } 143 }); 144 145 jls.win32.Window.registerClass(jls.win32.ImageElement.classname); 146 147