The WinMgr class maintains the order of the text windows and passes them events from the user. You can implement WinMgr as a collection class of Win objects, but what type of collection class should you use?
On the screen, the windows appear stacked like sheets of paper on a desktop, with the topmost window being the active one. This suggests a collection class that implements a stack data structure. However, a stack data structure permits access to only the topmost element. WinMgr must have access to windows other than the active one in order to make non-active windows active. Therefore, a stack class is inappropriate. Some kind of list class that provides access to all the elements is needed.
Consider the order in which WinMgr must access the windows to see which one should receive a mouse event. More than one window may occupy the position where the mouse was clicked, but only the exposed one receives the event. To find the exposed window, WinMgr must test the windows in order, starting from the top of the stack and going to the bottom. However, suppose WinMgr must refresh the screen, painting all the overlapping windows over again. This time WinMgr must access the windows starting from the bottom of the stack and going to the top. Thus, WinMgr needs a collection class that permits iteration of its elements in both directions.
Accordingly, a List class that allows operations in both directions is appropriate. Such a class could be implemented with a doubly-linked list or an array. The WinMgr class can have a member object of type List.
Most of the behavior of WinMgr is implemented by its handleEvent function. The interface is therefore fairly simple:
class WinMgr
{
public:
void handleEvent( Event &action );
void addWindow( Win *newWindow );
void deleteWindow();
void repaint();
private:
List winlist;
};
Consider how WinMgr, Win, and ScrollBar work together in a typical scenario (This is illustrated in Figure 10.5). The user clicks the mouse on a scroll bar in a text window. An Event object, containing the location at which the click occurred, is sent to the WinMgr object, which queries the windows in order to see which one is exposed at that location. WinMgr then passes the event to the appropriate Win object, which in turn queries its scroll bars to see if either of them should receive it. It passes the event to the appropriate ScrollBar, which checks the location of the mouse click and interprets it as meaning “scroll forward one line.” The ScrollBar object creates a new Event object containing this scrolling information and sends it to its parent. The text window scrolls the text it contains and updates the position of the scroll bar's slider.