The windowing system

This is a brief outline of how window creation, destruction and subclassing works. You should treat the source code (which is heavily commented) as authoritative. You don't need to know this to use JLib, but may find it interesting or helpful.

Calls to WinCreateWindow() only happen in the main thread. If attempts are made to create a window in another thread, the request is re-routed to thread one. The mechanism for this is in JIntern.cpp. This has consequences that dialogs must be run and windows must be destroyed in the main thread. The only exception to this rule is that object windows are not created in the main thread, for obvious reasons.

Once an instance of JWindow is given a window handle, it is added to the main window list. This is a hashtable of JWindow*, keyed by HWND. Duplicate entries are thrown out here. The window is not subclassed (in the PM sense of the phrase) until it needs to be, that is, subclass() is called, or a handler is attached. The window procedure for all JLib windows looks up the window in the main window list, and starts doing things with handlers. All the code for this is in JWindow.cpp

It is possible to create JWindow objects `by accident', using parent() or childWithID(). Equally, it's possible that there already is a JWindow for the reqired window. The job of resolving this is left to JWindowPtr. Given a HWND, it first checks in the main window list. Then it checks in the secondary window list. If it still hasn't found a match, it creates a bare JWindow and places this into the secondary window list.

The secondary window list is an active object, which periodically checks through any windows in itself. If it finds one which is also present in the main window list, it removes the window from itself. At library shutdown, the secondary window list is flushed; the main window list ought to be empty at this stage: if it isn't, then the user's program is at fault.


John Fairhurst 11th June 1998