Fixed a number of bugs in the handling of window parent and owner and added a regression test (based on the work of Bill Medland).