Landed in Server Window

In the last chapter, we have reached the app server Window class, its code is given below:

Window::Window(const BRect& frame, const char *name,
		window_look look, window_feel feel, uint32 flags, uint32 workspaces,
		::ServerWindow* window, DrawingEngine* drawingEngine)
	:
	fTitle(name),
	fFrame(frame),
	fScreen(NULL),

	fVisibleRegion(),
	fVisibleContentRegion(),
	fDirtyRegion(),

	fContentRegion(),
	fEffectiveDrawingRegion(),

	fVisibleContentRegionValid(false),
	fContentRegionValid(false),
	fEffectiveDrawingRegionValid(false),

	fRegionPool(),

	fWindow(window),
	fDrawingEngine(drawingEngine),
	fDesktop(window->Desktop()),

	fCurrentUpdateSession(&fUpdateSessions[0]),
	fPendingUpdateSession(&fUpdateSessions[1]),
	fUpdateRequested(false),
	fInUpdate(false),
	fUpdatesEnabled(false),

	// Windows start hidden
	fHidden(true),
	// Hidden is 1 or more
	fShowLevel(1),
	fMinimized(false),
	fIsFocus(false),

	fLook(look),
	fFeel(feel),
	fWorkspaces(workspaces),
	fCurrentWorkspace(-1),

	fMinWidth(1),
	fMaxWidth(32768),
	fMinHeight(1),
	fMaxHeight(32768),

	fWorkspacesViewCount(0)
{
	_InitWindowStack();

	// make sure our arguments are valid
	if (!IsValidLook(fLook))
		fLook = B_TITLED_WINDOW_LOOK;
	if (!IsValidFeel(fFeel))
		fFeel = B_NORMAL_WINDOW_FEEL;

	SetFlags(flags, NULL);

	if (fLook != B_NO_BORDER_WINDOW_LOOK && fCurrentStack.IsSet()) {
		// allocates a decorator
		::Decorator* decorator = Decorator();
		if (decorator != NULL) {
			decorator->GetSizeLimits(&fMinWidth, &fMinHeight, &fMaxWidth,
				&fMaxHeight);
		}
	}
	if (fFeel != kOffscreenWindowFeel)
		fWindowBehaviour.SetTo(gDecorManager.AllocateWindowBehaviour(this));

	// do we need to change our size to let the decorator fit?
	// _ResizeBy() will adapt the frame for validity before resizing
	if (feel == kDesktopWindowFeel) {
		// the desktop window spans over the whole screen
		// TODO: this functionality should be moved somewhere else
		//  (so that it is always used when the workspace is changed)
		uint16 width, height;
		uint32 colorSpace;
		float frequency;
		if (Screen() != NULL) {
			Screen()->GetMode(width, height, colorSpace, frequency);
// TODO: MOVE THIS AWAY!!! ResizeBy contains calls to virtual methods!
// Also, there is no TopView()!
			fFrame.OffsetTo(B_ORIGIN);
//			ResizeBy(width - frame.Width(), height - frame.Height(), NULL);
		}
	}

	STRACE(("Window %p, %s:\n", this, Name()));
	STRACE(("\tFrame: (%.1f, %.1f, %.1f, %.1f)\n", fFrame.left, fFrame.top,
		fFrame.right, fFrame.bottom));
	STRACE(("\tWindow %s\n", window ? window->Title() : "NULL"));
}

Now, in this chapter we will explore all the function calls that the above constructor does.

#1 _InitWindowStack()

This function is defined in src/servers/app/Window.cpp

WindowStack*
Window::_InitWindowStack()
{
	fCurrentStack = NULL;
	::Decorator* decorator = NULL;
	if (fLook != B_NO_BORDER_WINDOW_LOOK)
		decorator = gDecorManager.AllocateDecorator(this);

	WindowStack* stack = new(std::nothrow) WindowStack(decorator);
	if (stack == NULL)
		return NULL;

	if (stack->AddWindow(this) != true) {
		delete stack;
		return NULL;
	}
	fCurrentStack.SetTo(stack, true);
	return stack;
}

This method _InitWindowStack() is responsible for initializing the window's stack, which manages the stacking order of windows and their associated decorators. Let's break down this method:

	fCurrentStack = NULL;

It initializes the fCurrentStack member variable to NULL. This variable represents the current stack associated with the window.

	::Decorator* decorator = NULL;
	if (fLook != B_NO_BORDER_WINDOW_LOOK)
		decorator = gDecorManager.AllocateDecorator(this);

It checks if the window's look is not B_NO_BORDER_WINDOW_LOOK. If true, it allocates a decorator using the gDecorManager global instance.

	WindowStack* stack = new(std::nothrow) WindowStack(decorator);
	if (stack == NULL)
		return NULL;

It creates a new WindowStack object using the allocated decorator. If memory allocation fails, it returns NULL.

	if (stack->AddWindow(this) != true) {
		delete stack;
		return NULL;
	}

It attempts to add the window to the newly created stack using the AddWindow() method of the WindowStack class. If adding the window fails, it deletes the stack and returns NULL.

	fCurrentStack.SetTo(stack, true);
	return stack;

It sets the fCurrentStack member variable to point to the newly created stack and returns a pointer to the stack.


#2 WindowStack(decorator)

This constructor is defined at: src/servers/app/Window.cpp

WindowStack::WindowStack(::Decorator* decorator)
	:
	fDecorator(decorator)
{

}

This constructor initializes a WindowStack object with the provided decorator.
It takes a parameter decorator, which is a pointer to the decorator associated with the window stack.
The member initializer list initializes the fDecorator member variable with the provided decorator.

#3 stack->AddWindow(this)

This function is defined in: src/servers/app/Window.cpp

It is responsible for adding a window to the stack.

bool
WindowStack::AddWindow(Window* window, int32 position)
{
	if (position >= 0) {
		if (fWindowList.AddItem(window, position) == false)
			return false;
	} else if (fWindowList.AddItem(window) == false)
		return false;

	if (fWindowLayerOrder.AddItem(window) == false) {
		fWindowList.RemoveItem(window);
		return false;
	}
	return true;
}

Parameters:

  • Window* window: A pointer to the window that needs to be added to the stack.
  • int32 position (optional): The position at which the window should be inserted in the stack. If position is negative or not provided, the window will be added to the end of the stack.

Functionality:

  • The function first checks if the provided position is non-negative. If it is, it attempts to add the window to the fWindowList at the specified position using the AddItem function. If this operation fails (return false), indicating that the window could not be added at the specified position, the function return false.
  • If position is negative or not provided, the function attempts to add window to the end of the fWindowList using the AddItem function. Again, if this operation fails, the function returns false.
  • After successfully adding the window to the fWindowList, the function then adds the window to the fWindowLayerOrder list using the AddItem function.
  • If adding the window to the fWindowLayerOrder list fails, indicating that the window could not be added to the layer order after being added to the list of windows, the function removes the window from the fWindowList and returns false.
  • If all operations succeed, the function returns true, indicating that the window was successfully added to the stack.

Return Value:

  • true: Indicates that the window was successfully added to the stack.
  • false: Indicates that an error occurred while adding the window to the stack, such as failure to add the window to the list or layer order.