WIMP: A Window Manager Program

Time limit1sMemory limit128 MB

Summary
Simulate a window manager over a 1024x1024 screen, tracking overlapping windows and handling click, drag, zoom, close, create, and redraw events.
Level

Medium6 of 10

Topics
Simulation, Implementation, Geometry, Sorting
Solved
No attempts yet

Problem

A window manager takes care of creating, displaying, moving, and resizing a collection of windows in a graphical user interface. It also handles the input events (such as mouse clicks) related to those window-management tasks. In this problem you implement a simple WIndow Manager Program (WIMP).

The WIMP controls a screen of size 1024 × 1024 pixels, with the upper-left pixel at (0, 0). The x-coordinates run from 0 at the left edge to 1023 at the right edge, and the y-coordinates run from 0 at the top to 1023 at the bottom. All coordinates are integers. The user creates and manipulates rectangular windows by moving the mouse and clicking its button. A window is divided into 4 non-overlapping areas:

AreaLocation and size
Close boxupper-left corner of the window, 25 × 25 pixels
Zoom boxupper-right corner of the window, 25 × 25 pixels
Motion barthe top 25 pixels of the window, excluding the close and zoom boxes
Data areathe rest of the window

A window is always at least 51 × 26 so that all four areas are nonempty. Each window is given a unique integer identifier starting at 0 (the first window created has id 0, the second has id 1, and so on). Identifiers are never reused.

The WIMP accepts the following events:

EventMeaning
DN x ythe user pressed the mouse button at (x, y)
UP x ythe user released the mouse button at (x, y)
AT x ythe user moved the mouse to (x, y)
CR l t r bcreate a new window with left, top, right, and bottom positions l, t, r, b
REredraw all windows from back to front
ZZexit the WIMP

The values x, y, l, t, r, and b are all nonnegative integers within the screen dimensions. A CR event always produces a properly formed window. Because windows can overlap, on a RE event they must be redrawn from back (least recently brought to the front) to front (most recently brought to the front) so that they appear correctly overlapped.

The WIMP must keep track of all windows even when some overlap. Its rules are:

  1. A newly created window is always completely visible (on top of all other windows).
  2. A DN event anywhere on the visible part of a window selects that window and puts it on top, making the entire window visible. A DN event that is not on the visible part of any window does not affect the currently selected window.
  3. Closing and zooming each require a DN event followed by an UP event in the appropriate box. There may be one or more AT events in between. The DN and UP must be in the same box, but not necessarily at the exact same location.
  4. Closing a window removes it from the screen.
  5. Zooming is a toggle: it either makes the window occupy the entire screen or returns it to its original size.
  6. A DN event in the motion bar lets the window be moved. The move ends with an UP event. The window moves the same distance and direction that the mouse moved between the DN and the UP.
  7. AT events while moving a window must output the window's current position. AT events at any other time output nothing.
  8. A window that occupies the full screen cannot be moved.
  9. A window may move partially off the visible screen.
  10. AT events may happen at any time.

Input

The input consists of one or more lines, each containing a single event. A ZZ event marks the end of the input. All events belong to the same session.

Output

For each user action, output the corresponding message. On a RE event, output the position of every window from back to front using the format shown below.

ActionMessage
Create windowCreated window n at l, t, r, b
Select windowSelected window n
Close windowClosed window n
Move windowMoved window n to l, t, r, b
Zoom windowResized window n to l, t, r, b
RedrawWindow n at l, t, r, b

Examples2

  1. Example 1

    Input
    CR 0 0 200 200
    CR 50 50 250 250
    RE
    DN 195 5
    AT 50 50
    UP 198 6
    AT 100 100
    AT 1000 1000
    DN 1020 10
    UP 1020 10
    RE
    DN 100 100
    UP 800 0
    DN 0 700
    UP 1023 1023
    DN 50 10
    AT 70 70
    UP 100 100
    DN 60 60
    UP 60 60
    RE
    ZZ
    
    Expected output
    Created window 0 at 0, 0, 200, 200
    Created window 1 at 50, 50, 250, 250
    Window 0 at 0, 0, 200, 200
    Window 1 at 50, 50, 250, 250
    Selected window 0
    Resized window 0 to 0, 0, 1023, 1023
    Selected window 0
    Resized window 0 to 0, 0, 200, 200
    Window 1 at 50, 50, 250, 250
    Window 0 at 0, 0, 200, 200
    Selected window 0
    Selected window 0
    Moved window 0 to 20, 60, 220, 260
    Moved window 0 to 50, 90, 250, 290
    Selected window 1
    Closed window 1
    Window 0 at 50, 90, 250, 290
    
  2. Example 2

    Input
    CR 0 0 100 100
    CR 10 10 110 110
    CR 20 20 120 120
    RE
    ZZ
    
    Expected output
    Created window 0 at 0, 0, 100, 100
    Created window 1 at 10, 10, 110, 110
    Created window 2 at 20, 20, 120, 120
    Window 0 at 0, 0, 100, 100
    Window 1 at 10, 10, 110, 110
    Window 2 at 20, 20, 120, 120