PageRenderTime 52ms CodeModel.GetById 15ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 0ms

/platform/win/gui/osbutton.d

http://github.com/wilkie/djehuty
D | 252 lines | 169 code | 62 blank | 21 comment | 31 complexity | a2f0fd7199d19ffcbd0327ab826762a3 MD5 | raw file
  1module gui.osbutton;
  2
  3import gui.button;
  4
  5import binding.win32.windef;
  6import binding.win32.winnt;
  7import binding.win32.winbase;
  8import binding.win32.wingdi;
  9import binding.win32.winuser;
 10
 11import platform.win.widget;
 12import platform.win.main;
 13
 14import platform.vars.view;
 15
 16import core.string;
 17import core.definitions;
 18
 19import gui.widget;
 20import gui.window;
 21import gui.apploop;
 22
 23import io.console;
 24
 25import graphics.view;
 26import graphics.graphics;
 27
 28class OSButton : Button, WinWidget {
 29
 30	this(int x, int y, int width, int height, string value) {
 31		super(x,y,width,height,value);
 32	}
 33
 34	override void onAdd() {
 35
 36		ViewPlatformVars* viewVars = _window._viewVars;
 37
 38		HDC dc = GetDC(_hWnd);
 39
 40		HDC dc2 = CreateCompatibleDC(dc);
 41		HBITMAP hbmp = CreateCompatibleBitmap(viewVars.dc, this.width, this.height);
 42
 43		SelectObject(dc2, hbmp);
 44
 45		DeleteObject(hbmp);
 46		ReleaseDC(_hWnd, dc);
 47
 48		newx = -this.width + 1;
 49		newy = -this.height + 1;
 50		_hdc = dc2;
 51
 52		_hWnd = CreateWindowExW(0,
 53			"BUTTON\0", cast(wchar*)_value.ptr, WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_TEXT ,
 54			newx, newy, this.width, this.height,
 55			_window._pfvars.hWnd, null, cast(HINSTANCE)GetWindowLongW(_window._pfvars.hWnd,GWLP_HINSTANCE), null);
 56
 57		SetWindowPos(_hWnd, cast(HWND)HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
 58
 59		SendMessageW( _hWnd, WM_SETFONT, cast(WPARAM)GuiApplicationController.win_button_font, 1);
 60
 61		SetWindowLongW(_hWnd, GWLP_USERDATA, cast(LONG)(cast(void*)(cast(WinWidget)this)));
 62		_oldproc = cast(WNDPROC)SetWindowLongW(_hWnd, GWLP_WNDPROC, cast(ULONG)&GuiApplicationController.CtrlProc);
 63
 64		SendMessageW(_hWnd, WM_PRINTCLIENT, cast(DWORD)_hdc, PRF_CHILDREN | PRF_CLIENT | PRF_ERASEBKGND | PRF_NONCLIENT | PRF_OWNED);
 65	}
 66
 67	override void onDraw(ref Graphics g) {
 68		// save current background for later
 69
 70		// copy over current image
 71		ViewPlatformVars* viewVars = _window._viewVars;
 72
 73		BitBlt(viewVars.dc, this.left, this.top, this.width, this.height, _hdc, 0,0,SRCCOPY);
 74	}
 75
 76	override bool onPrimaryMouseDown(ref Mouse mouse) {
 77		hasCapture = true;
 78
 79		SendMessageW(_hWnd, WM_LBUTTONDOWN, 0, 0);
 80		return true;
 81	}
 82
 83	override bool onPrimaryMouseUp(ref Mouse mouse) {
 84		hasCapture = false;
 85
 86		SendMessageW(_hWnd, WM_LBUTTONUP, 0, 0);
 87		requestRelease();
 88		return true;
 89	}
 90
 91	override bool onMouseEnter() {
 92		return false;
 93	}
 94
 95	override bool onMouseMove(ref Mouse mouse) {
 96
 97		if (hasCapture) {
 98			return false;
 99		}
100
101		POINT pt;
102		GetCursorPos(&pt);
103
104		RECT rect;
105		RECT client;
106
107		int ncx, ncy;
108
109		GetWindowRect(_window._pfvars.hWnd, &rect);
110		ncx = rect.left;
111		ncy = rect.top;
112
113		ncx = pt.x + (newx - this.left);
114		ncy = pt.y + (newy - this.top);
115
116		ncy &= 0xffff;
117		ncx &= 0xffff;
118
119		uint lparam = (ncy << 16) | (ncx);
120		SendMessageW(_hWnd, WM_NCHITTEST, 0, lparam);
121
122		lparam = ((mouse.y - this.top) << 16) | (mouse.x - this.left);
123
124		SendMessageW(_hWnd, WM_MOUSEMOVE, 0, lparam);
125		SendMessageW(_hWnd, WM_PAINT, 0, 0);
126		//SendMessageW(_hWnd, WM_MOUSEHOVER, 0, lparam);
127		return false;
128	}
129
130	override bool onMouseLeave() {
131	//	Console.putln("mouseleave");
132		SendMessageW(_hWnd, WM_MOUSELEAVE, 0, 0);
133		return true;
134	}
135
136	override void onRemove() {
137		DestroyWindow(_hWnd);
138	}
139
140protected:
141
142	HDC _GetDC() {
143		return _hdc;
144	}
145
146	LRESULT _AppLoopMessage(uint message, WPARAM wParam, LPARAM lParam) {
147	//	Console.putln("message: ", new String("%x",message), " ml:", new String("%x", WM_MOUSELEAVE));
148		if (message == WM_COMMAND) {
149			raiseSignal(Button.Signal.Selected);
150			return 0;
151		}
152		else if (message == WM_LBUTTONUP && hasCapture) {
153			short x, y;
154			x = cast(short)(lParam & 0xffff);
155			y = cast(short)((lParam >> 16) & 0xffff);
156
157			// convert coords to window coords, send window WM_MOUSEMOVE
158			x += newx; //this.left;
159			y += newy;
160			uint windowlParam = (y << 16) | x;
161			x = cast(short)(lParam & 0xffff);
162			y = cast(short)((lParam >> 16) & 0xffff);
163//			Console.putln("mouse up (captured) x:", x, "y:",y);
164			SendMessageW(_window._pfvars.hWnd, message, 0, windowlParam);
165		}
166		else if (message == WM_MOUSELEAVE) {
167			// ignore!
168			if (_hovered) {
169				//Console.putln("message ignored");
170			return 0;
171			}
172			else {
173				//Console.putln("message meh!");
174			}
175		}
176		else if (message == WM_NCHITTEST || message == WM_MOUSEMOVE) {
177			// change wParam to point to where it thinks it is
178
179			short x, y;
180			x = cast(short)(lParam & 0xffff);
181			y = cast(short)((lParam >> 16) & 0xffff);
182
183			if (message == WM_NCHITTEST) {
184				//Console.put("HT ");
185			}
186			else {
187				//Console.put("MM ");
188			}
189			//Console.putln("x: ", x, " y: ", y);
190
191			if (hasCapture) {
192				// convert coords to window coords, send window WM_MOUSEMOVE
193				x += newx; //this.left;
194				y += newy;
195				uint windowlParam = (y << 16) | x;
196				x = cast(short)(lParam & 0xffff);
197				y = cast(short)((lParam >> 16) & 0xffff);
198			//	Console.putln("mouse move (captured) x:", x, "y:",y);
199				SendMessageW(_window._pfvars.hWnd,WM_MOUSEMOVE, 0, windowlParam);
200			}
201			else {
202				//Console.putln("mouse move");
203			}
204
205			if (message == WM_MOUSEMOVE) {
206				if (_hovered) {
207				//	Console.putln("hovered");
208					x += (newx - this.left);
209					y += (newy - this.top);
210					lParam = (y << 16) | x;
211				}
212				else {
213					//Console.putln("not hovered");
214				}
215			}
216
217//			Console.putln("x: ", x, " y: ", y, " msg: ", new String("%x",message));
218
219		}
220		else if (message == WM_ERASEBKGND) {
221		}
222
223		auto ret = CallWindowProcW(_oldproc, _hWnd, message, wParam, lParam);
224		
225		//Console.putln("return: ", ret);
226
227		if (message == WM_PAINT) {
228			_window.onDraw();
229		}
230
231		return ret;
232	}
233
234	View _ReturnView(out int x, out int y, out int w, out int h) {
235		x = this.left;
236		y = this.top;
237		w = this.width;
238		h = this.height;
239		return _view;
240	}
241
242	HWND _hWnd;
243	HDC _hdc;
244	WNDPROC _oldproc;
245
246	bool noDraw;
247
248	bool hasCapture;
249
250	int newy;
251	int newx;
252}