Excellent question. This gets to the core of how curses renders the screen.
In curses, when two windows occupy the same location, the one that is displayed on top is determined by the order in which the windows are refreshed.
There is no inherent "Z-order" or depth property for standard curses windows. Curses operates like a painter: the last thing you paint is what you see.
The Refresh Mechanism Explained
The key functions to understand are wrefresh(), wnoutrefresh(), and doupdate().
wnoutrefresh(win): This function is the first phase of a two-phase update. It copies the contents of your window (win) to a virtual screen buffer in memory. It does not update the physical terminal. It just decides what should be on the screen.
doupdate(): This is the second phase. It takes the accumulated changes in the virtual screen buffer (from one or more wnoutrefresh calls) and efficiently sends only the changed characters to the physical terminal, minimizing cursor movement and redraw.
wrefresh(win): This is simply a convenience function that combines the two steps: it's equivalent to calling wnoutrefresh(win) followed by doupdate().
Therefore, the window whose wnoutrefresh() (or wrefresh()) call happens last before a doupdate() will be the one displayed on top.
Practical Example (Python)
Let's illustrate with a simple Python curses example. We'll create two overlapping windows and switch which one is on top.
python
import curses
import time
def main(stdscr):
# Clear screen and hide cursor
curses.curs_set(0)
stdscr.clear()
stdscr.refresh()
# Get screen dimensions
height, width = stdscr.getmaxyx()
win_height = 10
win_width = 30
# Calculate position for centered windows
start_y = (height - win_height) // 2
start_x = (width - win_width) // 2
# --- Create two windows at the same location ---
# Window 1 (blue background)
win1 = curses.newwin(win_height, win_width, start_y, start_x)
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE)
win1.bkgd(' ', curses.color_pair(1))
win1.box()
win1.addstr(2, 2, "This is Window 1 (Blue)")
# Window 2 (red background)
win2 = curses.newwin(win_height, win_width, start_y, start_x)
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_RED)
win2.bkgd(' ', curses.color_pair(2))
win2.box()
win2.addstr(4, 2, "This is Window 2 (Red)")
# --- SCENARIO 1: Refresh win1, then win2 ---
stdscr.addstr(0, 0, "Refreshing Win1 then Win2. Win2 should be on top. Press any key...")
stdscr.refresh()
win1.refresh() # Win1 is drawn to the terminal
time.sleep(1) # Pause to see it
win2.refresh() # Win2 is drawn OVER Win1
stdscr.getch() # Wait for user input
# --- SCENARIO 2: Refresh win2, then win1 ---
stdscr.clear()
stdscr.addstr(0, 0, "Refreshing Win2 then Win1. Win1 should be on top. Press any key...")
stdscr.refresh()
win2.refresh() # Win2 is drawn to the terminal
time.sleep(1) # Pause to see it
win1.refresh() # Win1 is drawn OVER Win2
stdscr.getch() # Wait for user input
if __name__ == '__main__':
curses.wrapper(main)
How to run it:
Save the code as a Python file (e.g., window_stack.py).
Run it from your terminal: python window_stack.py
What you will observe:
In the first scenario, even though win1 is refreshed first, the final win2.refresh() call paints over it, so you will see the red window on top.
In the second scenario, the order is reversed. win1.refresh() is called last, so you will see the blue window on top.
The "Proper" Solution: The panel Library
While manually managing the refresh order works, it can become very complicated. If you need to manage a stack of overlapping windows (like a desktop environment), the standard Curses library provides an extension called panel.
The panel library is specifically designed to manage the stacking order (Z-order) of windows.
Key Concepts of the panel library:
You associate each window with a panel.
Panels exist in a stack. You can move panels to the top, bottom, or hide/show them.
Instead of calling wrefresh() on individual windows, you call panel.update_panels(), followed by curses.doupdate(). Curses then intelligently redraws the screen based on the panel stacking order, correctly handling overlaps.
Summary:
Method
How it Works
Use Case
Standard Windows
The window refreshed last appears on top.
Simple UIs with few or no overlapping elements.
The panel Library
You manage an explicit stack of panels. update_panels() draws them in the correct Z-order.
Complex UIs with multiple overlapping, movable, or pop-up windows.