/************************************
Author		: lujjjh
Date		: 2010.9
************************************/

namespace win.ui {
	import util.metaProperty;
	import win.subclass;
	import win.ui.ctrl.common
	
	statusbar = class {
		ctor (form) {
			var hParent = type(form) == type.table ? form.hwnd : form;
			assert( hParent, '创建状态栏失败！\r\n请提供正确的参数' );
			this.hwnd = ::CreateWindowEx(0, "msctls_statusbar32", "", 
										 0x40000000/*_WS_CHILD*/ | 0x10000000/*_WS_VISIBLE*/ | 0x4000000/*_WS_CLIPSIBLINGS*/, 
										 0 ,0 ,0 ,0, hParent, 0);
			assert( this.hwnd, '创建状态栏失败！\r\n创建窗口失败' );
			var itemsWidth = {};
			var subclass = ..win.subclass(
				hParent, 
				function (hwnd, message, wparam, lparam) {
					select (message) {
						case 0x5/*_WM_SIZE*/ {
							::SendMessageInt(this.hwnd, 0x5/*_WM_SIZE*/, 0, 0);
						}
					}
				}
			);
		}
		updateItems = function () {
			var tbl = {};
			var n = 0;
			for (k, v in itemsWidth) {
				..table.push(tbl, v == -1 ? -1 : n + v);
				n += v;
			}
			::SendMessageByStruct( this.hwnd, 0x404, #itemsWidth, ..raw.toarray( tbl, "int" ));
		}
		addItem = function ( text, width = -1 ) {
			var p = ..table.push( itemsWidth, width );
			this.updateItems();
			this.setText( p, text );
			return this, p;
		}
		insertItem = function ( position = 1, text, width = -1 ) {
			var p = ..table.insert( itemsWidth, width, position );
			this.updateItems();
			this.setText( position, text );
			return this, p;
		}
		getItemsNum = function () {
			return #itemsWidth;
		}
		getItemWidth = function ( id ) {
			return itemsWidth[id];
		}
		setItemWidth = function ( id, width = -1 ) {
			itemsWidth[id] = width;
			this.updateItems();
			return this;
		}
		getText = function ( id ) {
			if ( #itemsWidth == 0 ) {
				return ..win.getText( this.hwnd )
			} else {
				var re, buffer = ::SendMessageByString( this.hwnd, 0x402, id - 1, 255 );
				return buffer;
			}
		}
		setText = function ( p1, p2 ) {
			if ( #itemsWidth == 0 )
				..win.setText( this.hwnd, p1 );
			else
				SendMessageByString( this.hwnd, 0x401, p1 - 1, p2 );
			return this;
		}
		reset = function () {
			itemsWidth = {};
			::SendMessageInt( this.hwnd, 0x10/*_WM_CLOSE*/, 0, 0 );
		}
	}
}

/**intellisense(win.ui)
statusbar = 状态栏
statusbar(__/*父窗口*/) = 创建一个状态栏
end intellisense**/
/**intellisense(stat)
addItem(__/*文本*/, /*宽度*/) = 创建一个状态栏分块
insertItem(__/*插入位置*/, /*文本*/, /*宽度*/) = 插入一个状态栏分块，插入位置从 1 开始
getItemWidth(__/*第几个分块*/) = 获取指定分块的宽度，分块从 1 开始
setItemWidth(__/*第几个分块*/, /*宽度*/) = 设置指定分块的宽度，分块从 1 开始
getText() = 获取状态栏的文本（无分块）
getText(__/*第几个分块*/) = 获取指定分块的文本，分块从 1 开始
setText(__/*文本*/) = 设置状态栏的文本（无分块）
setText(__/*第几个分块*/, /*文本*/) = 设置指定分块的文本，分块从 1 开始
getItemsNum() = 获取分块的数量
reset() = 清除状态栏
end intellisense**/
