WidgetFocus
OpenTTD Development Documentation
External Links

OpenTTD GitHub
Contributing to OpenTTD - guidelines
OpenTTD Doxygen

General Reference

Coding style
Compiling OpenTTD
Debugging
Add a setting
Add a squirrel function
Understanding the SaveGame handler
Bumping the savegame version
Doing an OpenTTD release

Language and Strings

Manual of style
Format of langfiles
Using OpenTTD strings
List of special strings

Window System

Using the window system
Colour codes that exist in OpenTTD
Adding a text box
Understanding the widget focus system
GUI style guide

Multiplayer

The OpenTTD TCP protocol
The OpenTTD UDP protocol
Debugging desyncs
Server Admin Port development

Ingame Console

The console window
Console commands
Console variables
Using console scripting
Adding functions/commands to the console
Adding variables to the console
Console development history

Content APIs (modding frameworks)

Graphics and similar (NewGRF)
AI framework (NoAI)
GameScript framework (NoGO)
Social Integration

Other Reference

Map array (landscape grid)
Vehicles
Pathfinding
Train acceleration
Sound IDs

This document aims to give a short introduction to the widget focus system.

Contents

Focus levels

The focus system in OpenTTD has two levels. First there is a pointer that keeps track of which window has focus. Then each window instance keeps track of which widget is focused within that window. This means that a window that is not focused will still remember which widget was globally focused before another window stole the window focus. This widget might later become the globally focused widget if its window gets focused without changing the focused widget. (for example by clicking on the title bar)

The term globally focused widget refers to the focused widget of the focused window. Window::IsWidgetGloballyFocused will only return if given widget as well as the window instance is focused. Window::IsWidgetFocused on the other hand only requires that the given widget is focused within the window instance, but the window itself does not need to be focused.

The term local focus refers to focus within a window regardless if the window is focused or not.

How to interact with the focus system

For the last and most accurate details please refer to the source code.

Global functions

Window members

Unfocus

If you want to unfocus the currently locally focused widget but does not want to give another widget focus then you have to use the following code: (written as how to write from within a Window)

if (this->nested_focus != NULL) {
  this->nested_focus->SetDirty(this);
  this->nested_focus = NULL;
}

If the window however uses the old non-nested widget system the code above becomes:

if (this->focused_widget != NULL) {
  this->InvalidateWidget(this->focused_widget - this->widget);
  this->focused_widget = NULL;
}