/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once

/** \file
 * \ingroup bke
 */

#include <memory>
#include <string>

#include "BLI_compiler_attrs.h"
#include "BLI_enum_flags.hh"
#include "BLI_map.hh"
#include "BLI_math_vector_types.hh"
#include "BLI_string_ref.hh"
#include "BLI_vector.hh"

#include "DNA_listBase.h"
#include "DNA_space_types.h"
#include "DNA_vec_types.h"

#include "RNA_types.hh"

#include "BKE_context.hh"

namespace blender {
namespace bke::id {
class IDRemapper;
}

namespace asset_system {
class AssetRepresentation;
}

namespace ui {
struct Layout;
struct Block;
}  // namespace ui

struct ARegion;
struct ARegionType;
struct AssetShelfType;
struct BlendDataReader;
struct BlendLibReader;
struct BlendWriter;
struct Header;
struct HeaderType;
struct ID;
struct LayoutPanelState;
struct LibraryForeachIDData;
struct Menu;
struct Panel;
struct PanelType;
struct PanelCategoryDyn;
struct RegionDrawCB;
struct Scene;
struct ScrArea;
struct ScrAreaMap;
struct ScrEdge;
struct ScrVert;
struct SpaceType;
struct View3D;
struct View3DShading;
struct WorkSpace;
struct bContext;
struct bScreen;
struct uiList;
struct wmDrawBuffer;
struct wmEventHandler;
struct wmGizmoMap;
struct wmKeyConfig;
struct wmMsgBus;
struct wmNotifier;
struct wmTimer;
struct wmWindow;
struct wmWindowManager;
struct RegionDrawCB;
struct PanelType;
struct HeaderType;
struct ARegionType;
struct wmEventHandler;
struct PanelCategoryDyn;

/* spacetype has everything stored to get an editor working, it gets initialized via
 * #ED_spacetypes_init() in `editors/space_api/spacetypes.cc` */
/* an editor in Blender is a combined ScrArea + SpaceType + SpaceData. */

#define BKE_ST_MAXNAME 64

struct wmSpaceTypeListenerParams {
  wmWindow *window;
  ScrArea *area;
  const wmNotifier *notifier;
  const Scene *scene;
  const Main *bmain;
};

struct SpaceType {
  /** For menus. */
  char name[BKE_ST_MAXNAME];
  /** Unique space identifier. */
  int spaceid;
  /** Icon lookup for menus. */
  int iconid;

  /**
   * Initial allocation, after this WM will call init() too.
   * Some editors need area and scene data (e.g. frame range) to set their initial scrolling.
   */
  SpaceLink *(*create)(const ScrArea *area, const Scene *scene);
  /** Not free spacelink itself. */
  void (*free)(SpaceLink *sl);

  /** Init is to cope with file load, screen (size) changes, check handlers. */
  void (*init)(wmWindowManager *wm, ScrArea *area);
  /** Exit is called when the area is hidden or removed. */
  void (*exit)(wmWindowManager *wm, ScrArea *area);
  /** Listeners can react to bContext changes. */
  void (*listener)(const wmSpaceTypeListenerParams *params);

  /** Called when the mouse moves out of the area. */
  void (*deactivate)(ScrArea *area);

  /** Refresh context, called after file-reads, #ED_area_tag_refresh(). */
  void (*refresh)(const bContext *C, ScrArea *area);

  /** After a spacedata copy, an init should result in exact same situation. */
  SpaceLink *(*duplicate)(SpaceLink *sl);

  /** Register operator types on startup. */
  void (*operatortypes)();
  /** Add default items to WM keymap. */
  void (*keymap)(wmKeyConfig *keyconf);
  /** On startup, define dropboxes for spacetype+regions. */
  void (*dropboxes)();

  /** Initialize gizmo-map-types and gizmo-group-types with the region. */
  void (*gizmos)();

  /** Return context data. */
  bContextDataCallback context;

  /** Used when we want to replace an ID by another (or NULL). */
  void (*id_remap)(ScrArea *area, SpaceLink *sl, const bke::id::IDRemapper &mappings);

  /**
   * foreach_id callback to process all ID pointers of the editor. Used indirectly by lib_query's
   * #BKE_library_foreach_ID_link when #IDWALK_INCLUDE_UI bit-flag is set (through WM's foreach_id
   * usage of #BKE_screen_foreach_id_screen_area).
   */
  void (*foreach_id)(SpaceLink *space_link, LibraryForeachIDData *data);

  int (*space_subtype_get)(ScrArea *area);
  void (*space_subtype_set)(ScrArea *area, int value);
  void (*space_subtype_item_extend)(bContext *C, EnumPropertyItem **item, int *totitem);

  /** Return a custom name, based on subtype or other reason. */
  StringRefNull (*space_name_get)(const ScrArea *area);
  /** Return a custom icon, based on subtype or other reason. */
  int (*space_icon_get)(const ScrArea *area);

  /**
   * Update pointers for all structs directly owned by this space.
   */
  void (*blend_read_data)(BlendDataReader *reader, SpaceLink *space_link);

  /**
   * Update pointers to other id data blocks.
   */
  void (*blend_read_after_liblink)(BlendLibReader *reader, ID *parent_id, SpaceLink *space_link);

  /**
   * Write all structs that should be saved in a .blend file.
   */
  void (*blend_write)(BlendWriter *writer, SpaceLink *space_link);

  /** Region type definitions. */
  ListBaseT<ARegionType> regiontypes;

  /* read and write... */

  /** Default key-maps to add. */
  int keymapflag;

  ~SpaceType();
};

/* Region types are also defined using spacetypes_init, via a callback. */

struct wmRegionListenerParams {
  wmWindow *window;
  /** Can be NULL when the region is not part of an area. */
  ScrArea *area;
  ARegion *region;
  const wmNotifier *notifier;
  const Scene *scene;
};

struct wmRegionMessageSubscribeParams {
  const bContext *context;
  wmMsgBus *message_bus;
  WorkSpace *workspace;
  Scene *scene;
  bScreen *screen;
  ScrArea *area;
  ARegion *region;
};

struct RegionPollParams {
  const bScreen *screen;
  const ScrArea *area;
  const ARegion *region;

  /** Full context, if WM context above is not enough. */
  const bContext *context;
};

enum class ARegionTypeFlag {
  /**
   * Use panel categories, where #PanelType.category and #ARegion.panels_category_active define
   * which panels are visible. Available categories are collected during layout and cached in
   * #ARegion.panels_category_active.
   */
  UsePanelCategories = (1 << 0),
  /**
   * Same as #UsePanelCategories, plus panel drawing will draw tabs for the categories in this
   * region.
   */
  UsePanelCategoryTabs = (1 << 1),

  /**
   * When using panel categories, this hides the sidebar tab where there is only one category
   * active.
   */
  HideSinglePanelCategories = (1 << 2),
};
ENUM_OPERATORS(ARegionTypeFlag)

/* #ARegionType::lock */
enum ARegionDrawLockFlags {
  REGION_DRAW_LOCK_NONE = 0,
  REGION_DRAW_LOCK_RENDER = (1 << 0),
  REGION_DRAW_LOCK_BAKING = (1 << 1),
  REGION_DRAW_LOCK_ALL = (REGION_DRAW_LOCK_RENDER | REGION_DRAW_LOCK_BAKING)
};

struct ARegionType {
  ARegionType *next, *prev;
  /** Unique identifier within this space, defines `RGN_TYPE_xxxx`. */
  int regionid;

  /** Add handlers, stuff you only do once or on area/region type/size changes. */
  void (*init)(wmWindowManager *wm, ARegion *region);
  /** Exit is called when the region is hidden or removed. */
  void (*exit)(wmWindowManager *wm, ARegion *region);
  /**
   * Optional callback to decide whether the region should be treated as existing given the
   * current context. When returning false, the region will be kept in storage, but is not
   * available to the user in any way. Callbacks can assume that context has the owning area and
   * space-data set.
   */
  bool (*poll)(const RegionPollParams *params);
  /** Draw entirely, view changes should be handled here. */
  void (*draw)(const bContext *C, ARegion *region);
  /**
   * Handler to draw overlays. This handler is called every draw loop.
   *
   * \note Some editors should return early if the interface is locked
   * (check with #CTX_wm_interface_locked) to avoid accessing scene data
   * that another thread may be modifying
   */
  void (*draw_overlay)(const bContext *C, ARegion *region);
  /** Optional, compute button layout before drawing for dynamic size. */
  void (*layout)(const bContext *C, ARegion *region);
  /** Snap the size of the region (can be NULL for no snapping). */
  int (*snap_size)(const ARegion *region, int size, int axis);
  /** Contextual changes should be handled here. */
  void (*listener)(const wmRegionListenerParams *params);
  /** Optional callback to generate subscriptions. */
  void (*message_subscribe)(const wmRegionMessageSubscribeParams *params);

  void (*free)(ARegion *);

  /** Split region, copy data optionally. */
  void *(*duplicate)(void *poin);

  /** Allows default cursor per region. */
  void (*cursor)(wmWindow *win, ScrArea *area, ARegion *region);

  /** Return context data. */
  bContextDataCallback context;

  /**
   * Called on every frame in which the region's poll succeeds, regardless of visibility, before
   * drawing, visibility evaluation and initialization. Allows the region to override visibility.
   */
  void (*on_poll_success)(const bContext *C, ARegion *region);

  /**
   * Called whenever the user changes the region's size. Not called when the size is changed
   * through other means, like to adjust for a scaled down window.
   */
  void (*on_user_resize)(const ARegion *region);
  /**
   * Is called whenever the current visible View2D's region changes.
   *
   * Used from user code such as view navigation/zoom operators to inform region about changes.
   * The goal is to support zoom-to-fit features which gets disabled when manual navigation is
   * performed.
   *
   * This callback is not called on indirect changes of the current viewport (which could happen
   * when the `v2d->tot` is changed and `cur` is adopted accordingly).
   */
  void (*on_view2d_changed)(const bContext *C, ARegion *region);

  ARegionTypeFlag flag;

  /** Custom drawing callbacks. */
  ListBaseT<RegionDrawCB> drawcalls;

  /** Panels type definitions. */
  ListBaseT<PanelType> paneltypes;

  /** Header type definitions. */
  ListBaseT<HeaderType> headertypes;

  /** Hardcoded constraints, smaller than these values region is not visible. */
  int minsizex, minsizey;
  /** When new region opens (region prefsizex/y are zero then). */
  int prefsizex, prefsizey;
  /** Default keymaps to add. */
  int keymapflag;
  /**
   * Return without drawing.
   * lock is set by region definition, and copied to do_lock by render.
   * Set as bitflag value in #ARegionDrawLockFlags.
   */
  short do_lock, lock;
  /** Don't handle gizmos events behind #ui::Block's with #BLOCK_CLIP_EVENTS flag set. */
  bool clip_gizmo_events_by_ui;
  /** Call cursor function on each move event. */
  short event_cursor;
};

/* Panel types. */

struct PanelType {
  PanelType *next, *prev;

  /** Unique name. */
  char idname[BKE_ST_MAXNAME];
  /** For panel header. */
  char label[BKE_ST_MAXNAME];
  /** For panel tooltip. */
  const char *description;
  int icon;
  char translation_context[BKE_ST_MAXNAME];
  /** For buttons window. */
  char context[BKE_ST_MAXNAME];
  /** For category tabs. */
  char category[BKE_ST_MAXNAME];
  /** For work-spaces to selectively show. */
  char owner_id[128];
  /** Parent idname for sub-panels. */
  char parent_id[BKE_ST_MAXNAME];
  /** Boolean property identifier of the panel custom data. Used to draw a highlighted border. */
  char active_property[BKE_ST_MAXNAME];
  char pin_to_last_property[BKE_ST_MAXNAME];
  short space_type;
  short region_type;
  /** For popovers, 0 for default. */
  int ui_units_x;
  /**
   * For popovers, position the popover at the given offset (multiplied by #UI_UNIT_X/#UI_UNIT_Y)
   * relative to the top left corner, if it's not attached to a button.
   */
  float2 offset_units_xy;
  int order;

  int flag;

  /** Verify if the panel should draw or not. */
  bool (*poll)(const bContext *C, PanelType *pt);
  /** Draw header (optional) */
  void (*draw_header)(const bContext *C, Panel *panel);
  /** Draw header preset (optional) */
  void (*draw_header_preset)(const bContext *C, Panel *panel);
  /** Draw entirely, view changes should be handled here. */
  void (*draw)(const bContext *C, Panel *panel);
  /**
   * Listener to redraw the region this is contained in on changes. Only used for panels displayed
   * in popover regions.
   */
  void (*listener)(const wmRegionListenerParams *params);

  /* For instanced panels corresponding to a list: */

  /** Reorder function, called when drag and drop finishes. */
  void (*reorder)(bContext *C, Panel *pa, int new_index);
  /**
   * Get the panel and sub-panel's expansion state from the expansion flag in the corresponding
   * data item. Called on draw updates.
   * \note Sub-panels are indexed in depth first order,
   * the visual order you would see if all panels were expanded.
   */
  short (*get_list_data_expand_flag)(const bContext *C, Panel *pa);
  /**
   * Set the expansion bit-field from the closed / open state of this panel and its sub-panels.
   * Called when the expansion state of the panel changes with user input.
   * \note Sub-panels are indexed in depth first order,
   * the visual order you would see if all panels were expanded.
   */
  void (*set_list_data_expand_flag)(const bContext *C, Panel *pa, short expand_flag);

  /** Sub panels. */
  PanelType *parent;
  ListBaseT<LinkData> children;
  /** RNA integration. */
  ExtensionRNA rna_ext;
};

/** #PanelType.flag */
enum {
  PANEL_TYPE_DEFAULT_CLOSED = (1 << 0),
  PANEL_TYPE_NO_HEADER = (1 << 1),
  /** Makes buttons in the header shrink/stretch to fill full layout width. */
  PANEL_TYPE_HEADER_EXPAND = (1 << 2),
  PANEL_TYPE_LAYOUT_VERT_BAR = (1 << 3),
  /** This panel type represents data external to the UI. */
  PANEL_TYPE_INSTANCED = (1 << 4),
  /** Don't search panels with this type during property search. */
  PANEL_TYPE_NO_SEARCH = (1 << 7),
};

struct LayoutPanelHeader {
  float start_y;
  float end_y;
  PointerRNA open_owner_ptr;
  std::string open_prop_name;
};

struct LayoutPanelBody {
  float start_y;
  float end_y;
};

/**
 * "Layout Panels" are panels which are defined as part of the #ui::Layout. As such they
 * have a specific place in the layout and can not be freely dragged around like top level panels.
 *
 * This struct gathers information about the layout panels created by layout code. This is then
 * used for example drawing the backdrop of nested panels and to support opening and closing
 * multiple panels with a single mouse gesture.
 */
struct LayoutPanels {
  Vector<LayoutPanelHeader> headers;
  Vector<LayoutPanelBody> bodies;

  void clear()
  {
    this->headers.clear();
    this->bodies.clear();
  }
};

struct Panel_Runtime {
  /** Applied to Panel.ofsx, but saved separately so we can track changes between redraws. */
  int region_ofsx = 0;

  /**
   * Pointer for storing which data the panel corresponds to.
   * Useful when there can be multiple instances of the same panel type.
   *
   * \note A panel and its sub-panels share the same custom data pointer.
   * This avoids freeing the same pointer twice when panels are removed.
   */
  PointerRNA *custom_data_ptr = nullptr;

  /**
   * Pointer to the panel's block. Useful when changes to panel #ui::Blocks
   * need some context from traversal of the panel "tree".
   */
  ui::Block *block = nullptr;

  /** Non-owning pointer. The context is stored in the block. */
  bContextStore *context = nullptr;

  /** Information about nested layout panels generated in layout code. */
  LayoutPanels layout_panels;

  /**
   * Custom storage for saving the open-close-state of layout panels created with
   * `layout.panel(...)`. This precedes #Panel::layout_panel_states when storing layout panel
   * state.
   */
  ListBaseT<LayoutPanelState> *layout_panel_states_storage = nullptr;
};

namespace bke {

/** #ARegionRuntime.quadview_index */
enum class ARegionQuadviewIndex : uint8_t {
  None = 0,
  BottomLeft = 1,
  TopLeft = 2,
  BottomRight = 3,
  TopRight = 4,
};

struct ARegionRuntime {
  /** Callbacks for this region type. */
  struct ARegionType *type;

  /** Runtime for partial redraw, same or smaller than #ARegion::winrct. */
  rcti drawrct = {};

  /**
   * The visible part of the region, use with region overlap not to draw
   * on top of the overlapping regions.
   *
   * Lazy initialize, zeroed when unset, relative to #ARegion.winrct x/y min.
   */
  rcti visible_rect = {};

  /**
   * The offset needed to not overlap with window scroll-bars.
   * Only used by HUD regions for now.
   */
  int offset_x = 0;
  int offset_y = 0;

  /** Panel category to use between 'layout' and 'draw'. */
  const char *category = nullptr;

  /** Maps #ui::Block::name to ui::Block for faster lookups. */
  Map<std::string, ui::Block *> block_name_map;
  ListBaseT<ui::Block> uiblocks = {};

  ListBaseT<wmEventHandler> handlers = {};

  /** Use this string to draw info. */
  char *headerstr = nullptr;

  /** Gizmo-map of this region. */
  wmGizmoMap *gizmo_map = nullptr;

  /** Blend in/out. */
  wmTimer *regiontimer = nullptr;

  wmDrawBuffer *draw_buffer = nullptr;

  /** Panel categories runtime. */
  ListBaseT<PanelCategoryDyn> panels_category = {};

  /** Region is currently visible on screen. */
  short visible = 0;

  /** Private, cached notifier events. */
  short do_draw = 0;

  /** Private, cached notifier events. */
  short do_draw_paintcursor;

  ARegionQuadviewIndex quadview_index = ARegionQuadviewIndex::None;

  /** Dummy panel used in popups so they can support layout panels. */
  Panel *popup_block_panel = nullptr;
};

}  // namespace bke

/* #uiList types. */

/** Draw an item in the `ui_list`. */
using uiListDrawItemFunc = void (*)(uiList *ui_list,
                                    const bContext *C,
                                    ui::Layout &layout,
                                    PointerRNA *dataptr,
                                    PointerRNA *itemptr,
                                    int icon,
                                    PointerRNA *active_dataptr,
                                    const char *active_propname,
                                    int index,
                                    int flt_flag);

/** Draw the filtering part of an uiList. */
using uiListDrawFilterFunc = void (*)(uiList *ui_list, const bContext *C, ui::Layout &layout);

/** Filter items of an uiList. */
using uiListFilterItemsFunc = void (*)(uiList *ui_list,
                                       const bContext *C,
                                       PointerRNA *,
                                       const char *propname);

/** Listen to notifiers. Only for lists defined in C. */
using uiListListener = void (*)(uiList *ui_list, wmRegionListenerParams *params);

struct uiListType {
  uiListType *next, *prev;

  /** Unique name. */
  char idname[BKE_ST_MAXNAME];

  uiListDrawItemFunc draw_item;
  uiListDrawFilterFunc draw_filter;
  uiListFilterItemsFunc filter_items;

  /** For lists defined in C only. */
  uiListListener listener;

  /** RNA integration. */
  ExtensionRNA rna_ext;
};

/* Header types. */

struct HeaderType {
  HeaderType *next, *prev;
  /** Unique name. */
  char idname[BKE_ST_MAXNAME];
  int space_type;
  int region_type;

  bool (*poll)(const bContext *C, HeaderType *ht);
  /** Draw entirely, view changes should be handled here. */
  void (*draw)(const bContext *C, Header *header);

  /** RNA integration. */
  ExtensionRNA rna_ext;
};

struct Header {
  /** Runtime. */
  HeaderType *type;
  /** Runtime for drawing. */
  ui::Layout *layout;
};

/* Menu types. */

enum class MenuTypeFlag {
  /**
   * Whether the menu depends on data retrieved via #CTX_data_pointer_get. If it is context
   * dependent, menu search has to scan it in different contexts.
   */
  ContextDependent = (1 << 0),
  /**
   * Automatically start searching in the menu when pressing a key.
   */
  SearchOnKeyPress = (1 << 1),
};
ENUM_OPERATORS(MenuTypeFlag)

struct MenuType {
  MenuType *next, *prev;

  /** Unique name. */
  char idname[BKE_ST_MAXNAME];
  /** For button text. */
  char label[BKE_ST_MAXNAME];
  char translation_context[BKE_ST_MAXNAME];
  /** Optional, see: #wmOwnerID. */
  char owner_id[128];
  const char *description;

  /** Verify if the menu should draw or not. */
  bool (*poll)(const bContext *C, MenuType *mt);
  /** Draw entirely, view changes should be handled here. */
  void (*draw)(const bContext *C, Menu *menu);
  void (*listener)(const wmRegionListenerParams *params);

  MenuTypeFlag flag;

  /** RNA integration. */
  ExtensionRNA rna_ext;
};

struct Menu {
  /** Runtime. */
  MenuType *type;
  /** Runtime for drawing. */
  ui::Layout *layout;
};

/* Asset shelf types. */

/* #AssetShelfType.flag */
enum AssetShelfTypeFlag {
  /**
   * Do not trigger asset dragging on drag events.
   * Drag events can be overridden with custom keymap items then.
   */
  ASSET_SHELF_TYPE_FLAG_NO_ASSET_DRAG = (1 << 0),
  ASSET_SHELF_TYPE_FLAG_DEFAULT_VISIBLE = (1 << 1),
  ASSET_SHELF_TYPE_FLAG_STORE_CATALOGS_IN_PREFS = (1 << 2),
  /**
   * When spawning a context menu for an asset, activate the asset and call the activate operator
   * (`bl_activate_operator`/#AssetShelfType.activate_operator) if present, rather than just
   * highlighting the asset as active.
   */
  ASSET_SHELF_TYPE_FLAG_ACTIVATE_FOR_CONTEXT_MENU = (1 << 3),
};
ENUM_OPERATORS(AssetShelfTypeFlag);

#define ASSET_SHELF_PREVIEW_SIZE_DEFAULT 48

struct AssetShelfType {
  /** Unique name. */
  char idname[BKE_ST_MAXNAME];

  int space_type;

  /**
   * `FILTER_ID_` bit-flags to pre-filter ID types to include in the asset shelf, as if
   * #asset_poll() returned false for non-matching IDs. If this isn't set (== 0), no pre-filtering
   * will be done.
   *
   * For bigger asset libraries, many assets can usually be excluded cheaply this way. Calling
   * #asset_poll() on many assets isn't cheap, so doing the ID type check only in there can cause
   * performance issues.
   */
  uint64_t id_types_prefilter; /* rna_enum_id_type_filter_items */

  /** Operator to call when activating a grid view item. */
  std::string activate_operator;
  /** Operator to call when dragging a grid view item. */
  std::string drag_operator;

  AssetShelfTypeFlag flag;

  short default_preview_size;

  /** Determine if asset shelves of this type should be available in current context or not. */
  bool (*poll)(const bContext *C, const AssetShelfType *shelf_type);

  /**
   * Determine if an individual asset should be visible or not.
   * Don't use directly, use #ed::asset::shelf::type_asset_poll() (does additional
   * pre-filtering based on the ID-type).
   */
  bool (*asset_poll)(const AssetShelfType *shelf_type,
                     const asset_system::AssetRepresentation *asset);

  /** Asset shelves can define their own context menu via this layout definition callback. */
  void (*draw_context_menu)(const bContext *C,
                            const AssetShelfType *shelf_type,
                            const asset_system::AssetRepresentation *asset,
                            ui::Layout &layout);

  const AssetWeakReference *(*get_active_asset)(const AssetShelfType *shelf_type);

  /** RNA integration. */
  ExtensionRNA rna_ext;
};

/* Space-types. */

SpaceType *BKE_spacetype_from_id(int spaceid);
ARegionType *BKE_regiontype_from_id(const SpaceType *st, int regionid);
Span<std::unique_ptr<SpaceType>> BKE_spacetypes_list();
void BKE_spacetype_register(std::unique_ptr<SpaceType> st);
bool BKE_spacetype_exists(int spaceid);
/** Only for quitting blender. */
void BKE_spacetypes_free();

bool BKE_regiontype_uses_categories(const ARegionType *region_type);
bool BKE_regiontype_uses_category_tabs(const ARegionType *region_type);

/* Space-data. */

void BKE_spacedata_freelist(ListBaseT<SpaceLink> *lb);
/**
 * \param lb_dst: should be empty (will be cleared).
 */
void BKE_spacedata_copylist(ListBaseT<SpaceLink> *lb_dst, ListBaseT<SpaceLink> *lb_src);

/**
 * Facility to set locks for drawing to survive (render) threads accessing drawing data.
 *
 * \note Should be replaced in future by better local data handling for threads.
 * \note Effect of multiple calls to this function is not accumulative. The locking flags
 * will be set to by the last call.
 */
void BKE_spacedata_draw_locks(ARegionDrawLockFlags lock_flags);

/**
 * Version of #BKE_area_find_region_type that also works if \a slink
 * is not the active space of \a area.
 */
ARegion *BKE_spacedata_find_region_type(const SpaceLink *slink,
                                        const ScrArea *area,
                                        int region_type) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();

void BKE_spacedata_callback_id_remap_set(
    void (*func)(ScrArea *area, SpaceLink *sl, ID *old_id, ID *new_id));
/**
 * Currently unused!
 */
void BKE_spacedata_id_unref(ScrArea *area, SpaceLink *sl, ID *id);

/* Area/regions. */

ARegion *BKE_area_region_copy(const SpaceType *st, const ARegion *region);

ARegion *BKE_area_region_new();

/**
 * Doesn't free the region itself.
 */
void BKE_area_region_free(SpaceType *st, ARegion *region);
void BKE_area_region_panels_free(ListBaseT<Panel> *panels);
/**
 * Create and free panels.
 */
Panel *BKE_panel_new(PanelType *panel_type);
void BKE_panel_free(Panel *panel);
/**
 * Doesn't free the area itself.
 */
void BKE_screen_area_free(ScrArea *area);
/**
 * Gizmo-maps of a region need to be freed with the region.
 * Uses callback to avoid low-level call.
 */
void BKE_region_callback_free_gizmomap_set(void (*callback)(wmGizmoMap *));
void BKE_region_callback_refresh_tag_gizmomap_set(void (*callback)(wmGizmoMap *));

/**
 * Get the layout panel state for the given idname. If it does not exist yet, initialize a new
 * panel state with the given default value.
 */
LayoutPanelState *BKE_panel_layout_panel_state_ensure(Panel *panel,
                                                      StringRef idname,
                                                      bool default_closed);

/**
 * Find a region of type \a region_type in provided \a regionbase.
 *
 * \note this is useful for versioning where either the #Area or #SpaceLink regionbase are typical
 * inputs
 */
ARegion *BKE_region_find_in_listbase_by_type(const ListBaseT<ARegion> *regionbase,
                                             const int region_type);

void BKE_area_copy(ScrArea *area_dst, ScrArea *area_src);

/**
 * Find a region of type \a region_type in the currently active space of \a area.
 *
 * \note This does _not_ work if the region to look up is not in the active space.
 * Use #BKE_spacedata_find_region_type if that may be the case.
 */
ARegion *BKE_area_find_region_type(const ScrArea *area, int region_type);
ARegion *BKE_area_find_region_active_win(const ScrArea *area);
ARegion *BKE_area_find_region_xy(const ScrArea *area, int regiontype, const int xy[2])
    ATTR_NONNULL(3);

/**
 * \note This is only for screen level regions (typically menus/popups).
 */
ARegion *BKE_screen_find_region_type(const bScreen *screen, int region_type) ATTR_NONNULL(1);
/**
 * \note This is only for screen level regions (typically menus/popups).
 */
ARegion *BKE_screen_find_region_xy(const bScreen *screen,
                                   int regiontype,
                                   const int xy[2]) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1, 3);

ARegion *BKE_screen_find_main_region_at_xy(const bScreen *screen, int space_type, const int xy[2])
    ATTR_NONNULL(1, 3);
/**
 * \note Ideally we can get the area from the context,
 * there are a few places however where this isn't practical.
 */
ScrArea *BKE_screen_find_area_from_space(const bScreen *screen,
                                         const SpaceLink *sl) ATTR_WARN_UNUSED_RESULT
    ATTR_NONNULL(1, 2);
ARegion *BKE_screen_find_region_in_space(const bScreen *screen,
                                         const SpaceLink *sl,
                                         int region_type) ATTR_WARN_UNUSED_RESULT
    ATTR_NONNULL(1, 2);
/**
 * \note used to get proper RNA paths for spaces (editors).
 */
std::optional<std::string> BKE_screen_path_from_screen_to_space(const PointerRNA *ptr);
std::optional<std::string> BKE_screen_path_from_screen_to_area(const PointerRNA *ptr);
/**
 * \note Using this function is generally a last resort, you really want to be
 * using the context when you can - campbell
 */
ScrArea *BKE_screen_find_big_area(const bScreen *screen, int spacetype, short min);
ScrArea *BKE_screen_area_map_find_area_xy(const ScrAreaMap *areamap,
                                          int spacetype,
                                          const int xy[2]) ATTR_NONNULL(1, 3);
ScrArea *BKE_screen_find_area_xy(const bScreen *screen, int spacetype, const int xy[2])
    ATTR_NONNULL(1, 3);

void BKE_screen_gizmo_tag_refresh(bScreen *screen);

/**
 * Refresh any screen data that should be set on file-load
 * with "Load UI" disabled.
 */
void BKE_screen_runtime_refresh_for_blendfile(bScreen *screen);

void BKE_screen_view3d_sync(View3D *v3d, Scene *scene);
void BKE_screen_view3d_scene_sync(bScreen *screen, Scene *scene);
bool BKE_screen_is_fullscreen_area(const bScreen *screen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
bool BKE_screen_is_used(const bScreen *screen) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();

/* Zoom factor conversion. */

float BKE_screen_view3d_zoom_to_fac(float camzoom);
float BKE_screen_view3d_zoom_from_fac(float zoomfac);

void BKE_screen_view3d_shading_init(View3DShading *shading);

/* Screen. */

/**
 * Callback used by lib_query to walk over all ID usages
 * (mimics `foreach_id` callback of #IDTypeInfo structure).
 */
void BKE_screen_foreach_id_screen_area(LibraryForeachIDData *data, ScrArea *area);

/**
 * Free (or release) any data used by this screen (does not free the screen itself).
 */
void BKE_screen_free_data(bScreen *screen);
void BKE_screen_area_map_free(ScrAreaMap *area_map) ATTR_NONNULL();

/**
 * bScreen copying. Assumes that #screen_dst is cleared and can be fully overwritten.
 */
void BKE_screen_copy_data(bScreen *screen_dst, const bScreen *screen_src);

ScrEdge *BKE_screen_find_edge(const bScreen *screen, ScrVert *v1, ScrVert *v2);
void BKE_screen_sort_scrvert(ScrVert **v1, ScrVert **v2);
void BKE_screen_remove_double_scrverts(bScreen *screen);
void BKE_screen_remove_double_scredges(bScreen *screen);
void BKE_screen_remove_unused_scredges(bScreen *screen);
void BKE_screen_remove_unused_scrverts(bScreen *screen);

void BKE_screen_header_alignment_reset(bScreen *screen);

/* .blend file I/O */

void BKE_screen_view3d_shading_blend_write(BlendWriter *writer, View3DShading *shading);
void BKE_screen_view3d_shading_blend_read_data(BlendDataReader *reader, View3DShading *shading);

void BKE_screen_area_map_blend_write(BlendWriter *writer, ScrAreaMap *area_map);
/**
 * \return false on error.
 */
bool BKE_screen_area_map_blend_read_data(BlendDataReader *reader, ScrAreaMap *area_map);
/**
 * And as patch for 2.48 and older.
 * For the saved 2.50 files without `regiondata`.
 */
void BKE_screen_view3d_do_versions_250(View3D *v3d, ListBaseT<ARegion> *regions);

/**
 * Called after lib linking process is done, to perform some validation on the read data, or some
 * complex specific reading process that requires the data to be fully read and ID pointers to be
 * valid.
 */
void BKE_screen_area_blend_read_after_liblink(BlendLibReader *reader,
                                              ID *parent_id,
                                              ScrArea *area);
/**
 * Cannot use #IDTypeInfo callback yet, because of the return value.
 */
bool BKE_screen_blend_read_data(BlendDataReader *reader, bScreen *screen);

}  // namespace blender
