From 1260bbcc769dff12cc12d6fe3a31ca57c735d57e Mon Sep 17 00:00:00 2001
From: "Adam D. Ruppe" <destructionator@gmail.com>
Date: Tue, 8 Sep 2015 09:21:31 -0400
Subject: [PATCH] working on a gui app

---
 gamehelpers.d   |  2 --
 minigui.d       | 42 ++++++++++++++++++++++++++++++------------
 simpledisplay.d | 14 +++++++++++++-
 3 files changed, 43 insertions(+), 15 deletions(-)

diff --git a/gamehelpers.d b/gamehelpers.d
index 8cc6416..36e5242 100644
--- a/gamehelpers.d
+++ b/gamehelpers.d
@@ -40,11 +40,9 @@
 			return window;
 		}
 
-		/*
 		final void fillAudioBuffer(short[] buffer) {
 
 		}
-		*/
 	}
 
 	void main() {
diff --git a/minigui.d b/minigui.d
index 1d6ff02..9388315 100644
--- a/minigui.d
+++ b/minigui.d
@@ -711,6 +711,18 @@ class Widget {
 	protected void privatePaint(ScreenPainter painter, int lox, int loy) {
 		painter.originX = lox + x;
 		painter.originY = loy + y;
+
+		static if(UsingSimpledisplayX11) {
+			XRectangle[1] rects;
+			rects[0] = XRectangle(cast(short)(lox + x), cast(short)(loy + y), cast(short) width, cast(short) height);
+			XSetClipRectangles(XDisplayConnection.get, painter.impl.gc, 0, 0, rects.ptr, 1, 0);
+		} else {
+			version(Windows) {
+				auto region = CreateRectRgn(lox + x, loy + y, lox + x + width, loy + y + height);
+				SelectClipRgn(painter.impl.hdc, region);
+				DeleteObject(region);
+			}
+		}
 		if(paint !is null)
 			paint(painter);
 		foreach(child; children)
@@ -810,6 +822,15 @@ class Window : Widget {
 		this.width = win.width;
 		this.height = win.height;
 		this.parentWindow = this;
+
+
+		win.windowResized = (int w, int h) {
+			this.width = w;
+			this.height = h;
+			recomputeChildLayout();
+			redraw();
+		};
+
 		win.setEventHandlers(
 			(MouseEvent e) {
 				dispatchMouseEvent(e);
@@ -931,6 +952,7 @@ class Window : Widget {
 		else
 		this.paint = (ScreenPainter painter) {
 			painter.fillColor = windowBackgroundColor;
+			painter.outlineColor = windowBackgroundColor;
 			painter.drawRectangle(Point(0, 0), this.width, this.height);
 		};
 	}
@@ -1047,15 +1069,8 @@ class Window : Widget {
 }
 
 class MainWindow : Window {
-	this() {
-		super(500, 500);
-
-		win.windowResized = (int w, int h) {
-			this.width = w;
-			this.height = h;
-			recomputeChildLayout();
-			redraw();
-		};
+	this(string title = null) {
+		super(500, 500, title);
 
 		defaultEventHandlers["mouseover"] = delegate void(Widget _this, Event event) {
 			if(this.statusBar !is null && event.target.statusTip.length)
@@ -1383,8 +1398,10 @@ class StatusBar : Widget {
 				_content = s;
 				SendMessageA(owner.hwnd, SB_SETTEXT, idx, cast(LPARAM) toStringzInternal(s));
 			} else {
-				_content = s;
-				owner.redraw();
+				if(_content != s) {
+					_content = s;
+					owner.redraw();
+				}
 			}
 		}
 	}
@@ -1975,7 +1992,8 @@ class TextLabel : Widget {
 		super(parent);
 		parentWindow = parent.parentWindow;
 		paint = (ScreenPainter painter) {
-			painter.drawText(Point(0, 0), this.label);
+			painter.outlineColor = Color.black;
+			painter.drawText(Point(0, 0), this.label, Point(width,height), TextAlignment.Right);
 		};
 	}
 
diff --git a/simpledisplay.d b/simpledisplay.d
index a229b66..4419f97 100644
--- a/simpledisplay.d
+++ b/simpledisplay.d
@@ -2155,7 +2155,9 @@ version(Windows) {
 				rect.bottom = y2;
 
 				uint mode = DT_LEFT;
-				if(alignment & TextAlignment.Center)
+				if(alignment & TextAlignment.Right)
+					mode = DT_RIGHT;
+				else if(alignment & TextAlignment.Center)
 					mode = DT_CENTER;
 
 				// FIXME: vcenter on windows only works with single line, but I want it to work in all cases
@@ -2769,6 +2771,7 @@ version(X11) {
 			// FIXME: this.window.width/height is probably wrong
 
 			// src x,y     then dest x, y
+			XSetClipMask(display, gc, None);
 			XCopyArea(display, d, destiny, gc, 0, 0, this.window.width, this.window.height, 0, 0);
 
 			XFreeGC(display, gc);
@@ -5354,6 +5357,15 @@ struct Visual
 	int XSetClipMask(Display*, GC, Pixmap);
 	int XSetClipOrigin(Display*, GC, int, int);
 
+	void XSetClipRectangles(Display*, GC, int, int, XRectangle*, int, int);
+
+	struct XRectangle {
+		short x;
+		short y;
+		ushort width;
+		ushort height;
+	}
+
 	void XSetWMName(Display*, Window, XTextProperty*);
 
 	enum ClipByChildren = 0;