May 21, 2006
---
import dwt.all, std.string;

pragma (lib, "dwt"         );
pragma (lib, "advapi32"    );
pragma (lib, "comctl32"    );
pragma (lib, "ole32"       );
pragma (lib, "uuid"        );
pragma (lib, "imm32_dwt"   );
pragma (lib, "msimg32_dwt" );
pragma (lib, "usp10_dwt"   );
pragma (lib, "oleaut32_dwt");
pragma (lib, "oleacc_dwt"  );

const void delegate(Event)
	onTextInput;

static this() {
	onTextInput = delegate(Event e) {
		MessageBox.showMsg(format("Got string of length %d: XXX %s XXX",
e.text.length, e.text));
	};
}

void main() {
	Display display = Display.getDefault();
	Shell shell = new Shell(display);

	Text input = new Text(shell, DWT.BORDER);
	input.handleEvent(null, DWT.Verify, onTextInput);

	input.pack();
	shell.pack();
	shell.open();

	while (!shell.isDisposed())
		if (!display.readAndDispatch())
			display.sleep();

	display.dispose();
}
---

Whenever I paste something into the text box in the above program, the message box comes up with strange results.

For instance, pasting "asdf", I get "Got string of length 11: XXX asdf". The latter "XXX" doesn't show up at all, and the string should be of length 4, not 11.

Making the delegate write to a file I see the whole string, and find that it's full of strange characters that shouldn't be there - memory not actually belonging to the array, but apparently taken because the length is incorrect.

Wrapping a toString() call around e.text seems to make it work, but why does this occur in the first place? Is the type of e.text really char[] in this case? And/or is there just a bug somewhere causing the length to be wrong?