28.4 Focus and autofocus

A slight interface problem arises when we use the accelerator keys or the mouse wheel to change the cursor type. The problem is this. We might sometimes have two views open in a splitter window, one on the left and one on the right. Suppose that in the view on the left, the Pin cursor is selected and in the view on the right, the Hand cursor is selected. Since the CPopView::OnSetCursor gets called by a view whenever the cursor passes over it, the cursor changes from Pin to Hand as we move it from left to right, and changes back to Pin when we move back to the left. This happens automatically, without any clicking of the mouse.

In an MDI program with several views open, only one view will have what is called the focus. This is the view whose caption bar is highlighted. In an MDI program, you can change the focus by clicking on one of the views, by pressing the Ctrl+F6 key combination, or by using the Window popup menu and selecting one of the views listed at the bottom. The focus view is the view which handles menu selections, toolbar buttons, and accelerator keys, and dialog information.

Now here's our problem. Suppose the left view has the focus. Its caption bar is highlighted. The cursor is over the left view and it shows the Pin cursor. Move the cursor over the right view, and the cursor changes to the Hand cursor. Now press 2 or rotate the mouse wheel. What happens?

The highlighted toolbar button showing the active cursor type changes. But the cursor doesn't change. And you're confused. You're an unhappy user. And then, to confuse you the more, if you move your cursor back over the left view, the cursor there is a hand instead of a pin.

The problem here is that the cursor's appearance is reflecting the toolstate of the view under the cursor. But the accelerator key and mouse wheel shortcuts are changing the toolstate of the focus view, which will not be the same as the view under the cursor until you've clicked the cursor in that view.

What to do? One option would be just to get rid of the confusing accelerator key and mouse wheel tool controls. Another option would be to use a context menu as described in Exercise 28.5. This option would work because once you right-click to open the context menu, you've clicked inside the underlying view and you've automatically made it your focus. An even easier option, that you can try in Exercise 28.7, is to let a right-click just swap the tool type without even showing you a context menu.

There is a more complicated solution, which is to have the focus change automatically to whatever view is currently under the cursor. The Pop Framework lets you choose this option by selecting Window | Autofocus. You can check the Pop Framework CPopView::OnSetCursor code in popview.cpp to see how it's implemented.

    Part I: Software Engineering and Computer Games
    Part II: Software Engineering and Computer Games Reference