#include "opus_x64_compat.h"
#include "opus_x64_heap.h"
extern "C" {
#include "dac.h"
}

#include <algorithm>
#include <cstdio>
#include <cstdint>
#include <cstring>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

extern "C" {
extern void** hcabDlgCur;
extern std::uintptr_t wRefDlgCur;
extern HWND vhWndMsgBoxParent;
void GetCabSz(void**, char*, std::uint16_t, std::uint16_t);
int FSetCabSz(void**, const char*, std::uint16_t);
void OpusX64TraceRibbon(const char*, int, int, int, int, long, long, int);
}

/*
 * Flat, stateful implementation of the public SDM 2.21 dialog API used by
 * Opus.  The archive contains the SDM headers and 16-bit .obj files, but not
 * its C sources.  This layer retains the original CAB/TMC/HDLG contracts and
 * supplies the dialog state that the original Word modules expect.  Native
 * window creation is deliberately separate from this state layer.
 */

namespace {

using Word = std::uint16_t;
using Dword = std::uint32_t;
using Hdlg = Word;
using Tmc = Word;
using Hcab = void**;
using OriginalListProc = Word (*)(Word, char*, int, Word, Word, Word);
using FontValueProc = int (*)(const char*);
using FontNameFromValueProc = void (*)(int, char*, int);

struct Rec {
    int x;
    int y;
    int dx;
    int dy;
};

struct DltHeader {
    Rec rec;
    Word hid;
    Tmc tmc_sel_init;
    void* dialog_proc;
    Word base_item_count;
    Word border;
};

struct Dli {
    HWND hwnd;
    int dx;
    int dy;
    Dword flags;
    std::uintptr_t reference;
    unsigned char* runtime_items;
};

struct ControlState {
    Word value = 0;
    Dword selection = 0;
    bool enabled = true;
    bool visible = true;
    Word text_limit = 0xffff;
    Rec rectangle{};
    HWND window = nullptr;
    std::string text;
    std::vector<unsigned char> large_value;
    std::vector<std::string> entries;
};

struct DialogState {
    Hdlg handle = 0;
    DltHeader** template_handle = nullptr;
    Hcab cab = nullptr;
    HWND window = nullptr;
    std::uintptr_t reference = 0;
    Word hid = 0;
    Word sab = 0;
    Tmc focus = 0;
    Tmc default_tmc = 1;
    Tmc result_tmc = 2;
    bool visible = false;
    bool dying = false;
    bool modal = false;
    bool native_modal = false;
    bool commands_active = false;
    std::string caption;
    std::string current_directory;
    std::string file_pattern;
    HWND directory_label = nullptr;
    std::unordered_map<Tmc, ControlState> controls;
};

std::unordered_map<Hdlg, DialogState> g_dialogs;
DialogState g_no_dialog;
Hdlg g_next_dialog = 1;
Hdlg g_current_dialog = 0;
Hdlg g_focus_dialog = 0;
bool g_initialized = false;
bool g_noninteractive = false;
OriginalListProc g_list_font_name = nullptr;
OriginalListProc g_list_font_size = nullptr;
OriginalListProc g_list_styles = nullptr;
OriginalListProc g_list_character_color = nullptr;
FontValueProc g_font_name_to_value = nullptr;
FontValueProc g_font_size_to_value = nullptr;
FontNameFromValueProc g_font_name_from_value = nullptr;

constexpr Word kDlmInit = 0x0001;
constexpr Word kDlmTerm = 0x0003;
constexpr Word kDlmExit = 0x0004;
constexpr Word kDlmChange = 0x0005;
constexpr Word kDlmClick = 0x0006;
constexpr Word kDlmDblClk = 0x0007;
constexpr Word kDlmSetItemFocus = 0x000b;
constexpr Word kDlmKillItemFocus = 0x000c;
constexpr Word kDlmSetDialogFocus = 0x000d;
constexpr Word kDlmKillDialogFocus = 0x000e;
constexpr Word kDlmDialogClick = 0x0012;
constexpr UINT kWmCommitRibbonSelection = WM_APP + 0x352;
constexpr Word kIddNewDoc = 2;
constexpr Word kIddOpen = 3;
constexpr Word kIddSaveAs = 4;
constexpr Word kIddCharacter = 16;
constexpr Word kIddApplyStyle = 23;
constexpr Word kIddDefineStyle = 24;
constexpr Word kIddAbout = 44;
constexpr Word kCxtRibbonIconBar = 0x8005;
constexpr Word kCxtRulerIconBar = 0x8006;
constexpr Tmc kTmcOk = 1;
constexpr Tmc kTmcCancel = 2;
constexpr Tmc kTmcUserMin = 0x0400;
constexpr Tmc kTmcCharacterName = kTmcUserMin;
constexpr Tmc kTmcCharacterSize = kTmcUserMin + 2;
constexpr Tmc kTmcCharacterColor = kTmcUserMin + 4;
constexpr Tmc kTmcApplyStyle = kTmcUserMin;
constexpr Tmc kTmcApplyDefine = kTmcUserMin + 2;
constexpr Tmc kTmcApplyBanter = kTmcUserMin + 3;
constexpr Tmc kTmcDefineStyle = kTmcUserMin;
constexpr Tmc kTmcDefineChars = kTmcUserMin + 2;
constexpr Tmc kTmcDefineParas = kTmcUserMin + 3;
constexpr Tmc kTmcDefineTabs = kTmcUserMin + 4;
constexpr Tmc kTmcDefinePosition = kTmcUserMin + 5;
constexpr Tmc kTmcDefineOptions = kTmcUserMin + 6;
constexpr Tmc kTmcDefineBanter = kTmcUserMin + 7;
constexpr Tmc kTmcDefineBasedOn = kTmcUserMin + 8;
constexpr Tmc kTmcDefineNext = kTmcUserMin + 10;
constexpr Tmc kTmcDefineTemplate = kTmcUserMin + 12;
constexpr Tmc kTmcDefineCommit = kTmcUserMin + 13;
constexpr Tmc kTmcDefineDelete = kTmcUserMin + 14;
constexpr Tmc kTmcDefineRename = kTmcUserMin + 15;
constexpr Tmc kTmcDefineMerge = kTmcUserMin + 16;
constexpr Word kTmmCount = 2;
constexpr Word kTmmText = 3;
constexpr Word kUnknownListCount = 0xffff;
constexpr Tmc kTmcSummary = 0x0400;
constexpr Tmc kTmcNewDot = 0x0401;
constexpr Tmc kTmcRNewDoc = 0x0402;
constexpr Tmc kTmcRNewDot = 0x0403;
constexpr Tmc kTmcNewType = 0x0404;
constexpr Tmc kTmcNewTypeList = 0x0405;
constexpr Tmc kTmcOpenFileName = 0x0400;
constexpr Tmc kTmcOpenFileList = 0x0401;
constexpr Tmc kTmcOpenFileDir = 0x0402;
constexpr Tmc kTmcOpenCatalog = 0x0403;
constexpr Tmc kTmcOpenReadOnly = 0x0404;
constexpr Tmc kTmcSaveFile = kTmcUserMin;
constexpr Tmc kTmcSaveOptions = kTmcUserMin + 1;
constexpr Tmc kTmcSaveDirectoryText = kTmcUserMin + 2;
constexpr Tmc kTmcSaveDirectoryList = kTmcUserMin + 3;
constexpr Tmc kTmcSaveDirectory = kTmcUserMin + 4;
constexpr Tmc kTmcSaveFormatPrompt = kTmcUserMin + 5;
constexpr Tmc kTmcSaveFormat = kTmcUserMin + 6;
constexpr Tmc kTmcSaveQuick = kTmcUserMin + 7;
constexpr Tmc kTmcSaveBackup = kTmcUserMin + 8;
constexpr Tmc kTmcSaveLockAnnotations = kTmcUserMin + 9;
constexpr Word kOpenFileNameIag = 1;
constexpr std::size_t kOpenCabBytes = 24;
constexpr std::size_t kOpenReadOnlyOffset = 20;
constexpr Word kNewTypeIag = 1;
constexpr std::size_t kNewCabBytes = 24;
constexpr std::size_t kNewDotOffset = 16;

using NativeDialogProc = int (*)(Word, Tmc, Word, Word, Word);

void set_sds_handle(Hdlg current, Hdlg focus);
LRESULT CALLBACK native_dialog_window_proc(HWND, UINT, WPARAM, LPARAM);
void handle_dialog_command(Hdlg, WPARAM, LPARAM);

DialogState* find_dialog(const Hdlg dialog) {
    const auto found = g_dialogs.find(dialog);
    return found == g_dialogs.end() ? nullptr : &found->second;
}

DialogState& active_dialog() {
    auto* dialog = find_dialog(g_current_dialog);
    return dialog == nullptr ? g_no_dialog : *dialog;
}

#define g_dialog active_dialog()

ControlState& control(const Tmc tmc) {
    return g_dialog.controls[static_cast<Tmc>(tmc & ~0x8000u)];
}

const ControlState* find_control(const Tmc tmc) {
    const auto found =
        g_dialog.controls.find(static_cast<Tmc>(tmc & ~0x8000u));
    return found == g_dialog.controls.end() ? nullptr : &found->second;
}

void sync_current_dialog_globals() {
    const auto* dialog = find_dialog(g_current_dialog);
    hcabDlgCur = dialog == nullptr ? nullptr : dialog->cab;
    wRefDlgCur = dialog == nullptr ? 0 : dialog->reference;
    set_sds_handle(g_current_dialog, g_focus_dialog);
}

int scaled_x(const int value) {
    return MulDiv(value, dac.dxSysFontChar == 0 ? 8 : dac.dxSysFontChar, 4);
}

int scaled_y(const int value) {
    return MulDiv(value, dac.dySysFontChar == 0 ? 16 : dac.dySysFontChar, 8);
}

ATOM ensure_native_dialog_class() {
    static ATOM atom = 0;
    if (atom != 0) {
        return atom;
    }
    WNDCLASSEXA cls{};
    cls.cbSize = sizeof(cls);
    cls.style = CS_DBLCLKS;
    cls.lpfnWndProc = native_dialog_window_proc;
    cls.hInstance = GetModuleHandleW(nullptr);
    cls.hCursor = LoadCursorA(nullptr, IDC_ARROW);
    cls.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BTNFACE + 1);
    cls.lpszClassName = "OpusSdmDialog";
    atom = RegisterClassExA(&cls);
    if (atom == 0 && GetLastError() == ERROR_CLASS_ALREADY_EXISTS) {
        atom = 1;
    }
    return atom;
}

HWND create_dialog_host(const DialogState& dialog, const Dli* initializer) {
    if (dialog.modal &&
        (dialog.hid == kIddOpen || dialog.hid == kIddNewDoc ||
         dialog.hid == kIddSaveAs || dialog.hid == kIddAbout ||
         dialog.hid == kIddCharacter || dialog.hid == kIddApplyStyle ||
         dialog.hid == kIddDefineStyle)) {
        if (ensure_native_dialog_class() == 0) {
            return nullptr;
        }

        HWND owner = initializer != nullptr ? initializer->hwnd : nullptr;
        if (owner == nullptr || !IsWindow(owner)) {
            owner = vhWndMsgBoxParent;
        }
        if (owner == nullptr || !IsWindow(owner)) {
            owner = GetActiveWindow();
        }

        int client_width = scaled_x(dialog.hid == kIddNewDoc ? 127 : 206);
        int client_height = scaled_y(dialog.hid == kIddNewDoc ? 114 : 104);
        if (dialog.template_handle != nullptr &&
            *dialog.template_handle != nullptr) {
            client_width = (std::max)(1, scaled_x(
                (*dialog.template_handle)->rec.dx));
            client_height = (std::max)(1, scaled_y(
                (*dialog.template_handle)->rec.dy));
        }
        constexpr DWORD style =
            WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_CLIPCHILDREN;
        constexpr DWORD extended_style =
            WS_EX_DLGMODALFRAME | WS_EX_CONTROLPARENT;
        RECT window_rect{0, 0, client_width, client_height};
        AdjustWindowRectEx(&window_rect, style, false, extended_style);
        const int width = window_rect.right - window_rect.left;
        const int height = window_rect.bottom - window_rect.top;

        RECT anchor{};
        if (owner == nullptr || !GetWindowRect(owner, &anchor)) {
            SystemParametersInfoA(SPI_GETWORKAREA, 0, &anchor, 0);
        }
        const int x = anchor.left +
                      (anchor.right - anchor.left - width) / 2;
        const int y = anchor.top +
                      (anchor.bottom - anchor.top - height) / 2;
        const char* caption = "Open";
        switch (dialog.hid) {
            case kIddNewDoc:
                caption = "New";
                break;
            case kIddSaveAs:
                caption = "Save As";
                break;
            case kIddCharacter:
                caption = "Character";
                break;
            case kIddApplyStyle:
                caption = "Apply Style";
                break;
            case kIddDefineStyle:
                caption = "Define Style";
                break;
            case kIddAbout:
                caption = "About Microsoft Word";
                break;
        }
        return CreateWindowExA(
            extended_style, "OpusSdmDialog", caption, style, x, y, width,
            height, owner, nullptr, GetModuleHandleW(nullptr),
            reinterpret_cast<void*>(static_cast<std::uintptr_t>(
                dialog.handle)));
    }

    if (initializer == nullptr || initializer->hwnd == nullptr ||
        !IsWindow(initializer->hwnd)) {
        return nullptr;
    }
    if (ensure_native_dialog_class() == 0) {
        return nullptr;
    }

    int x = initializer->dx;
    int y = initializer->dy;
    int width = 1;
    int height = 1;
    if (dialog.template_handle != nullptr &&
        *dialog.template_handle != nullptr) {
        const auto& rec = (*dialog.template_handle)->rec;
        x += scaled_x(rec.x);
        y += scaled_y(rec.y);
        width = (std::max)(1, scaled_x(rec.dx));
        height = (std::max)(1, scaled_y(rec.dy));
    }

    /* HdlgStartDlg is used by Opus only for ribbon/ruler child dialogs.
       Win16 propagated WM_SETVISIBLE when their icon-bar parent was shown;
       Win64 does not.  Keep the child style visible so parent visibility is
       inherited without depending on that retired message. */
    const DWORD style =
        WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
    return CreateWindowExA(
        0, "OpusSdmDialog", "", style, x, y, width, height,
        initializer->hwnd, nullptr, GetModuleHandleW(nullptr),
        reinterpret_cast<void*>(static_cast<std::uintptr_t>(dialog.handle)));
}

HWND create_native_control(DialogState& dialog, const Tmc tmc,
                           const char* window_class, const char* caption,
                           const Rec& rectangle, const DWORD control_style) {
    if (dialog.window == nullptr || !IsWindow(dialog.window)) {
        return nullptr;
    }
    auto& state = dialog.controls[tmc];
    state.rectangle = rectangle;
    state.text = caption == nullptr ? "" : caption;
    state.window = CreateWindowExA(
        0, window_class, state.text.c_str(),
        WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | control_style,
        scaled_x(rectangle.x), scaled_y(rectangle.y),
        (std::max)(1, scaled_x(rectangle.dx)),
        (std::max)(1, scaled_y(rectangle.dy)), dialog.window,
        reinterpret_cast<HMENU>(static_cast<std::uintptr_t>(tmc)),
        GetModuleHandleW(nullptr), nullptr);
    if (state.window != nullptr) {
        SendMessageA(state.window, WM_SETFONT,
                     reinterpret_cast<WPARAM>(GetStockObject(DEFAULT_GUI_FONT)),
                     false);
    }
    return state.window;
}

void create_static_text(DialogState& dialog, const char* caption,
                        const Rec& rectangle) {
    if (dialog.window == nullptr || !IsWindow(dialog.window)) {
        return;
    }
    const HWND window = CreateWindowExA(
        0, "STATIC", caption,
        WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | SS_LEFT,
        scaled_x(rectangle.x), scaled_y(rectangle.y),
        (std::max)(1, scaled_x(rectangle.dx)),
        (std::max)(1, scaled_y(rectangle.dy)), dialog.window, nullptr,
        GetModuleHandleW(nullptr), nullptr);
    if (window != nullptr) {
        SendMessageA(window, WM_SETFONT,
                     reinterpret_cast<WPARAM>(GetStockObject(DEFAULT_GUI_FONT)),
                     false);
    }
}

void create_untracked_control(DialogState& dialog, const char* window_class,
                              const char* caption, const Rec& rectangle,
                              const DWORD control_style) {
    if (dialog.window == nullptr || !IsWindow(dialog.window)) {
        return;
    }
    const HWND window = CreateWindowExA(
        0, window_class, caption,
        WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | control_style,
        scaled_x(rectangle.x), scaled_y(rectangle.y),
        (std::max)(1, scaled_x(rectangle.dx)),
        (std::max)(1, scaled_y(rectangle.dy)), dialog.window, nullptr,
        GetModuleHandleW(nullptr), nullptr);
    if (window != nullptr) {
        SendMessageA(window, WM_SETFONT,
                     reinterpret_cast<WPARAM>(GetStockObject(DEFAULT_GUI_FONT)),
                     false);
    }
}

bool window_is_class(const HWND window, const char* expected) {
    if (window == nullptr || !IsWindow(window)) {
        return false;
    }
    char actual[32] = {};
    return GetClassNameA(window, actual, static_cast<int>(sizeof(actual))) != 0 &&
           _stricmp(actual, expected) == 0;
}

void add_entry_to_native_control(const ControlState& state,
                                 const std::string& entry) {
    if (state.window == nullptr || !IsWindow(state.window)) {
        return;
    }
    const UINT message =
        window_is_class(state.window, "COMBOBOX") ? CB_ADDSTRING : LB_ADDSTRING;
    SendMessageA(state.window, message, 0,
                 reinterpret_cast<LPARAM>(entry.c_str()));
}

void reset_native_list(ControlState& state) {
    if (state.window == nullptr || !IsWindow(state.window)) {
        return;
    }
    const UINT message = window_is_class(state.window, "COMBOBOX")
                             ? CB_RESETCONTENT
                             : LB_RESETCONTENT;
    SendMessageA(state.window, message, 0, 0);
}

std::string join_path(const std::string& directory,
                      const std::string& leaf) {
    if (directory.empty()) {
        return leaf;
    }
    const char last = directory.back();
    return directory + (last == '\\' || last == '/' ? "" : "\\") + leaf;
}

void add_native_list_entry(DialogState& dialog, const Tmc tmc,
                           const std::string& entry) {
    auto& state = dialog.controls[tmc];
    state.entries.push_back(entry);
    add_entry_to_native_control(state, entry);
}

void set_open_directory_label(DialogState& dialog) {
    if (dialog.directory_label != nullptr &&
        IsWindow(dialog.directory_label)) {
        SetWindowTextA(dialog.directory_label,
                       dialog.current_directory.c_str());
    }
}

void establish_open_directory(DialogState& dialog,
                              const std::string& specification) {
    char full_path[32768] = {};
    char* file_part = nullptr;
    const std::string source = specification.empty() ? "*.*" : specification;
    const DWORD length = GetFullPathNameA(
        source.c_str(), static_cast<DWORD>(sizeof(full_path)), full_path,
        &file_part);
    if (length == 0 || length >= sizeof(full_path)) {
        GetCurrentDirectoryA(static_cast<DWORD>(sizeof(full_path)), full_path);
        dialog.current_directory = full_path;
        dialog.file_pattern = "*.*";
        return;
    }

    const DWORD attributes = GetFileAttributesA(full_path);
    if (attributes != INVALID_FILE_ATTRIBUTES &&
        (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
        dialog.current_directory = full_path;
        dialog.file_pattern = "*.*";
        return;
    }

    dialog.file_pattern =
        file_part == nullptr || *file_part == '\0' ? "*.*" : file_part;
    if (file_part == nullptr || file_part == full_path) {
        GetCurrentDirectoryA(static_cast<DWORD>(sizeof(full_path)), full_path);
        dialog.current_directory = full_path;
    } else {
        std::size_t directory_length =
            static_cast<std::size_t>(file_part - full_path);
        while (directory_length > 3 &&
               (full_path[directory_length - 1] == '\\' ||
                full_path[directory_length - 1] == '/')) {
            --directory_length;
        }
        dialog.current_directory.assign(full_path, directory_length);
    }
}

void populate_open_lists(DialogState& dialog) {
    auto& files = dialog.controls[kTmcOpenFileList];
    auto& directories = dialog.controls[kTmcOpenFileDir];
    files.entries.clear();
    directories.entries.clear();
    if (files.window != nullptr) {
        SendMessageA(files.window, LB_RESETCONTENT, 0, 0);
    }
    if (directories.window != nullptr) {
        SendMessageA(directories.window, LB_RESETCONTENT, 0, 0);
    }

    WIN32_FIND_DATAA find_data{};
    HANDLE find = FindFirstFileA(
        join_path(dialog.current_directory, dialog.file_pattern).c_str(),
        &find_data);
    if (find != INVALID_HANDLE_VALUE) {
        do {
            if ((find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) {
                add_native_list_entry(dialog, kTmcOpenFileList,
                                      find_data.cFileName);
            }
        } while (FindNextFileA(find, &find_data));
        FindClose(find);
    }

    char parent_path[32768] = {};
    const std::string parent_spec = join_path(dialog.current_directory, "..");
    if (GetFullPathNameA(parent_spec.c_str(),
                         static_cast<DWORD>(sizeof(parent_path)), parent_path,
                         nullptr) != 0 &&
        _stricmp(parent_path, dialog.current_directory.c_str()) != 0) {
        add_native_list_entry(dialog, kTmcOpenFileDir, "[..]");
    }
    find = FindFirstFileA(join_path(dialog.current_directory, "*.*").c_str(),
                          &find_data);
    if (find != INVALID_HANDLE_VALUE) {
        do {
            if ((find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 &&
                std::strcmp(find_data.cFileName, ".") != 0 &&
                std::strcmp(find_data.cFileName, "..") != 0) {
                add_native_list_entry(
                    dialog, kTmcOpenFileDir,
                    "[" + std::string(find_data.cFileName) + "]");
            }
        } while (FindNextFileA(find, &find_data));
        FindClose(find);
    }
    set_open_directory_label(dialog);
}

void read_open_cab(DialogState& dialog) {
    char file_name[32768] = {};
    if (dialog.cab != nullptr) {
        GetCabSz(dialog.cab, file_name,
                 static_cast<Word>((std::min)(sizeof(file_name),
                                              std::size_t{0xffffu})),
                 kOpenFileNameIag);
    }
    auto& edit = dialog.controls[kTmcOpenFileName];
    edit.text = file_name;
    if (edit.window != nullptr) {
        SetWindowTextA(edit.window, edit.text.c_str());
    }

    if (dialog.cab != nullptr && *dialog.cab != nullptr &&
        OpusCbOfH(dialog.cab) >= kOpenCabBytes) {
        int read_only = 0;
        std::memcpy(&read_only,
                    static_cast<const unsigned char*>(*dialog.cab) +
                        kOpenReadOnlyOffset,
                    sizeof(read_only));
        auto& checkbox = dialog.controls[kTmcOpenReadOnly];
        checkbox.value = read_only != 0;
        if (checkbox.window != nullptr) {
            SendMessageA(checkbox.window, BM_SETCHECK,
                         checkbox.value ? BST_CHECKED : BST_UNCHECKED, 0);
        }
    }
    establish_open_directory(dialog, edit.text);
    populate_open_lists(dialog);
}

void sync_open_cab(DialogState& dialog) {
    auto edit = dialog.controls.find(kTmcOpenFileName);
    if (edit != dialog.controls.end() && edit->second.window != nullptr) {
        const int length = GetWindowTextLengthA(edit->second.window);
        std::vector<char> buffer(static_cast<std::size_t>(length) + 1);
        GetWindowTextA(edit->second.window, buffer.data(),
                       static_cast<int>(buffer.size()));
        edit->second.text = buffer.data();
    }
    if (dialog.cab != nullptr && edit != dialog.controls.end()) {
        FSetCabSz(dialog.cab, edit->second.text.c_str(), kOpenFileNameIag);
    }

    auto checkbox = dialog.controls.find(kTmcOpenReadOnly);
    if (checkbox != dialog.controls.end() && checkbox->second.window != nullptr) {
        checkbox->second.value = static_cast<Word>(
            SendMessageA(checkbox->second.window, BM_GETCHECK, 0, 0) ==
            BST_CHECKED);
    }
    if (dialog.cab != nullptr && *dialog.cab != nullptr &&
        OpusCbOfH(dialog.cab) >= kOpenCabBytes &&
        checkbox != dialog.controls.end()) {
        const int read_only = checkbox->second.value != 0;
        std::memcpy(static_cast<unsigned char*>(*dialog.cab) +
                        kOpenReadOnlyOffset,
                    &read_only, sizeof(read_only));
    }
}

void populate_new_type_list(DialogState& dialog) {
    auto& list = dialog.controls[kTmcNewTypeList];
    list.entries.clear();
    if (list.window != nullptr) {
        SendMessageA(list.window, LB_RESETCONTENT, 0, 0);
    }

    const std::string initial = dialog.controls[kTmcNewType].text;
    add_native_list_entry(dialog, kTmcNewTypeList,
                          initial.empty() ? "NORMAL" : initial);

    WIN32_FIND_DATAA find_data{};
    HANDLE find = FindFirstFileA("*.DOT", &find_data);
    if (find == INVALID_HANDLE_VALUE) {
        return;
    }
    do {
        if ((find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
            continue;
        }
        const auto duplicate = std::find_if(
            list.entries.begin(), list.entries.end(),
            [&find_data](const std::string& entry) {
                return _stricmp(entry.c_str(), find_data.cFileName) == 0;
            });
        if (duplicate == list.entries.end()) {
            add_native_list_entry(dialog, kTmcNewTypeList,
                                  find_data.cFileName);
        }
    } while (FindNextFileA(find, &find_data));
    FindClose(find);
}

void read_new_cab(DialogState& dialog) {
    char type_name[32768] = {};
    if (dialog.cab != nullptr) {
        GetCabSz(dialog.cab, type_name,
                 static_cast<Word>((std::min)(sizeof(type_name),
                                              std::size_t{0xffffu})),
                 kNewTypeIag);
    }
    auto& edit = dialog.controls[kTmcNewType];
    edit.text = type_name;
    if (edit.window != nullptr) {
        SetWindowTextA(edit.window, edit.text.c_str());
    }

    bool new_template = false;
    if (dialog.cab != nullptr && *dialog.cab != nullptr &&
        OpusCbOfH(dialog.cab) >= kNewCabBytes) {
        int value = 0;
        std::memcpy(&value,
                    static_cast<const unsigned char*>(*dialog.cab) +
                        kNewDotOffset,
                    sizeof(value));
        new_template = value != 0;
    }
    dialog.controls[kTmcNewDot].value = new_template;
    dialog.controls[kTmcRNewDoc].value = !new_template;
    dialog.controls[kTmcRNewDot].value = new_template;
    if (dialog.controls[kTmcRNewDoc].window != nullptr) {
        SendMessageA(dialog.controls[kTmcRNewDoc].window, BM_SETCHECK,
                     new_template ? BST_UNCHECKED : BST_CHECKED, 0);
    }
    if (dialog.controls[kTmcRNewDot].window != nullptr) {
        SendMessageA(dialog.controls[kTmcRNewDot].window, BM_SETCHECK,
                     new_template ? BST_CHECKED : BST_UNCHECKED, 0);
    }
    populate_new_type_list(dialog);
}

void sync_new_cab(DialogState& dialog) {
    auto edit = dialog.controls.find(kTmcNewType);
    if (edit != dialog.controls.end() && edit->second.window != nullptr) {
        const int length = GetWindowTextLengthA(edit->second.window);
        std::vector<char> buffer(static_cast<std::size_t>(length) + 1);
        GetWindowTextA(edit->second.window, buffer.data(),
                       static_cast<int>(buffer.size()));
        edit->second.text = buffer.data();
    }
    if (dialog.cab != nullptr && edit != dialog.controls.end()) {
        FSetCabSz(dialog.cab, edit->second.text.c_str(), kNewTypeIag);
    }

    bool new_template = false;
    const auto radio = dialog.controls.find(kTmcRNewDot);
    if (radio != dialog.controls.end() && radio->second.window != nullptr) {
        new_template = SendMessageA(radio->second.window, BM_GETCHECK, 0, 0) ==
                       BST_CHECKED;
    }
    dialog.controls[kTmcNewDot].value = new_template;
    if (dialog.cab != nullptr && *dialog.cab != nullptr &&
        OpusCbOfH(dialog.cab) >= kNewCabBytes) {
        const int value = new_template;
        std::memcpy(static_cast<unsigned char*>(*dialog.cab) + kNewDotOffset,
                    &value, sizeof(value));
    }
}

void materialize_new_template(DialogState& dialog) {
    if (dialog.hid != kIddNewDoc || dialog.window == nullptr) {
        return;
    }
    create_native_control(dialog, kTmcOk, "BUTTON", "OK", {74, 6, 46, 14},
                          WS_TABSTOP | BS_DEFPUSHBUTTON);
    create_native_control(dialog, kTmcCancel, "BUTTON", "Cancel",
                          {74, 23, 46, 14}, WS_TABSTOP | BS_PUSHBUTTON);
    create_native_control(dialog, kTmcSummary, "BUTTON", "&Summary...",
                          {74, 39, 46, 14}, WS_TABSTOP | BS_PUSHBUTTON);
    create_untracked_control(dialog, "BUTTON", "New", {5, 2, 61, 36},
                             BS_GROUPBOX);
    create_native_control(dialog, kTmcRNewDoc, "BUTTON", "&Document",
                          {9, 12, 42, 10},
                          WS_TABSTOP | WS_GROUP | BS_AUTORADIOBUTTON);
    create_native_control(dialog, kTmcRNewDot, "BUTTON", "&Template",
                          {9, 24, 42, 10},
                          WS_TABSTOP | BS_AUTORADIOBUTTON);
    create_static_text(dialog, "&Use Template:", {5, 42, 53, 10});
    create_native_control(dialog, kTmcNewType, "EDIT", "",
                          {5, 53, 61, 12},
                          WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL);
    create_native_control(dialog, kTmcNewTypeList, "LISTBOX", "",
                          {8, 65, 68, 40},
                          WS_TABSTOP | WS_BORDER | WS_VSCROLL | LBS_NOTIFY |
                              LBS_SORT | LBS_NOINTEGRALHEIGHT);
    dialog.caption = "New";
    dialog.native_modal = true;
    read_new_cab(dialog);
}

void materialize_open_template(DialogState& dialog) {
    if (dialog.hid != kIddOpen || dialog.window == nullptr) {
        return;
    }
    create_native_control(dialog, kTmcOpenFileName, "EDIT", "",
                          {67, 6, 83, 12},
                          WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL);
    create_static_text(dialog, "Open File &Name:", {5, 7, 61, 9});
    create_native_control(dialog, kTmcOpenFileList, "LISTBOX", "",
                          {5, 32, 60, 64},
                          WS_TABSTOP | WS_BORDER | WS_VSCROLL | LBS_NOTIFY |
                              LBS_SORT | LBS_NOINTEGRALHEIGHT);
    create_static_text(dialog, "&Files:", {5, 21, 25, 9});
    create_native_control(dialog, kTmcOpenFileDir, "LISTBOX", "",
                          {71, 48, 65, 48},
                          WS_TABSTOP | WS_BORDER | WS_VSCROLL | LBS_NOTIFY |
                              LBS_SORT | LBS_NOINTEGRALHEIGHT);
    create_static_text(dialog, "&Directories:", {71, 38, 50, 9});
    dialog.directory_label = CreateWindowExA(
        0, "STATIC", "", WS_CHILD | WS_VISIBLE | SS_LEFT,
        scaled_x(70), scaled_y(24), scaled_x(78), scaled_y(8),
        dialog.window, nullptr, GetModuleHandleW(nullptr), nullptr);
    if (dialog.directory_label != nullptr) {
        SendMessageA(dialog.directory_label, WM_SETFONT,
                     reinterpret_cast<WPARAM>(GetStockObject(DEFAULT_GUI_FONT)),
                     false);
    }
    create_native_control(dialog, kTmcOk, "BUTTON", "OK",
                          {154, 5, 47, 14},
                          WS_TABSTOP | BS_DEFPUSHBUTTON);
    create_native_control(dialog, kTmcCancel, "BUTTON", "Cancel",
                          {154, 22, 47, 14}, WS_TABSTOP | BS_PUSHBUTTON);
    create_native_control(dialog, kTmcOpenCatalog, "BUTTON", "F&ind...",
                          {154, 39, 47, 14}, WS_TABSTOP | BS_PUSHBUTTON);
    create_native_control(dialog, kTmcOpenReadOnly, "BUTTON", "&Read Only",
                          {155, 56, 47, 12},
                          WS_TABSTOP | BS_AUTOCHECKBOX);
    dialog.caption = "Open";
    dialog.native_modal = true;
    read_open_cab(dialog);
}

std::string read_cab_string(const DialogState& dialog, const Word iag) {
    char buffer[1024] = {};
    if (dialog.cab != nullptr) {
        GetCabSz(dialog.cab, buffer, static_cast<Word>(sizeof(buffer)), iag);
    }
    return buffer;
}

void materialize_about_template(DialogState& dialog) {
    if (dialog.hid != kIddAbout || dialog.window == nullptr) {
        return;
    }

    const auto static_from_cab = [&dialog](const Word iag, const Rec& rec,
                                           const DWORD alignment) {
        const std::string text = read_cab_string(dialog, iag);
        create_untracked_control(dialog, "STATIC", text.c_str(), rec,
                                 alignment);
    };
    static_from_cab(1, {4, 5, 192, 9}, SS_CENTER);
    static_from_cab(2, {4, 22, 192, 9}, SS_CENTER);
    static_from_cab(3, {4, 34, 192, 9}, SS_CENTER);
    create_native_control(dialog, kTmcOk, "BUTTON", "OK",
                          {84, 48, 34, 14},
                          WS_TABSTOP | BS_DEFPUSHBUTTON);
    create_untracked_control(dialog, "STATIC", "", {0, 64, 205, 4},
                             SS_ETCHEDHORZ);
    create_untracked_control(dialog, "STATIC", "Conventional Memory:",
                             {5, 74, 110, 9}, SS_RIGHT);
    create_untracked_control(dialog, "STATIC", "Expanded Memory:",
                             {5, 83, 110, 9}, SS_RIGHT);
    create_untracked_control(dialog, "STATIC", "Math Co-processor:",
                             {5, 92, 110, 9}, SS_RIGHT);
    create_untracked_control(dialog, "STATIC", "Disk Space:",
                             {5, 101, 110, 9}, SS_RIGHT);
    static_from_cab(4, {120, 74, 76, 9}, SS_LEFT);
    static_from_cab(5, {120, 83, 76, 9}, SS_LEFT);
    static_from_cab(6, {120, 92, 76, 9}, SS_LEFT);
    static_from_cab(7, {120, 101, 76, 9}, SS_LEFT);
    dialog.caption = "About Microsoft Word";
    dialog.native_modal = true;
}

struct CabSaveNative {
    Word simple_words;
    Word handle_words;
    Word sab;
    Word alignment;
    char** file_name;
    int directory_list;
    int format;
    int quick_save;
    int backup;
    int lock_annotations;
    int options;
};

CabSaveNative* save_cab(DialogState& dialog) {
    if (dialog.cab == nullptr || *dialog.cab == nullptr ||
        OpusCbOfH(dialog.cab) < sizeof(CabSaveNative)) {
        return nullptr;
    }
    return static_cast<CabSaveNative*>(*dialog.cab);
}

void set_save_check(DialogState& dialog, const Tmc tmc, const bool checked) {
    auto& state = dialog.controls[tmc];
    state.value = checked;
    if (state.window != nullptr && IsWindow(state.window)) {
        SendMessageA(state.window, BM_SETCHECK,
                     checked ? BST_CHECKED : BST_UNCHECKED, 0);
    }
}

void populate_save_directories(DialogState& dialog) {
    char directory[32768] = {};
    if (dialog.current_directory.empty()) {
        GetCurrentDirectoryA(static_cast<DWORD>(sizeof(directory)), directory);
        dialog.current_directory = directory;
    }

    auto& list = dialog.controls[kTmcSaveDirectoryList];
    list.entries.clear();
    if (list.window != nullptr) {
        SendMessageA(list.window, LB_RESETCONTENT, 0, 0);
    }

    char parent[32768] = {};
    const std::string parent_spec = join_path(dialog.current_directory, "..");
    if (GetFullPathNameA(parent_spec.c_str(),
                         static_cast<DWORD>(sizeof(parent)), parent,
                         nullptr) != 0 &&
        _stricmp(parent, dialog.current_directory.c_str()) != 0) {
        add_native_list_entry(dialog, kTmcSaveDirectoryList, "[..]");
    }

    WIN32_FIND_DATAA find_data{};
    HANDLE find = FindFirstFileA(
        join_path(dialog.current_directory, "*.*").c_str(), &find_data);
    if (find != INVALID_HANDLE_VALUE) {
        do {
            if ((find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 &&
                std::strcmp(find_data.cFileName, ".") != 0 &&
                std::strcmp(find_data.cFileName, "..") != 0) {
                add_native_list_entry(
                    dialog, kTmcSaveDirectoryList,
                    "[" + std::string(find_data.cFileName) + "]");
            }
        } while (FindNextFileA(find, &find_data));
        FindClose(find);
    }

    auto& label = dialog.controls[kTmcSaveDirectory];
    label.text = dialog.current_directory;
    if (label.window != nullptr) {
        SetWindowTextA(label.window, label.text.c_str());
    }
}

void set_save_options_visible(DialogState& dialog, const bool visible) {
    const Tmc option_controls[] = {
        kTmcSaveFormatPrompt, kTmcSaveFormat, kTmcSaveQuick,
        kTmcSaveBackup, kTmcSaveLockAnnotations};
    for (const Tmc tmc : option_controls) {
        auto found = dialog.controls.find(tmc);
        if (found == dialog.controls.end()) {
            continue;
        }
        found->second.visible = visible;
        if (found->second.window != nullptr) {
            ShowWindow(found->second.window, visible ? SW_SHOWNA : SW_HIDE);
        }
    }
    if (dialog.window == nullptr || !IsWindow(dialog.window)) {
        return;
    }
    RECT client{0, 0, scaled_x(150), scaled_y(visible ? 157 : 102)};
    const DWORD style = static_cast<DWORD>(GetWindowLongPtrA(
        dialog.window, GWL_STYLE));
    const DWORD extended_style = static_cast<DWORD>(GetWindowLongPtrA(
        dialog.window, GWL_EXSTYLE));
    AdjustWindowRectEx(&client, style, false, extended_style);
    SetWindowPos(dialog.window, nullptr, 0, 0,
                 client.right - client.left, client.bottom - client.top,
                 SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
}

void read_save_cab(DialogState& dialog) {
    auto& edit = dialog.controls[kTmcSaveFile];
    edit.text = read_cab_string(dialog, 1);
    if (edit.window != nullptr) {
        SetWindowTextA(edit.window, edit.text.c_str());
    }

    if (auto* cab = save_cab(dialog); cab != nullptr) {
        set_save_check(dialog, kTmcSaveQuick, cab->quick_save != 0);
        set_save_check(dialog, kTmcSaveBackup, cab->backup != 0);
        set_save_check(dialog, kTmcSaveLockAnnotations,
                       cab->lock_annotations != 0);
        auto& format = dialog.controls[kTmcSaveFormat];
        format.value = static_cast<Word>(cab->format);
        const std::string entry = "Current document format";
        format.entries = {entry};
        add_entry_to_native_control(format, entry);
        if (format.window != nullptr) {
            SendMessageA(format.window, CB_SETCURSEL, 0, 0);
        }
    }
    populate_save_directories(dialog);
}

void sync_save_cab(DialogState& dialog) {
    auto& edit = dialog.controls[kTmcSaveFile];
    if (edit.window != nullptr && IsWindow(edit.window)) {
        const int length = GetWindowTextLengthA(edit.window);
        std::vector<char> buffer(static_cast<std::size_t>(length) + 1);
        GetWindowTextA(edit.window, buffer.data(),
                       static_cast<int>(buffer.size()));
        edit.text = buffer.data();
    }
    if (dialog.cab != nullptr) {
        FSetCabSz(dialog.cab, edit.text.c_str(), 1);
    }
    if (auto* cab = save_cab(dialog); cab != nullptr) {
        const auto checked = [&dialog](const Tmc tmc) {
            const auto found = dialog.controls.find(tmc);
            return found != dialog.controls.end() &&
                   found->second.window != nullptr &&
                   SendMessageA(found->second.window, BM_GETCHECK, 0, 0) ==
                       BST_CHECKED;
        };
        cab->quick_save = checked(kTmcSaveQuick);
        cab->backup = checked(kTmcSaveBackup);
        cab->lock_annotations = checked(kTmcSaveLockAnnotations);
        cab->options = dialog.sab == 0 &&
                       dialog.controls[kTmcSaveFormat].visible;
    }
}

void materialize_save_as_template(DialogState& dialog) {
    if (dialog.hid != kIddSaveAs || dialog.window == nullptr) {
        return;
    }
    create_static_text(dialog, "Save File &Name:", {4, 2, 60, 9});
    create_native_control(dialog, kTmcSaveFile, "EDIT", "",
                          {4, 13, 81, 12},
                          WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL);
    create_native_control(dialog, kTmcOk, "BUTTON", "OK", {97, 6, 47, 14},
                          WS_TABSTOP | BS_DEFPUSHBUTTON);
    create_native_control(dialog, kTmcCancel, "BUTTON", "Cancel",
                          {97, 25, 47, 14}, WS_TABSTOP | BS_PUSHBUTTON);
    create_native_control(dialog, kTmcSaveOptions, "BUTTON", "&Options >>",
                          {97, 42, 47, 14}, WS_TABSTOP | BS_PUSHBUTTON);
    create_native_control(dialog, kTmcSaveDirectoryText, "STATIC",
                          "&Directories:", {4, 38, 73, 9}, SS_LEFT);
    create_native_control(dialog, kTmcSaveDirectoryList, "LISTBOX", "",
                          {4, 50, 89, 48},
                          WS_TABSTOP | WS_BORDER | WS_VSCROLL | LBS_NOTIFY |
                              LBS_SORT | LBS_NOINTEGRALHEIGHT);
    create_native_control(dialog, kTmcSaveDirectory, "STATIC", "",
                          {4, 27, 90, 9}, SS_LEFT);
    create_native_control(dialog, kTmcSaveFormatPrompt, "STATIC",
                          "&File Format:", {4, 105, 50, 9}, SS_LEFT);
    create_native_control(dialog, kTmcSaveFormat, "COMBOBOX", "",
                          {55, 103, 91, 72},
                          WS_TABSTOP | WS_VSCROLL | CBS_DROPDOWNLIST);
    create_native_control(dialog, kTmcSaveQuick, "BUTTON", "Fast &Save",
                          {4, 116, 46, 12},
                          WS_TABSTOP | BS_AUTOCHECKBOX);
    create_native_control(dialog, kTmcSaveBackup, "BUTTON", "Create &Backup",
                          {4, 128, 62, 12},
                          WS_TABSTOP | BS_AUTOCHECKBOX);
    create_native_control(dialog, kTmcSaveLockAnnotations, "BUTTON",
                          "&Lock for Annotations", {4, 140, 92, 12},
                          WS_TABSTOP | BS_AUTOCHECKBOX);
    dialog.caption = "Save As";
    dialog.native_modal = true;
    read_save_cab(dialog);
    set_save_options_visible(dialog, false);
}

bool is_font_name_control(const DialogState& dialog, const Tmc tmc) {
    return (dialog.hid == kCxtRibbonIconBar && tmc == kTmcUserMin) ||
           (dialog.hid == kIddCharacter && tmc == kTmcCharacterName);
}

bool is_font_size_control(const DialogState& dialog, const Tmc tmc) {
    return (dialog.hid == kCxtRibbonIconBar && tmc == kTmcUserMin + 1) ||
           (dialog.hid == kIddCharacter && tmc == kTmcCharacterSize);
}

void refresh_font_control_value(DialogState& dialog, const Tmc raw_tmc,
                                ControlState& state,
                                const bool read_native_text) {
    const Tmc tmc = static_cast<Tmc>(raw_tmc & ~0x8000u);
    if (read_native_text && state.window != nullptr &&
        IsWindow(state.window)) {
        HWND text_window = state.window;
        if (window_is_class(state.window, "COMBOBOX")) {
            COMBOBOXINFO info{};
            info.cbSize = sizeof(info);
            if (GetComboBoxInfo(state.window, &info) &&
                info.hwndItem != nullptr) {
                text_window = info.hwndItem;
            }
        }
        const int wide_length = GetWindowTextLengthW(text_window);
        std::vector<wchar_t> wide_text(
            static_cast<std::size_t>(wide_length) + 1);
        GetWindowTextW(text_window, wide_text.data(),
                       static_cast<int>(wide_text.size()));
        const int byte_count = WideCharToMultiByte(
            CP_ACP, 0, wide_text.data(), -1, nullptr, 0, nullptr, nullptr);
        if (byte_count > 0) {
            std::vector<char> text(static_cast<std::size_t>(byte_count));
            WideCharToMultiByte(CP_ACP, 0, wide_text.data(), -1, text.data(),
                                byte_count, nullptr, nullptr);
            state.text = text.data();
        } else {
            state.text.clear();
        }
    }

    if (is_font_name_control(dialog, tmc) &&
        g_font_name_to_value != nullptr && !state.text.empty()) {
        state.value =
            static_cast<Word>(g_font_name_to_value(state.text.c_str()));
    } else if (is_font_size_control(dialog, tmc) &&
               g_font_size_to_value != nullptr && !state.text.empty()) {
        state.value =
            static_cast<Word>(g_font_size_to_value(state.text.c_str()));
    }
}

int CALLBACK collect_system_font(const LOGFONTA* logical_font,
                                 const TEXTMETRICA*, DWORD, LPARAM parameter) {
    if (logical_font == nullptr || logical_font->lfFaceName[0] == '\0' ||
        logical_font->lfFaceName[0] == '@') {
        return 1;
    }
    auto* fonts = reinterpret_cast<std::vector<std::string>*>(parameter);
    fonts->emplace_back(logical_font->lfFaceName);
    return 1;
}

std::vector<std::string> installed_windows_fonts() {
    std::vector<std::string> fonts;
    const HDC dc = GetDC(nullptr);
    if (dc != nullptr) {
        LOGFONTA logical_font{};
        logical_font.lfCharSet = DEFAULT_CHARSET;
        EnumFontFamiliesExA(dc, &logical_font,
                            reinterpret_cast<FONTENUMPROCA>(collect_system_font),
                            reinterpret_cast<LPARAM>(&fonts), 0);
        ReleaseDC(nullptr, dc);
    }
    std::sort(fonts.begin(), fonts.end(),
              [](const std::string& left, const std::string& right) {
                  return _stricmp(left.c_str(), right.c_str()) < 0;
              });
    fonts.erase(std::unique(fonts.begin(), fonts.end(),
                            [](const std::string& left,
                               const std::string& right) {
                                return _stricmp(left.c_str(), right.c_str()) == 0;
                            }),
                fonts.end());
    return fonts;
}

void replace_list_entries(DialogState& dialog, const Tmc tmc,
                          const std::vector<std::string>& entries) {
    auto& state = dialog.controls[tmc];
    if (state.window != nullptr && IsWindow(state.window)) {
        const int length = GetWindowTextLengthA(state.window);
        std::vector<char> buffer(static_cast<std::size_t>(length) + 1);
        GetWindowTextA(state.window, buffer.data(),
                       static_cast<int>(buffer.size()));
        state.text = buffer.data();
    }
    const std::string edit_text = state.text;
    state.entries = entries;
    reset_native_list(state);
    for (const auto& entry : state.entries) {
        add_entry_to_native_control(state, entry);
    }
    state.text = edit_text;
    if (state.window != nullptr && IsWindow(state.window)) {
        SetWindowTextA(state.window, state.text.c_str());
    }
}

bool populate_windows_font_control(DialogState& dialog, const Tmc tmc) {
    if (is_font_name_control(dialog, tmc)) {
        replace_list_entries(dialog, tmc, installed_windows_fonts());
        return !dialog.controls[tmc].entries.empty();
    }
    if (is_font_size_control(dialog, tmc)) {
        static const std::vector<std::string> sizes = {
            "8",  "9",  "10", "11", "12", "14", "16", "18",
            "20", "22", "24", "26", "28", "36", "48", "72"};
        replace_list_entries(dialog, tmc, sizes);
        return true;
    }
    return false;
}

struct OriginalListBinding {
    OriginalListProc proc = nullptr;
    Word parameter = 0;
};

OriginalListBinding original_list_binding(const DialogState& dialog,
                                          const Tmc raw_tmc) {
    const Tmc tmc = static_cast<Tmc>(raw_tmc & ~0x8000u);
    if (dialog.hid == kCxtRibbonIconBar) {
        if (tmc == kTmcUserMin) {
            return {g_list_font_name, 0};
        }
        if (tmc == kTmcUserMin + 1) {
            return {g_list_font_size, kTmcUserMin};
        }
    } else if (dialog.hid == kCxtRulerIconBar && tmc == kTmcUserMin) {
        return {g_list_styles, 0};
    } else if (dialog.hid == kIddCharacter) {
        if (tmc == kTmcCharacterName) {
            return {g_list_font_name, 0};
        }
        if (tmc == kTmcCharacterSize) {
            return {g_list_font_size, kTmcCharacterName};
        }
        if (tmc == kTmcCharacterColor) {
            return {g_list_character_color, 0};
        }
    } else if (dialog.hid == kIddApplyStyle && tmc == kTmcApplyStyle) {
        return {g_list_styles, 0};
    } else if (dialog.hid == kIddDefineStyle &&
               (tmc == kTmcDefineStyle || tmc == kTmcDefineBasedOn ||
                tmc == kTmcDefineNext)) {
        return {g_list_styles, 0};
    }
    return {};
}

bool populate_original_list(DialogState& dialog, const Tmc raw_tmc) {
    const Tmc tmc = static_cast<Tmc>(raw_tmc & ~0x8000u);
    if (populate_windows_font_control(dialog, tmc)) {
        return true;
    }
    const auto binding = original_list_binding(dialog, tmc);
    if (binding.proc == nullptr) {
        return false;
    }

    auto& state = dialog.controls[tmc];
    if (state.window != nullptr && IsWindow(state.window)) {
        const int length = GetWindowTextLengthA(state.window);
        std::vector<char> text_buffer(static_cast<std::size_t>(length) + 1);
        GetWindowTextA(state.window, text_buffer.data(),
                       static_cast<int>(text_buffer.size()));
        state.text = text_buffer.data();
    }
    const std::string edit_text = state.text;
    state.entries.clear();
    reset_native_list(state);

    const Hdlg previous_current = g_current_dialog;
    g_current_dialog = dialog.handle;
    sync_current_dialog_globals();

    char buffer[512] = {};
    const Word reported_count =
        binding.proc(kTmmCount, buffer, 0, 0, tmc, binding.parameter);
    const unsigned limit = reported_count == kUnknownListCount
                               ? 4096u
                               : static_cast<unsigned>(reported_count);
    for (unsigned index = 0; index < limit; ++index) {
        buffer[0] = '\0';
        if (binding.proc(kTmmText, buffer, static_cast<int>(index), 0, tmc,
                         binding.parameter) == 0) {
            break;
        }
        state.entries.emplace_back(buffer);
        add_entry_to_native_control(state, state.entries.back());
    }

    g_current_dialog = previous_current;
    sync_current_dialog_globals();
    state.text = edit_text;
    if (state.window != nullptr && IsWindow(state.window)) {
        SetWindowTextA(state.window, state.text.c_str());
    }
    return !state.entries.empty();
}

void read_style_cab(DialogState& dialog, const Tmc tmc) {
    char style_name[256] = {};
    if (dialog.cab != nullptr) {
        /* Pointer arguments begin at iag 1 in the native, aligned CAB layout
           used by CABAPPLYSTYLE and CABDEFINESTYLE. */
        GetCabSz(dialog.cab, style_name,
                 static_cast<Word>(sizeof(style_name)), 1);
    }
    auto& state = dialog.controls[tmc];
    state.text = style_name;
    if (state.window != nullptr) {
        SetWindowTextA(state.window, state.text.c_str());
    }
}

void sync_style_cab(DialogState& dialog, const Tmc tmc) {
    auto found = dialog.controls.find(tmc);
    if (found == dialog.controls.end()) {
        return;
    }
    auto& state = found->second;
    if (state.window != nullptr && IsWindow(state.window)) {
        const int length = GetWindowTextLengthA(state.window);
        std::vector<char> buffer(static_cast<std::size_t>(length) + 1);
        GetWindowTextA(state.window, buffer.data(),
                       static_cast<int>(buffer.size()));
        state.text = buffer.data();
    }
    if (dialog.cab != nullptr) {
        FSetCabSz(dialog.cab, state.text.c_str(), 1);
    }
}

struct CabCharacterNative {
    Word simple_words;
    Word handle_words;
    Word sab;
    Word alignment;
    int ftc;
    int hps;
    int color;
    int bold;
    int italic;
    int small_caps;
    int hidden;
    int underline;
    int word_underline;
    int double_underline;
    int position;
    int position_amount;
    int spacing;
    int spacing_amount;
};

CabCharacterNative* character_cab(DialogState& dialog) {
    if (dialog.cab == nullptr || *dialog.cab == nullptr ||
        OpusCbOfH(dialog.cab) < sizeof(CabCharacterNative)) {
        return nullptr;
    }
    return static_cast<CabCharacterNative*>(*dialog.cab);
}

void set_native_check(DialogState& dialog, const Tmc tmc, const int value) {
    auto& state = dialog.controls[tmc];
    state.value = static_cast<Word>(value);
    if (state.window != nullptr) {
        SendMessageA(state.window, BM_SETCHECK,
                     value != 0 ? BST_CHECKED : BST_UNCHECKED, 0);
    }
}

void read_character_cab(DialogState& dialog) {
    auto* cab = character_cab(dialog);
    if (cab == nullptr) {
        return;
    }
    char font_name[LF_FACESIZE] = {};
    if (g_font_name_from_value != nullptr) {
        g_font_name_from_value(cab->ftc, font_name,
                               static_cast<int>(sizeof(font_name)));
    }
    auto& font = dialog.controls[kTmcCharacterName];
    font.text = font_name;
    font.value = static_cast<Word>(cab->ftc);
    if (font.window != nullptr) {
        SetWindowTextA(font.window, font.text.c_str());
    }

    char size_text[32] = {};
    if (cab->hps >= 0 && cab->hps != 0x8001) {
        if ((cab->hps & 1) == 0) {
            std::snprintf(size_text, sizeof(size_text), "%d", cab->hps / 2);
        } else {
            std::snprintf(size_text, sizeof(size_text), "%d.5", cab->hps / 2);
        }
    }
    auto& size = dialog.controls[kTmcCharacterSize];
    size.text = size_text;
    size.value = static_cast<Word>(cab->hps);
    if (size.window != nullptr) {
        SetWindowTextA(size.window, size.text.c_str());
    }

    if (cab->color >= 0 &&
        static_cast<std::size_t>(cab->color) <
            dialog.controls[kTmcCharacterColor].entries.size()) {
        SendMessageA(dialog.controls[kTmcCharacterColor].window, CB_SETCURSEL,
                     cab->color, 0);
        dialog.controls[kTmcCharacterColor].value =
            static_cast<Word>(cab->color);
    }
    set_native_check(dialog, kTmcUserMin + 5, cab->bold);
    set_native_check(dialog, kTmcUserMin + 6, cab->italic);
    set_native_check(dialog, kTmcUserMin + 7, cab->small_caps);
    set_native_check(dialog, kTmcUserMin + 8, cab->hidden);
    set_native_check(dialog, kTmcUserMin + 9, cab->underline);
    set_native_check(dialog, kTmcUserMin + 10, cab->word_underline);
    set_native_check(dialog, kTmcUserMin + 11, cab->double_underline);
    const int position = cab->position >= 0 && cab->position <= 2
                             ? cab->position
                             : 0;
    set_native_check(dialog, static_cast<Tmc>(kTmcUserMin + 13 + position), 1);
    const int spacing = cab->spacing >= 0 && cab->spacing <= 2
                            ? cab->spacing
                            : 0;
    set_native_check(dialog, static_cast<Tmc>(kTmcUserMin + 18 + spacing), 1);
}

void sync_character_cab(DialogState& dialog) {
    auto* cab = character_cab(dialog);
    if (cab == nullptr) {
        return;
    }
    const auto read_text = [&dialog](const Tmc tmc) {
        auto& state = dialog.controls[tmc];
        if (state.window != nullptr) {
            const int length = GetWindowTextLengthA(state.window);
            std::vector<char> buffer(static_cast<std::size_t>(length) + 1);
            GetWindowTextA(state.window, buffer.data(),
                           static_cast<int>(buffer.size()));
            state.text = buffer.data();
        }
        return state.text;
    };
    if (g_font_name_to_value != nullptr) {
        const int ftc =
            g_font_name_to_value(read_text(kTmcCharacterName).c_str());
        if (ftc >= 0) {
            cab->ftc = ftc;
        }
    }
    if (g_font_size_to_value != nullptr) {
        const int hps =
            g_font_size_to_value(read_text(kTmcCharacterSize).c_str());
        if (hps >= 0) {
            cab->hps = hps;
        }
    }
    const LRESULT color = SendMessageA(
        dialog.controls[kTmcCharacterColor].window, CB_GETCURSEL, 0, 0);
    if (color != CB_ERR) {
        cab->color = static_cast<int>(color);
    }
    const auto checked = [&dialog](const Tmc tmc) {
        return SendMessageA(dialog.controls[tmc].window, BM_GETCHECK, 0, 0) ==
               BST_CHECKED;
    };
    cab->bold = checked(kTmcUserMin + 5);
    cab->italic = checked(kTmcUserMin + 6);
    cab->small_caps = checked(kTmcUserMin + 7);
    cab->hidden = checked(kTmcUserMin + 8);
    cab->underline = checked(kTmcUserMin + 9);
    cab->word_underline = checked(kTmcUserMin + 10);
    cab->double_underline = checked(kTmcUserMin + 11);
    for (int index = 0; index < 3; ++index) {
        if (checked(static_cast<Tmc>(kTmcUserMin + 13 + index))) {
            cab->position = index;
        }
        if (checked(static_cast<Tmc>(kTmcUserMin + 18 + index))) {
            cab->spacing = index;
        }
    }
}

void materialize_character_template(DialogState& dialog) {
    if (dialog.hid != kIddCharacter || dialog.window == nullptr) {
        return;
    }
    constexpr DWORD combo_style =
        WS_TABSTOP | WS_VSCROLL | CBS_DROPDOWN | CBS_AUTOHSCROLL;
    create_static_text(dialog, "Character", {4, 4, 44, 9});
    create_static_text(dialog, "&Font:", {4, 14, 35, 9});
    create_native_control(dialog, kTmcCharacterName, "COMBOBOX", "",
                          {4, 24, 80, 68}, combo_style);
    create_static_text(dialog, "&Points:", {88, 14, 35, 9});
    create_native_control(dialog, kTmcCharacterSize, "COMBOBOX", "",
                          {88, 24, 40, 68}, combo_style);
    create_static_text(dialog, "Co&lor:", {4, 38, 35, 9});
    create_native_control(dialog, kTmcCharacterColor, "COMBOBOX", "",
                          {4, 48, 44, 72},
                          WS_TABSTOP | WS_VSCROLL | CBS_DROPDOWNLIST);

    const struct {
        Tmc tmc;
        const char* caption;
        Rec rec;
    } checks[] = {
        {kTmcUserMin + 5, "&Bold", {5, 63, 26, 12}},
        {kTmcUserMin + 6, "&Italic", {5, 75, 34, 12}},
        {kTmcUserMin + 7, "Small &Caps", {5, 87, 50, 12}},
        {kTmcUserMin + 8, "&Hidden", {5, 99, 34, 12}},
        {kTmcUserMin + 9, "&Underline", {5, 111, 46, 12}},
        {kTmcUserMin + 10, "&Word underline", {5, 123, 66, 12}},
        {kTmcUserMin + 11, "&Double underline", {5, 135, 74, 12}},
    };
    for (const auto& check : checks) {
        create_native_control(dialog, check.tmc, "BUTTON", check.caption,
                              check.rec, WS_TABSTOP | BS_AUTOCHECKBOX);
    }

    create_untracked_control(dialog, "BUTTON", "Position", {84, 44, 89, 48},
                             BS_GROUPBOX);
    create_native_control(dialog, kTmcUserMin + 13, "BUTTON", "&Normal",
                          {87, 54, 45, 12},
                          WS_TABSTOP | WS_GROUP | BS_AUTORADIOBUTTON);
    create_native_control(dialog, kTmcUserMin + 14, "BUTTON", "&Superscript",
                          {87, 65, 54, 12},
                          WS_TABSTOP | BS_AUTORADIOBUTTON);
    create_native_control(dialog, kTmcUserMin + 15, "BUTTON", "Subsc&ript",
                          {87, 76, 50, 12},
                          WS_TABSTOP | BS_AUTORADIOBUTTON);
    create_static_text(dialog, "B&y:", {141, 67, 13, 9});
    create_native_control(dialog, kTmcUserMin + 16, "EDIT", "",
                          {141, 77, 30, 12},
                          WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL);

    create_untracked_control(dialog, "BUTTON", "Character Spacing",
                             {84, 96, 89, 49}, BS_GROUPBOX);
    create_native_control(dialog, kTmcUserMin + 18, "BUTTON", "N&ormal",
                          {87, 107, 45, 12},
                          WS_TABSTOP | WS_GROUP | BS_AUTORADIOBUTTON);
    create_native_control(dialog, kTmcUserMin + 19, "BUTTON", "&Expanded",
                          {87, 119, 46, 12},
                          WS_TABSTOP | BS_AUTORADIOBUTTON);
    create_native_control(dialog, kTmcUserMin + 20, "BUTTON", "&Condensed",
                          {87, 130, 50, 12},
                          WS_TABSTOP | BS_AUTORADIOBUTTON);
    create_static_text(dialog, "By&:", {141, 120, 12, 9});
    create_native_control(dialog, kTmcUserMin + 21, "EDIT", "",
                          {141, 130, 30, 12},
                          WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL);
    create_native_control(dialog, kTmcOk, "BUTTON", "OK", {140, 6, 34, 14},
                          WS_TABSTOP | BS_DEFPUSHBUTTON);
    create_native_control(dialog, kTmcCancel, "BUTTON", "Cancel",
                          {140, 23, 34, 14}, WS_TABSTOP | BS_PUSHBUTTON);
    dialog.caption = "Character";
    dialog.native_modal = true;
    populate_original_list(dialog, kTmcCharacterName);
    populate_original_list(dialog, kTmcCharacterSize);
    populate_original_list(dialog, kTmcCharacterColor);
    read_character_cab(dialog);
}

void materialize_apply_style_template(DialogState& dialog) {
    if (dialog.hid != kIddApplyStyle || dialog.window == nullptr) {
        return;
    }
    create_static_text(dialog, "&Style Name:", {4, 3, 55, 9});
    create_native_control(dialog, kTmcApplyStyle, "COMBOBOX", "",
                          {4, 13, 96, 60},
                          WS_TABSTOP | WS_VSCROLL | CBS_DROPDOWN |
                              CBS_AUTOHSCROLL);
    create_native_control(dialog, kTmcOk, "BUTTON", "OK", {104, 6, 40, 14},
                          WS_TABSTOP | BS_DEFPUSHBUTTON);
    create_native_control(dialog, kTmcCancel, "BUTTON", "Cancel",
                          {104, 23, 40, 14}, WS_TABSTOP | BS_PUSHBUTTON);
    create_native_control(dialog, kTmcApplyDefine, "BUTTON", "&Define...",
                          {103, 39, 42, 14}, WS_TABSTOP | BS_PUSHBUTTON);
    create_native_control(dialog, kTmcApplyBanter, "STATIC", "",
                          {4, 75, 141, 24}, SS_LEFT);
    dialog.caption = "Apply Style";
    dialog.native_modal = true;
    read_style_cab(dialog, kTmcApplyStyle);
    populate_original_list(dialog, kTmcApplyStyle);
}

void materialize_define_style_template(DialogState& dialog) {
    if (dialog.hid != kIddDefineStyle || dialog.window == nullptr) {
        return;
    }
    create_static_text(dialog, "Define &Style Name:", {4, 3, 78, 10});
    create_native_control(dialog, kTmcDefineStyle, "COMBOBOX", "",
                          {4, 14, 76, 60},
                          WS_TABSTOP | WS_VSCROLL | CBS_DROPDOWN |
                              CBS_AUTOHSCROLL);
    create_native_control(dialog, kTmcDefineChars, "BUTTON", "&Character...",
                          {85, 14, 52, 14}, WS_TABSTOP | BS_PUSHBUTTON);
    create_native_control(dialog, kTmcDefineParas, "BUTTON", "&Paragraph...",
                          {85, 30, 52, 14}, WS_TABSTOP | BS_PUSHBUTTON);
    create_native_control(dialog, kTmcDefineTabs, "BUTTON", "&Tabs...",
                          {85, 46, 52, 14}, WS_TABSTOP | BS_PUSHBUTTON);
    create_native_control(dialog, kTmcDefinePosition, "BUTTON", "Pos&ition...",
                          {85, 62, 52, 14}, WS_TABSTOP | BS_PUSHBUTTON);
    create_native_control(dialog, kTmcOk, "BUTTON", "OK", {140, 14, 45, 14},
                          WS_TABSTOP | BS_DEFPUSHBUTTON);
    create_native_control(dialog, kTmcCancel, "BUTTON", "Cancel",
                          {140, 30, 45, 14}, WS_TABSTOP | BS_PUSHBUTTON);
    create_native_control(dialog, kTmcDefineOptions, "BUTTON", "&Options >>",
                          {140, 62, 45, 14}, WS_TABSTOP | BS_PUSHBUTTON);
    create_native_control(dialog, kTmcDefineBanter, "STATIC", "",
                          {4, 76, 180, 24}, SS_LEFT);
    create_native_control(dialog, kTmcDefineCommit, "BUTTON", "&Define",
                          {4, 129, 43, 14}, WS_TABSTOP | BS_PUSHBUTTON);
    create_native_control(dialog, kTmcDefineDelete, "BUTTON", "De&lete",
                          {49, 129, 43, 14}, WS_TABSTOP | BS_PUSHBUTTON);
    create_native_control(dialog, kTmcDefineRename, "BUTTON", "&Rename...",
                          {94, 129, 43, 14}, WS_TABSTOP | BS_PUSHBUTTON);
    create_native_control(dialog, kTmcDefineMerge, "BUTTON", "&Merge...",
                          {139, 129, 43, 14}, WS_TABSTOP | BS_PUSHBUTTON);
    dialog.caption = "Define Style";
    dialog.native_modal = true;
    read_style_cab(dialog, kTmcDefineStyle);
    populate_original_list(dialog, kTmcDefineStyle);
}

void materialize_icon_bar_template(DialogState& dialog) {
    constexpr DWORD combo_style =
        WS_TABSTOP | WS_VSCROLL | CBS_DROPDOWN | CBS_AUTOHSCROLL;

    if (dialog.hid == kCxtRibbonIconBar) {
        const bool win3 = (*dialog.template_handle)->rec.dx == 160;
        if (win3) {
            create_static_text(dialog, "Font:", {4, 3, 20, 8});
            create_native_control(dialog, kTmcUserMin, "COMBOBOX", "",
                                  {26, 1, 76, 67}, combo_style);
            create_static_text(dialog, "Pts:", {108, 3, 14, 8});
            create_native_control(dialog, kTmcUserMin + 1, "COMBOBOX", "",
                                  {127, 1, 28, 67}, combo_style);
        } else {
            create_static_text(dialog, "Font:", {4, 3, 20, 8});
            create_native_control(dialog, kTmcUserMin, "COMBOBOX", "",
                                  {29, 1, 80, 68}, combo_style);
            create_static_text(dialog, "Pts:", {115, 3, 16, 8});
            create_native_control(dialog, kTmcUserMin + 1, "COMBOBOX", "",
                                  {134, 1, 32, 68}, combo_style);
        }
    } else if (dialog.hid == kCxtRulerIconBar) {
        const bool win3 = (*dialog.template_handle)->rec.dx == 102;
        if (win3) {
            create_static_text(dialog, "Style:", {4, 3, 20, 8});
            create_native_control(dialog, kTmcUserMin, "COMBOBOX", "",
                                  {26, 1, 76, 68}, combo_style);
        } else {
            create_static_text(dialog, "Style:", {4, 3, 24, 8});
            create_native_control(dialog, kTmcUserMin, "COMBOBOX", "",
                                  {29, 1, 80, 68}, combo_style);
        }
    }
    if (dialog.hid == kCxtRibbonIconBar) {
        populate_original_list(dialog, kTmcUserMin);
        populate_original_list(dialog, kTmcUserMin + 1);
    }
}

HWND ensure_control_window(const Tmc raw_tmc) {
    auto& state = control(raw_tmc);
    if (state.window != nullptr && IsWindow(state.window)) {
        return state.window;
    }
    if (g_dialog.window == nullptr || !IsWindow(g_dialog.window)) {
        return nullptr;
    }

    const Tmc tmc = static_cast<Tmc>(raw_tmc & ~0x8000u);
    DWORD style = WS_CHILD | WS_CLIPSIBLINGS;
    if (state.visible) {
        style |= WS_VISIBLE;
    }
    state.window = CreateWindowExA(
        0, "STATIC", state.text.c_str(), style, state.rectangle.x,
        state.rectangle.y, (std::max)(1, state.rectangle.dx),
        (std::max)(1, state.rectangle.dy), g_dialog.window,
        reinterpret_cast<HMENU>(static_cast<std::uintptr_t>(tmc)),
        GetModuleHandleW(nullptr), nullptr);
    if (state.window != nullptr) {
        EnableWindow(state.window, state.enabled);
    }
    return state.window;
}

void copy_text(const std::string& source, char* destination,
               const Word capacity) {
    if (destination == nullptr || capacity == 0) {
        return;
    }
    const std::size_t count =
        (std::min)(source.size(), static_cast<std::size_t>(capacity - 1));
    std::memcpy(destination, source.data(), count);
    destination[count] = '\0';
}

std::string counted_or_zero_terminated(const char* text) {
    if (text == nullptr) {
        return {};
    }
    return std::string(text);
}

bool invoke_dialog_proc(DialogState& dialog, const Word message,
                        const Tmc tmc, const Word new_value = 0,
                        const Word old_value = 0,
                        const Word parameter = 0) {
    if (dialog.template_handle == nullptr ||
        *dialog.template_handle == nullptr ||
        (*dialog.template_handle)->dialog_proc == nullptr) {
        return true;
    }
    const auto proc = reinterpret_cast<NativeDialogProc>(
        (*dialog.template_handle)->dialog_proc);
    return proc(message, tmc, new_value, old_value, parameter) != 0;
}

void ensure_icon_bar_commands_active(DialogState& dialog, const Tmc tmc) {
    if (dialog.hid != kCxtRibbonIconBar || dialog.commands_active) {
        return;
    }
    dialog.commands_active = true;
    invoke_dialog_proc(dialog, kDlmSetDialogFocus, tmc);
    invoke_dialog_proc(dialog, kDlmSetItemFocus, tmc);
}

void commit_ribbon_list_selection(DialogState& dialog, const Tmc tmc) {
    if (dialog.hid != kCxtRibbonIconBar || !dialog.commands_active) {
        return;
    }
    /* The Win16 SDM/icon-bar loop applied a combo selection when its dialog
       focus was terminated.  A native Win32 drop-down keeps focus on the
       combo after the list closes, so that termination never reliably
       occurs.  CBN_SELENDOK identifies the completed choice, after all
       intermediate CBN_SELCHANGE notifications, and is the safe point to
       send the same original callbacks.  Typed edit text still commits on
       CBN_KILLFOCUS. */
    OpusX64TraceRibbon("commit-begin", kDlmKillDialogFocus, tmc,
                       dialog.controls[tmc].value, dialog.commands_active,
                       0, 0, 0);
    const bool item_result =
        invoke_dialog_proc(dialog, kDlmKillItemFocus, tmc);
    const bool dialog_result =
        invoke_dialog_proc(dialog, kDlmKillDialogFocus, tmc);
    dialog.commands_active = false;
    /* Win16 SDM returned focus to the document when an icon-bar session
       ended.  A native combo otherwise retains focus, forcing the user to
       click the document; that mouse click reloads the properties at the
       caret and discards the just-applied insertion font and size.  Route
       the focus return through Microsoft's original FDlgIb handler. */
    const bool focus_result =
        invoke_dialog_proc(dialog, kDlmDialogClick, tmc);
    OpusX64TraceRibbon("commit-end", kDlmKillDialogFocus, tmc,
                       item_result, dialog_result, focus_result, 0, 0);
}

std::string selected_list_text(const ControlState& control_state) {
    if (control_state.window == nullptr) {
        return {};
    }
    const bool combo = window_is_class(control_state.window, "COMBOBOX");
    const LRESULT selection = SendMessageA(
        control_state.window, combo ? CB_GETCURSEL : LB_GETCURSEL, 0, 0);
    if (selection == (combo ? CB_ERR : LB_ERR)) {
        return {};
    }
    const LRESULT length = SendMessageA(
        control_state.window, combo ? CB_GETLBTEXTLEN : LB_GETTEXTLEN,
        selection, 0);
    if (length == (combo ? CB_ERR : LB_ERR)) {
        return {};
    }
    std::vector<char> text(static_cast<std::size_t>(length) + 1);
    SendMessageA(control_state.window, combo ? CB_GETLBTEXT : LB_GETTEXT,
                 selection,
                 reinterpret_cast<LPARAM>(text.data()));
    return text.data();
}

void select_new_type(DialogState& dialog) {
    const auto found = dialog.controls.find(kTmcNewTypeList);
    if (found == dialog.controls.end()) {
        return;
    }
    const std::string selected = selected_list_text(found->second);
    if (selected.empty()) {
        return;
    }
    auto& edit = dialog.controls[kTmcNewType];
    edit.text = selected;
    if (edit.window != nullptr) {
        SetWindowTextA(edit.window, edit.text.c_str());
        SendMessageA(edit.window, EM_SETSEL, 0, static_cast<LPARAM>(-1));
    }
}

void select_open_file(DialogState& dialog) {
    const auto found = dialog.controls.find(kTmcOpenFileList);
    if (found == dialog.controls.end()) {
        return;
    }
    const std::string selected = selected_list_text(found->second);
    if (selected.empty()) {
        return;
    }
    auto& edit = dialog.controls[kTmcOpenFileName];
    edit.text = join_path(dialog.current_directory, selected);
    if (edit.window != nullptr) {
        SetWindowTextA(edit.window, edit.text.c_str());
        SendMessageA(edit.window, EM_SETSEL, 0,
                     static_cast<LPARAM>(-1));
    }
}

void enter_open_directory(DialogState& dialog) {
    const auto found = dialog.controls.find(kTmcOpenFileDir);
    if (found == dialog.controls.end()) {
        return;
    }
    std::string selected = selected_list_text(found->second);
    if (selected.size() < 2 || selected.front() != '[' ||
        selected.back() != ']') {
        return;
    }
    selected = selected.substr(1, selected.size() - 2);
    char full_path[32768] = {};
    const std::string destination =
        join_path(dialog.current_directory, selected);
    if (GetFullPathNameA(destination.c_str(),
                         static_cast<DWORD>(sizeof(full_path)), full_path,
                         nullptr) == 0) {
        return;
    }
    dialog.current_directory = full_path;
    populate_open_lists(dialog);
    auto& edit = dialog.controls[kTmcOpenFileName];
    edit.text = join_path(dialog.current_directory, dialog.file_pattern);
    if (edit.window != nullptr) {
        SetWindowTextA(edit.window, edit.text.c_str());
    }
}

void finish_native_dialog(DialogState& dialog, const Tmc result) {
    if (dialog.hid == kIddOpen) {
        sync_open_cab(dialog);
    } else if (dialog.hid == kIddSaveAs) {
        sync_save_cab(dialog);
    } else if (dialog.hid == kIddNewDoc) {
        sync_new_cab(dialog);
    } else if (dialog.hid == kIddApplyStyle) {
        sync_style_cab(dialog, kTmcApplyStyle);
    } else if (dialog.hid == kIddDefineStyle) {
        sync_style_cab(dialog, kTmcDefineStyle);
    } else if (dialog.hid == kIddCharacter) {
        sync_character_cab(dialog);
    }
    if (!invoke_dialog_proc(dialog, kDlmTerm, result)) {
        return;
    }
    dialog.result_tmc = result;
    dialog.dying = true;
    dialog.visible = false;
    if (dialog.window != nullptr && IsWindow(dialog.window)) {
        ShowWindow(dialog.window, SW_HIDE);
    }
}

void handle_dialog_command(const Hdlg handle, const WPARAM w_param,
                           const LPARAM l_param) {
    auto* dialog = find_dialog(handle);
    if (dialog == nullptr || dialog->dying) {
        return;
    }
    const Tmc tmc = static_cast<Tmc>(LOWORD(w_param) & ~0x8000u);
    const Word notification = HIWORD(w_param);
    auto found = dialog->controls.find(tmc);
    if (found != dialog->controls.end() &&
        found->second.window == reinterpret_cast<HWND>(l_param)) {
        if (tmc == kTmcOpenFileName && dialog->hid == kIddOpen) {
            const int length = GetWindowTextLengthA(found->second.window);
            std::vector<char> text(static_cast<std::size_t>(length) + 1);
            GetWindowTextA(found->second.window, text.data(),
                           static_cast<int>(text.size()));
            found->second.text = text.data();
        } else if (tmc == kTmcOpenReadOnly && dialog->hid == kIddOpen) {
            found->second.value = static_cast<Word>(
                SendMessageA(found->second.window, BM_GETCHECK, 0, 0) ==
                BST_CHECKED);
        } else if (tmc == kTmcNewType && dialog->hid == kIddNewDoc) {
            const int length = GetWindowTextLengthA(found->second.window);
            std::vector<char> text(static_cast<std::size_t>(length) + 1);
            GetWindowTextA(found->second.window, text.data(),
                           static_cast<int>(text.size()));
            found->second.text = text.data();
        } else if (tmc == kTmcSaveFile && dialog->hid == kIddSaveAs) {
            const int length = GetWindowTextLengthA(found->second.window);
            std::vector<char> text(static_cast<std::size_t>(length) + 1);
            GetWindowTextA(found->second.window, text.data(),
                           static_cast<int>(text.size()));
            found->second.text = text.data();
        } else if (dialog->hid == kIddSaveAs &&
                   (tmc == kTmcSaveQuick || tmc == kTmcSaveBackup ||
                    tmc == kTmcSaveLockAnnotations)) {
            found->second.value = static_cast<Word>(SendMessageA(
                found->second.window, BM_GETCHECK, 0, 0) == BST_CHECKED);
        }
    }
    g_current_dialog = handle;
    g_focus_dialog = handle;
    sync_current_dialog_globals();

    if (found != dialog->controls.end() &&
        window_is_class(found->second.window, "COMBOBOX")) {
        OpusX64TraceRibbon("combo", notification, tmc,
                           found->second.value, dialog->commands_active,
                           0, 0, 0);
        if (notification == CBN_SETFOCUS) {
            if (!dialog->commands_active) {
                dialog->commands_active = true;
                invoke_dialog_proc(*dialog, kDlmSetDialogFocus, tmc);
            }
            invoke_dialog_proc(*dialog, kDlmSetItemFocus, tmc);
            return;
        }
        if (notification == CBN_KILLFOCUS) {
            if (dialog->commands_active) {
                invoke_dialog_proc(*dialog, kDlmKillItemFocus, tmc);
                invoke_dialog_proc(*dialog, kDlmKillDialogFocus, tmc);
                dialog->commands_active = false;
            }
            return;
        }
        if (notification == CBN_DROPDOWN) {
            populate_original_list(*dialog, tmc);
            return;
        }
        if (notification == CBN_EDITCHANGE) {
            ensure_icon_bar_commands_active(*dialog, tmc);
            refresh_font_control_value(*dialog, tmc, found->second, true);
            OpusX64TraceRibbon("combo-edit", notification, tmc,
                               found->second.value,
                               dialog->commands_active, 0, 0, 0);
            invoke_dialog_proc(*dialog, kDlmChange, tmc);
            return;
        }
        if (notification == CBN_SELCHANGE) {
            ensure_icon_bar_commands_active(*dialog, tmc);
            const LRESULT selection =
                SendMessageA(found->second.window, CB_GETCURSEL, 0, 0);
            if (selection != CB_ERR) {
                dialog->controls[static_cast<Tmc>(tmc + 1)].value =
                    static_cast<Word>(selection);
                found->second.value = static_cast<Word>(selection);
                found->second.text = selected_list_text(found->second);
                refresh_font_control_value(*dialog, tmc, found->second,
                                           false);
                OpusX64TraceRibbon("combo-select", notification, tmc,
                                   found->second.value,
                                   static_cast<int>(selection), 0, 0, 0);
                SetWindowTextA(found->second.window,
                               found->second.text.c_str());
                invoke_dialog_proc(*dialog, kDlmClick,
                                   static_cast<Tmc>(tmc + 1));
                invoke_dialog_proc(*dialog, kDlmChange, tmc);
            }
            return;
        }
        if (notification == CBN_SELENDOK) {
            ensure_icon_bar_commands_active(*dialog, tmc);
            PostMessageW(dialog->window, kWmCommitRibbonSelection, tmc, 0);
            return;
        }
    }

    if (!dialog->commands_active) {
        return;
    }

    if (dialog->hid == kIddNewDoc && tmc == kTmcNewType &&
        notification == EN_CHANGE) {
        invoke_dialog_proc(*dialog, kDlmChange, tmc);
        return;
    }
    if (dialog->hid == kIddNewDoc && tmc == kTmcNewTypeList &&
        (notification == LBN_SELCHANGE || notification == LBN_DBLCLK)) {
        select_new_type(*dialog);
        invoke_dialog_proc(*dialog,
                           notification == LBN_DBLCLK ? kDlmDblClk
                                                     : kDlmClick,
                           tmc);
        return;
    }
    if (dialog->hid == kIddOpen && tmc == kTmcOpenFileName &&
        notification == EN_CHANGE) {
        invoke_dialog_proc(*dialog, kDlmChange, tmc);
        return;
    }
    if (dialog->hid == kIddSaveAs && tmc == kTmcSaveFile &&
        notification == EN_CHANGE) {
        invoke_dialog_proc(*dialog, kDlmChange, tmc);
        return;
    }
    if (dialog->hid == kIddSaveAs && tmc == kTmcSaveDirectoryList &&
        (notification == LBN_SELCHANGE || notification == LBN_DBLCLK)) {
        const LRESULT selection = SendMessageA(
            found->second.window, LB_GETCURSEL, 0, 0);
        if (selection != LB_ERR) {
            found->second.value = static_cast<Word>(selection);
        }
        invoke_dialog_proc(*dialog,
                           notification == LBN_DBLCLK ? kDlmDblClk
                                                     : kDlmClick,
                           tmc, found->second.value);
        return;
    }
    if (dialog->hid == kIddOpen && tmc == kTmcOpenFileList &&
        (notification == LBN_SELCHANGE || notification == LBN_DBLCLK)) {
        select_open_file(*dialog);
        invoke_dialog_proc(*dialog,
                           notification == LBN_DBLCLK ? kDlmDblClk
                                                     : kDlmClick,
                           tmc);
        if (notification == LBN_DBLCLK && !dialog->dying) {
            finish_native_dialog(*dialog, kTmcOk);
        }
        return;
    }
    if (dialog->hid == kIddOpen && tmc == kTmcOpenFileDir &&
        (notification == LBN_SELCHANGE || notification == LBN_DBLCLK)) {
        invoke_dialog_proc(*dialog,
                           notification == LBN_DBLCLK ? kDlmDblClk
                                                     : kDlmClick,
                           tmc);
        if (notification == LBN_DBLCLK && !dialog->dying) {
            enter_open_directory(*dialog);
        }
        return;
    }
    if (notification == BN_CLICKED) {
        if (dialog->hid == kIddNewDoc &&
            (tmc == kTmcRNewDoc || tmc == kTmcRNewDot)) {
            const bool new_template = tmc == kTmcRNewDot;
            dialog->controls[kTmcNewDot].value = new_template;
            dialog->controls[kTmcRNewDoc].value = !new_template;
            dialog->controls[kTmcRNewDot].value = new_template;
            invoke_dialog_proc(*dialog, kDlmClick, tmc);
        } else if (tmc == kTmcOk || tmc == kTmcCancel ||
                   (dialog->hid == kIddOpen &&
                    tmc == kTmcOpenCatalog) ||
                   (dialog->hid == kIddNewDoc &&
                    tmc == kTmcSummary)) {
            finish_native_dialog(*dialog, tmc);
        } else {
            const Word new_value = found == dialog->controls.end()
                                       ? 0
                                       : found->second.value;
            invoke_dialog_proc(*dialog, kDlmClick, tmc, new_value);
        }
    }
}

LRESULT CALLBACK native_dialog_window_proc(const HWND window,
                                           const UINT message,
                                           const WPARAM w_param,
                                           const LPARAM l_param) {
    if (message == WM_NCCREATE) {
        const auto* create = reinterpret_cast<const CREATESTRUCTA*>(l_param);
        SetWindowLongPtrA(window, GWLP_USERDATA,
                          reinterpret_cast<LONG_PTR>(create->lpCreateParams));
    }
    const Hdlg handle = static_cast<Hdlg>(reinterpret_cast<std::uintptr_t>(
        reinterpret_cast<void*>(GetWindowLongPtrA(window, GWLP_USERDATA))));
    switch (message) {
        case WM_COMMAND:
            handle_dialog_command(handle, w_param, l_param);
            return 0;
        case kWmCommitRibbonSelection:
            if (auto* dialog = find_dialog(handle); dialog != nullptr) {
                g_current_dialog = handle;
                g_focus_dialog = handle;
                sync_current_dialog_globals();
                ensure_icon_bar_commands_active(
                    *dialog, static_cast<Tmc>(w_param));
                commit_ribbon_list_selection(
                    *dialog, static_cast<Tmc>(w_param));
            }
            return 0;
        case WM_CLOSE:
            if (auto* dialog = find_dialog(handle); dialog != nullptr) {
                finish_native_dialog(*dialog, kTmcCancel);
            }
            return 0;
        default:
            return DefWindowProcA(window, message, w_param, l_param);
    }
}

}  // namespace

extern "C" {

struct OpusSdsCompat {
    std::uintptr_t current_segment;
    std::uintptr_t focus_segment;
    void** current_dialog;
    void** focus_dialog;
};

OpusSdsCompat sds = {};
DAC_SDM dac = {};
HWND vhWndMsgBoxParent = nullptr;

extern void** hcabDlgCur;
extern std::uintptr_t wRefDlgCur;

void OpusRegisterOriginalDialogCallbacks(
    OriginalListProc list_font_name, OriginalListProc list_font_size,
    OriginalListProc list_styles, OriginalListProc list_character_color,
    FontValueProc font_name_to_value, FontValueProc font_size_to_value,
    FontNameFromValueProc font_name_from_value) {
    g_list_font_name = list_font_name;
    g_list_font_size = list_font_size;
    g_list_styles = list_styles;
    g_list_character_color = list_character_color;
    g_font_name_to_value = font_name_to_value;
    g_font_size_to_value = font_size_to_value;
    g_font_name_from_value = font_name_from_value;
}

int FInitSdm_sdm21(void*) {
    g_initialized = true;
    dac.dxBorder = GetSystemMetrics(SM_CXBORDER);
    dac.dyBorder = GetSystemMetrics(SM_CYBORDER);
    dac.dyCaption = GetSystemMetrics(SM_CYCAPTION);
    dac.dxVScroll = GetSystemMetrics(SM_CXVSCROLL);
    dac.dxSysFontChar = 8;
    dac.dySysFontChar = 16;
    dac.dySysFontAscent = 12;
    dac.clrWindow = GetSysColor(COLOR_WINDOW);
    dac.clrWindowFrame = GetSysColor(COLOR_WINDOWFRAME);
    dac.clrWindowText = GetSysColor(COLOR_WINDOWTEXT);
    dac.clrButton = GetSysColor(COLOR_BTNFACE);
    dac.clrButtonText = GetSysColor(COLOR_BTNTEXT);
    return true;
}

void EndSdm() {
    for (auto& entry : g_dialogs) {
        if (entry.second.window != nullptr && IsWindow(entry.second.window)) {
            DestroyWindow(entry.second.window);
        }
    }
    g_dialogs.clear();
    g_no_dialog = {};
    g_current_dialog = 0;
    g_focus_dialog = 0;
    g_initialized = false;
    sync_current_dialog_globals();
}

Word FtmeIsSdmMessage(MSG*) { return 0; }
void ChangeColors() {}

Hdlg HdlgStartDlg(DltHeader** dialog_template, Hcab cab, Dli* initializer) {
    Hdlg handle = 0;
    for (unsigned attempt = 0; attempt < 0xfffeu; ++attempt) {
        const Hdlg candidate = g_next_dialog;
        ++g_next_dialog;
        if (g_next_dialog == 0 || g_next_dialog == static_cast<Hdlg>(-1)) {
            g_next_dialog = 1;
        }
        if (g_dialogs.find(candidate) == g_dialogs.end()) {
            handle = candidate;
            break;
        }
    }
    if (handle == 0) {
        return 0;
    }

    DialogState dialog{};
    dialog.handle = handle;
    dialog.template_handle = dialog_template;
    dialog.cab = cab;
    if (dialog_template != nullptr && *dialog_template != nullptr) {
        dialog.hid = (*dialog_template)->hid;
        dialog.focus = (*dialog_template)->tmc_sel_init;
    }
    if (initializer != nullptr) {
        dialog.reference = initializer->reference;
        dialog.visible = true;
        dialog.modal = (initializer->flags & 0x00000001u) != 0;
    }
    dialog.window = create_dialog_host(dialog, initializer);
    g_dialogs.emplace(handle, std::move(dialog));
    g_current_dialog = handle;
    g_focus_dialog = handle;
    sync_current_dialog_globals();
    materialize_icon_bar_template(g_dialogs.at(handle));
    materialize_new_template(g_dialogs.at(handle));
    materialize_open_template(g_dialogs.at(handle));
    materialize_save_as_template(g_dialogs.at(handle));
    materialize_about_template(g_dialogs.at(handle));
    materialize_character_template(g_dialogs.at(handle));
    materialize_apply_style_template(g_dialogs.at(handle));
    materialize_define_style_template(g_dialogs.at(handle));
    return handle;
}

Tmc TmcDoDlgDli(DltHeader** dialog_template, Hcab cab, Dli* initializer) {
    const Hdlg previous_current = g_current_dialog;
    const Hdlg previous_focus = g_focus_dialog;
    const Hdlg handle = HdlgStartDlg(dialog_template, cab, initializer);
    if (handle == 0) {
        return static_cast<Tmc>(-1);
    }
    auto* dialog = find_dialog(handle);
    if (dialog == nullptr) {
        return static_cast<Tmc>(-1);
    }

    Tmc result = kTmcCancel;
    HWND owner = nullptr;
    bool restore_owner = false;
    if (dialog->modal && dialog->native_modal && dialog->window != nullptr) {
        owner = GetWindow(dialog->window, GW_OWNER);
        restore_owner = owner != nullptr && IsWindow(owner) &&
                        IsWindowEnabled(owner) != 0;
        if (restore_owner) {
            EnableWindow(owner, false);
        }

        invoke_dialog_proc(*dialog, kDlmInit, dialog->focus);
        dialog->commands_active = true;
        if (!dialog->dying) {
            dialog->visible = true;
            ShowWindow(dialog->window, SW_SHOW);
            UpdateWindow(dialog->window);
            SetActiveWindow(dialog->window);
            const auto focus = dialog->controls.find(
                static_cast<Tmc>(dialog->focus & ~0x8000u));
            if (focus != dialog->controls.end() &&
                focus->second.window != nullptr) {
                SetFocus(focus->second.window);
            }
        }

        MSG message{};
        while (!dialog->dying) {
            const int status = GetMessageA(&message, nullptr, 0, 0);
            if (status <= 0) {
                if (status == 0) {
                    PostQuitMessage(static_cast<int>(message.wParam));
                }
                dialog->result_tmc = kTmcCancel;
                dialog->dying = true;
                break;
            }
            if (!IsDialogMessageA(dialog->window, &message)) {
                TranslateMessage(&message);
                DispatchMessageA(&message);
            }
            dialog = find_dialog(handle);
            if (dialog == nullptr) {
                break;
            }
        }
        if (dialog != nullptr) {
            result = dialog->result_tmc;
            dialog->commands_active = false;
            invoke_dialog_proc(*dialog, kDlmExit, result);
        }
    }

    dialog = find_dialog(handle);
    if (dialog != nullptr && dialog->window != nullptr &&
        IsWindow(dialog->window)) {
        DestroyWindow(dialog->window);
    }
    g_dialogs.erase(handle);
    if (restore_owner && owner != nullptr && IsWindow(owner)) {
        EnableWindow(owner, true);
        SetActiveWindow(owner);
    }
    g_current_dialog = find_dialog(previous_current) == nullptr
                           ? 0
                           : previous_current;
    g_focus_dialog = find_dialog(previous_focus) == nullptr ? 0
                                                            : previous_focus;
    sync_current_dialog_globals();
    return result;
}

Tmc TmcDoDlg_sdm21(DltHeader** dialog_template, Hcab cab,
                    unsigned char* runtime_items) {
    Dli initializer{};
    initializer.flags = 0x00000001u;
    initializer.runtime_items = runtime_items;
    return TmcDoDlgDli(dialog_template, cab, &initializer);
}

void EndDlg(Tmc result) {
    g_dialog.result_tmc = result;
    g_dialog.dying = true;
    g_dialog.visible = false;
    if (g_dialog.window != nullptr && IsWindow(g_dialog.window)) {
        ShowWindow(g_dialog.window, SW_HIDE);
    }
}

int FFreeDlg() {
    const Hdlg handle = g_current_dialog;
    auto found = g_dialogs.find(handle);
    if (found == g_dialogs.end()) {
        return false;
    }
    if (found->second.window != nullptr && IsWindow(found->second.window)) {
        DestroyWindow(found->second.window);
    }
    g_dialogs.erase(found);
    if (g_focus_dialog == handle) {
        g_focus_dialog = 0;
    }
    g_current_dialog = 0;
    sync_current_dialog_globals();
    return true;
}

Word HidOfDlg(Hdlg dialog) {
    const auto* state = find_dialog(dialog == 0 ? g_current_dialog : dialog);
    return state == nullptr ? 0 : state->hid;
}

Hdlg HdlgSetCurDlg(Hdlg dialog) {
    const Hdlg previous = g_current_dialog;
    if (dialog == 0 || find_dialog(dialog) != nullptr) {
        g_current_dialog = dialog;
        sync_current_dialog_globals();
    }
    return previous;
}

Hdlg HdlgSetFocusDlg(Hdlg dialog) {
    const Hdlg previous = g_focus_dialog;
    if (dialog == 0 || find_dialog(dialog) != nullptr) {
        g_focus_dialog = dialog;
        sync_current_dialog_globals();
    }
    return previous;
}

int FKillDlgFocus() {
    HdlgSetFocusDlg(0);
    return true;
}

int FModalDlg(Hdlg dialog) {
    const auto* state = find_dialog(dialog);
    return state != nullptr && state->modal;
}

int FIsDlgDying() { return g_dialog.dying; }
void ClearListError(Hdlg) {}

void ShowDlg(int visible) {
    g_dialog.visible = visible != 0;
    if (g_dialog.window != nullptr && IsWindow(g_dialog.window)) {
        ShowWindow(g_dialog.window, visible ? SW_SHOWNA : SW_HIDE);
    }
}
int FVisibleDlg() { return g_dialog.visible; }
void ResizeDlg(int width, int height) {
    if (g_dialog.template_handle != nullptr &&
        *g_dialog.template_handle != nullptr) {
        (*g_dialog.template_handle)->rec.dx = width;
        (*g_dialog.template_handle)->rec.dy = height;
    }
    if (g_dialog.window != nullptr && IsWindow(g_dialog.window)) {
        SetWindowPos(g_dialog.window, nullptr, 0, 0, (std::max)(1, width),
                     (std::max)(1, height),
                     SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
    }
}
void MoveDlg(int x, int y) {
    if (g_dialog.template_handle != nullptr &&
        *g_dialog.template_handle != nullptr) {
        (*g_dialog.template_handle)->rec.x = x;
        (*g_dialog.template_handle)->rec.y = y;
    }
    if (g_dialog.window != nullptr && IsWindow(g_dialog.window)) {
        SetWindowPos(g_dialog.window, nullptr, x, y, 0, 0,
                     SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
    }
}
void SdmScaleRec(Rec*) {}

int FSetDlgSab(Word sab) {
    g_dialog.sab = sab;
    if (g_dialog.hid == kIddSaveAs) {
        set_save_options_visible(g_dialog, true);
    }
    return true;
}
Word SabGetDlg() { return g_dialog.sab; }

void SetTmcVal_sdm21(Tmc tmc, Word value) {
    auto& state = control(tmc);
    state.value = value;
    if (state.window != nullptr && IsWindow(state.window)) {
        if (window_is_class(state.window, "BUTTON")) {
            SendMessageA(state.window, BM_SETCHECK,
                         value ? BST_CHECKED : BST_UNCHECKED, 0);
        } else if (window_is_class(state.window, "LISTBOX")) {
            SendMessageA(state.window, LB_SETCURSEL, value, 0);
        } else {
            SendMessageA(state.window, CB_SETCURSEL, value, 0);
        }
    }
}
Word ValGetTmc(Tmc tmc) {
    const Tmc value_tmc = static_cast<Tmc>(tmc & ~0x8000u);
    const auto found = g_dialog.controls.find(value_tmc);
    if (found == g_dialog.controls.end()) {
        return 0;
    }
    if (found->second.window != nullptr &&
        window_is_class(found->second.window, "BUTTON")) {
        found->second.value = static_cast<Word>(SendMessageA(
            found->second.window, BM_GETCHECK, 0, 0) == BST_CHECKED);
    } else if (found->second.window != nullptr &&
               window_is_class(found->second.window, "LISTBOX")) {
        const LRESULT selection = SendMessageA(
            found->second.window, LB_GETCURSEL, 0, 0);
        if (selection != LB_ERR) {
            found->second.value = static_cast<Word>(selection);
        }
    }
    refresh_font_control_value(g_dialog, value_tmc, found->second, true);
    return found->second.value;
}

void SetTmcText_sdm21(Tmc tmc, char* text) {
    auto& state = control(tmc);
    state.text = counted_or_zero_terminated(text);
    if (state.text.size() > state.text_limit) {
        state.text.resize(state.text_limit);
    }
    if (state.window != nullptr && IsWindow(state.window)) {
        SetWindowTextA(state.window, state.text.c_str());
    }
    refresh_font_control_value(g_dialog, tmc, state, false);
}
void GetTmcText_sdm21(Tmc tmc, char* destination, Word capacity) {
    const auto* state = find_control(tmc);
    copy_text(state == nullptr ? std::string{} : state->text, destination,
              capacity);
}
Word CchGetTmcText(Tmc tmc, char* destination, Word capacity) {
    const auto* state = find_control(tmc);
    const std::string empty;
    const auto& text = state == nullptr ? empty : state->text;
    copy_text(text, destination, capacity);
    return static_cast<Word>((std::min)(
        text.size(), static_cast<std::size_t>(0xffffu)));
}
Word CchGetTmc(Tmc tmc) { return CchGetTmcText(tmc, nullptr, 0); }

void GetTmcLargeVal(Tmc tmc, void* destination, Word byte_count) {
    if (destination == nullptr) {
        return;
    }
    const auto* state = find_control(tmc);
    const std::size_t count = state == nullptr
                                  ? 0
                                  : (std::min)(state->large_value.size(),
                                               std::size_t{byte_count});
    if (count != 0) {
        std::memcpy(destination, state->large_value.data(), count);
    }
    if (count < byte_count) {
        std::memset(static_cast<unsigned char*>(destination) + count, 0,
                    byte_count - count);
    }
}
int FSetTmcLargeVal(Tmc tmc, void* source) {
    if (source == nullptr) {
        return false;
    }
    /* Large values are application records; retain one pointer-sized unit
       when SDM has no generated TM metadata describing a larger record. */
    auto& value = control(tmc).large_value;
    value.resize(sizeof(void*));
    std::memcpy(value.data(), source, value.size());
    return true;
}

void SetFocusTmc(Tmc tmc) {
    g_dialog.focus = tmc;
    if (HWND window = ensure_control_window(tmc); window != nullptr) {
        SetFocus(window);
    }
}
Tmc TmcGetFocus() { return g_dialog.focus; }
void SetDefaultTmc(Tmc tmc) { g_dialog.default_tmc = tmc; }
Tmc TmcGetDefault(int) { return g_dialog.default_tmc; }
void SetTmcTxs(Tmc tmc, Dword selection) {
    control(tmc).selection = selection;
}
Dword TxsGetTmc(Tmc tmc) {
    const auto* state = find_control(tmc);
    return state == nullptr ? 0 : state->selection;
}
Word TmvGetTmc(Tmc tmc) {
    const auto* state = find_control(tmc);
    return state != nullptr && !state->text.empty() ? 3 : 1;
}
void RedisplayTmc(Tmc tmc) { populate_original_list(g_dialog, tmc); }

void EnableTmc_sdm21(Tmc tmc, int enabled) {
    auto& state = control(tmc);
    state.enabled = enabled != 0;
    if (state.window != nullptr && IsWindow(state.window)) {
        EnableWindow(state.window, state.enabled);
    }
}
int FEnabledTmc_sdm21(Tmc tmc) {
    const auto* state = find_control(tmc);
    return state == nullptr || state->enabled;
}
void EnableNoninteractiveTmc(Tmc tmc, int enabled) {
    EnableTmc_sdm21(tmc, enabled);
}
void SetVisibleTmc(Tmc tmc, int visible) {
    auto& state = control(tmc);
    state.visible = visible != 0;
    if (state.window != nullptr && IsWindow(state.window)) {
        ShowWindow(state.window, state.visible ? SW_SHOWNA : SW_HIDE);
    }
}
int FIsVisibleTmc(Tmc tmc) {
    const auto* state = find_control(tmc);
    return state == nullptr || state->visible;
}
void LimitTextTmc(Tmc tmc, Word limit) {
    auto& state = control(tmc);
    state.text_limit = limit;
    if (state.text.size() > limit) {
        state.text.resize(limit);
    }
}
void CompleteComboTmc(Tmc) {}

void AddListBoxEntry(Tmc tmc, char* entry) {
    auto& state = control(tmc);
    state.entries.emplace_back(counted_or_zero_terminated(entry));
    add_entry_to_native_control(state, state.entries.back());
}
void InsertListBoxEntry(Tmc tmc, char* entry, Word index) {
    auto& state = control(tmc);
    auto& entries = state.entries;
    const auto position = (std::min)(entries.size(), std::size_t{index});
    entries.insert(entries.begin() + static_cast<std::ptrdiff_t>(position),
                   counted_or_zero_terminated(entry));
    if (state.window != nullptr && IsWindow(state.window)) {
        SendMessageA(state.window, CB_INSERTSTRING,
                     static_cast<WPARAM>(position),
                     reinterpret_cast<LPARAM>(entries[position].c_str()));
    }
}
void DeleteListBoxEntry(Tmc tmc, Word index) {
    auto& state = control(tmc);
    auto& entries = state.entries;
    if (index < entries.size()) {
        entries.erase(entries.begin() + index);
        if (state.window != nullptr && IsWindow(state.window)) {
            SendMessageA(state.window, CB_DELETESTRING, index, 0);
        }
    }
}
Word CentryListBoxTmc(Tmc tmc) {
    const auto* state = find_control(tmc);
    return static_cast<Word>(state == nullptr
                                 ? 0
                                 : (std::min)(state->entries.size(),
                                              std::size_t{0xffffu}));
}
Word CEntryListBoxTmc(Tmc tmc) { return CentryListBoxTmc(tmc); }
void GetListBoxEntry(Tmc tmc, Word index, char* destination, Word capacity) {
    const auto* state = find_control(tmc);
    copy_text(state == nullptr || index >= state->entries.size()
                  ? std::string{}
                  : state->entries[index],
              destination, capacity);
}
Word CchGetListBoxEntry(Tmc tmc, Word index, char* destination,
                        Word capacity) {
    const auto* state = find_control(tmc);
    const std::string empty;
    const auto& entry = state == nullptr || index >= state->entries.size()
                            ? empty
                            : state->entries[index];
    copy_text(entry, destination, capacity);
    return static_cast<Word>((std::min)(entry.size(), std::size_t{0xffffu}));
}
Word IEntryFindListBox(Tmc tmc, char* entry, Word*) {
    const auto* state = find_control(tmc);
    if (state == nullptr) {
        return static_cast<Word>(-1);
    }
    const std::string sought = counted_or_zero_terminated(entry);
    const auto found = std::find(state->entries.begin(), state->entries.end(),
                                 sought);
    return found == state->entries.end()
               ? static_cast<Word>(-1)
               : static_cast<Word>(found - state->entries.begin());
}
Word CselListBoxTmc(Tmc tmc) {
    const auto* state = find_control(tmc);
    return state == nullptr || state->entries.empty() ? 0 : 1;
}
void StartListBoxUpdate(Tmc) {}
void BeginListBoxUpdate(Tmc, int) {}
void EndListBoxUpdate(Tmc) {}

Hcab HcabFromDlg(int dialog) {
    const auto* state =
        find_dialog(dialog == 0 ? g_current_dialog : static_cast<Hdlg>(dialog));
    return state == nullptr ? nullptr : state->cab;
}
void SaveCabs(void (*callback)(Hcab, Word, Tmc, int), int save) {
    if (callback != nullptr && g_dialog.cab != nullptr) {
        callback(g_dialog.cab, g_dialog.hid, 0, save);
    }
}

void GetTmcRec(Tmc tmc, Rec* rectangle) {
    if (rectangle == nullptr) {
        return;
    }
    const auto* state = find_control(tmc);
    *rectangle = state == nullptr ? Rec{} : state->rectangle;
}
HWND HwndOfTmc(Tmc tmc) { return ensure_control_window(tmc); }
Hdlg HdlgFromHwnd(HWND window) {
    if (window == nullptr) {
        return 0;
    }
    for (const auto& entry : g_dialogs) {
        if (entry.second.window == window ||
            (entry.second.window != nullptr &&
             IsChild(entry.second.window, window))) {
            return entry.first;
        }
    }
    return 0;
}
HWND HwndFromDlg(Hdlg dialog) {
    const auto* state = find_dialog(dialog == 0 ? g_current_dialog : dialog);
    return state == nullptr ? nullptr : state->window;
}
HWND HwndSwapSdmParent(HWND window) {
    return std::exchange(vhWndMsgBoxParent, window);
}
int FIsDlgInteractive() { return !g_noninteractive; }
void SetDlgCaption(char* caption) {
    g_dialog.caption = counted_or_zero_terminated(caption);
    if (g_dialog.window != nullptr && IsWindow(g_dialog.window)) {
        SetWindowTextA(g_dialog.window, g_dialog.caption.c_str());
    }
}

int FSetNoninteractive(Word, Tmc) {
    g_noninteractive = true;
    return true;
}
void EndSdmTranscription() { g_noninteractive = false; }
int FExecutable() { return true; }
void CBTState(int) {}
int FRestoreDlg(int) { return true; }
int FRestoreTmc(Tmc, int) { return true; }

Word IdDoMsgBox(char* text, char* caption, Word flags) {
    return static_cast<Word>(MessageBoxA(vhWndMsgBoxParent, text, caption,
                                         static_cast<UINT>(flags)));
}

Word FlbfFillDirListTmc(char*, char*, Tmc, Tmc, Tmc, Word, Word) {
    return 0x0008;  // flbfListingMade; directory enumeration is UI-backed.
}

}  // extern "C"

namespace {

void set_sds_handle(const Hdlg current, const Hdlg focus) {
    sds.current_dialog =
        reinterpret_cast<void**>(static_cast<std::uintptr_t>(current));
    sds.focus_dialog =
        reinterpret_cast<void**>(static_cast<std::uintptr_t>(focus));
}

}  // namespace
