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

/** \file
 * \ingroup DNA
 */

#pragma once

#include "BLI_enum_flags.hh"

#include "DNA_asset_types.h"
#include "DNA_defs.h"
#include "DNA_listBase.h"
#include "DNA_vec_types.h"
#include "DNA_view2d_types.h"

#include "DNA_ID.h"

namespace blender {

struct ARegion;
struct ARegionType;
struct PanelType;
struct PointerRNA;
struct Scene;
struct SpaceLink;
struct SpaceType;
struct uiList;
struct uiListType;
struct wmDrawBuffer;
struct wmTimer;
struct wmTooltipState;
struct Panel_Runtime;
namespace bke {
struct ARegionRuntime;
struct FileHandlerType;
}  // namespace bke
namespace ui {
struct Layout;
}  // namespace ui

/** #bScreen.flag */
enum eScreen_Flag : short {
  SCREEN_DEPRECATED = 1,
  SCREEN_COLLAPSE_STATUSBAR = 2,
};
ENUM_OPERATORS(eScreen_Flag)

/** #bScreen.state */
enum eScreen_State : char {
  SCREENNORMAL = 0,
  /** One editor taking over the screen. */
  SCREENMAXIMIZED = 1,
  /**
   * One editor taking over the screen with no bare-minimum UI elements.
   *
   * Besides making the area full-screen this disables navigation & statistics because
   * this is part of a stereo 3D pipeline where these elements would interfere, see: !142418.
   */
  SCREENFULL = 2,
};

/** #bScreen.fullscreen_flag */
enum eScreen_Fullscreen_Flag : short {
  FULLSCREEN_RESTORE_GIZMO_NAVIGATE = (1 << 0),
  FULLSCREEN_RESTORE_TEXT = (1 << 1),
  FULLSCREEN_RESTORE_STATS = (1 << 2),
};
ENUM_OPERATORS(eScreen_Fullscreen_Flag)

/** #bScreen.redraws_flag */
enum eScreen_Redraws_Flag : short {
  TIME_REGION = (1 << 0),
  TIME_ALL_3D_WIN = (1 << 1),
  TIME_ALL_ANIM_WIN = (1 << 2),
  TIME_ALL_BUTS_WIN = (1 << 3),
  // TIME_WITH_SEQ_AUDIO    = (1 << 4), /* DEPRECATED */
  TIME_SEQ = (1 << 5),
  TIME_ALL_IMAGE_WIN = (1 << 6),
  // TIME_CONTINUE_PHYSICS  = (1 << 7), /* UNUSED */
  TIME_NODES = (1 << 8),
  TIME_CLIPS = (1 << 9),
  TIME_SPREADSHEETS = (1 << 10),

  TIME_FOLLOW = static_cast<short>(1 << 15),
};
ENUM_OPERATORS(eScreen_Redraws_Flag)

/* TODO: Doing this is quite ugly :)
 * Once the top-bar is merged bScreen should be refactored to use ScrAreaMap. */
#define AREAMAP_FROM_SCREEN(screen) ((ScrAreaMap *)&(screen)->vertbase)

struct bScreen {
#ifdef __cplusplus
  /** See #ID_Type comment for why this is here. */
  static constexpr ID_Type id_type = ID_SCR;
#endif

  ID id;

  /* TODO: Should become ScrAreaMap now.
   * NOTE: KEEP ORDER IN SYNC WITH #ScrAreaMap! (see AREAMAP_FROM_SCREEN macro above). */
  /** Screens have vertices/edges to define areas. */
  ListBaseT<struct ScrVert> vertbase = {nullptr, nullptr};
  ListBaseT<struct ScrEdge> edgebase = {nullptr, nullptr};
  ListBaseT<struct ScrArea> areabase = {nullptr, nullptr};
  /* End variables that must be in sync with #ScrAreaMap. */

  /** Screen level regions (menus), runtime only. */
  ListBaseT<ARegion> regionbase = {nullptr, nullptr};

  DNA_DEPRECATED struct Scene *scene = nullptr;

  /** General flags. */
  eScreen_Flag flag = {};
  /** Window-ID from WM, starts with 1. */
  short winid = 0;
  /** User-setting for which editors get redrawn during animation playback. */
  eScreen_Redraws_Flag redraws_flag = {};

  /** Temp screen in a temp window, don't save (like user-preferences). */
  char temp = 0;
  /** Temp screen for image render display or file-select. */
  eScreen_State state = SCREENNORMAL;
  /** Notifier for drawing edges. */
  char do_draw = 0;
  /** Notifier for scale screen, changed screen, etc. */
  char do_refresh = 0;
  /** Notifier for gesture draw. */
  char do_draw_gesture = 0;
  /** Notifier for paint cursor draw. */
  char do_draw_paintcursor = 0;
  /** Notifier for dragging draw. */
  char do_draw_drag = 0;
  /** Set to delay screen handling after switching back from maximized area. */
  char skip_handling = 0;
  /** Set when scrubbing to avoid some costly updates. */
  char scrubbing = 0;
  char _pad[1] = {};

  /** Active region that has mouse focus. */
  struct ARegion *active_region = nullptr;

  /** If set, screen has timer handler added in window. */
  struct wmTimer *animtimer = nullptr;
  /** Context callback. */
  void /*bContextDataCallback*/ *context = nullptr;

  /* Used to restore after SCREENFULL state. */
  eScreen_Fullscreen_Flag fullscreen_flag = {};
  char _pad2[6] = {};

  /** Runtime. */
  struct wmTooltipState *tool_tip = nullptr;

  PreviewImage *preview = nullptr;
};

struct ScrVert {
  struct ScrVert *next = nullptr, *prev = nullptr, *newv = nullptr;
  vec2s vec = {};
  /* first one used internally, second one for tools */
  short flag = 0, editflag = 0;
};

struct ScrEdge {
  struct ScrEdge *next = nullptr, *prev = nullptr;
  ScrVert *v1 = nullptr, *v2 = nullptr;
  /** 1 when at edge of screen. */
  short border = 0;
  short flag = 0;
  char _pad[4] = {};
};

struct ScrAreaMap {
  /* ** NOTE: KEEP ORDER IN SYNC WITH LISTBASES IN bScreen! ** */

  /** Screens have vertices/edges to define areas. */
  ListBaseT<ScrVert> vertbase = {nullptr, nullptr};
  ListBaseT<ScrEdge> edgebase = {nullptr, nullptr};
  ListBaseT<ScrArea> areabase = {nullptr, nullptr};
};

enum LayoutPanelStateFlag : uchar {
  /** If set, the panel is currently open. Otherwise it is collapsed. */
  LAYOUT_PANEL_STATE_FLAG_OPEN = (1 << 0),
};
ENUM_OPERATORS(LayoutPanelStateFlag)

/**
 * Used for passing expansion between instanced panel data and the panels themselves.
 * There are 16 defines because the expansion data is typically stored in a short.
 *
 * \note Expansion for instanced panels is stored in depth first order. For example, the value of
 * UI_SUBPANEL_DATA_EXPAND_2 correspond to mean the expansion of the second sub-panel or the first
 * sub-panel's first sub-panel.
 */
enum uiPanelDataExpansion : ushort {
  UI_PANEL_DATA_EXPAND_ROOT = (1 << 0),
  UI_SUBPANEL_DATA_EXPAND_1 = (1 << 1),
  UI_SUBPANEL_DATA_EXPAND_2 = (1 << 2),
  UI_SUBPANEL_DATA_EXPAND_3 = (1 << 3),
  UI_SUBPANEL_DATA_EXPAND_4 = (1 << 4),
  UI_SUBPANEL_DATA_EXPAND_5 = (1 << 5),
  UI_SUBPANEL_DATA_EXPAND_6 = (1 << 6),
  UI_SUBPANEL_DATA_EXPAND_7 = (1 << 7),
  UI_SUBPANEL_DATA_EXPAND_8 = (1 << 8),
  UI_SUBPANEL_DATA_EXPAND_9 = (1 << 9),
  UI_SUBPANEL_DATA_EXPAND_10 = (1 << 10),
  UI_SUBPANEL_DATA_EXPAND_11 = (1 << 11),
  UI_SUBPANEL_DATA_EXPAND_12 = (1 << 12),
  UI_SUBPANEL_DATA_EXPAND_13 = (1 << 13),
  UI_SUBPANEL_DATA_EXPAND_14 = (1 << 14),
  UI_SUBPANEL_DATA_EXPAND_15 = (1 << 15),
};
ENUM_OPERATORS(uiPanelDataExpansion)

/** #Panel.flag */
enum ePanel_Flag : short {
  PNL_SELECT = (1 << 0),
  PNL_UNUSED_1 = (1 << 1), /* Cleared */
  PNL_CLOSED = (1 << 2),
  // PNL_TABBED = (1 << 3),  /* UNUSED */
  // PNL_OVERLAP = (1 << 4), /* UNUSED */
  PNL_PIN = (1 << 5),
  PNL_POPOVER = (1 << 6),
  /** The panel has been drag-drop reordered and the instanced panel list needs to be rebuilt. */
  PNL_INSTANCED_LIST_ORDER_CHANGED = (1 << 7),
};
ENUM_OPERATORS(ePanel_Flag)

/** Fallback panel category (only for old scripts which need updating). */
#define PNL_CATEGORY_FALLBACK "Misc"

struct LayoutPanelState {
  struct LayoutPanelState *next = nullptr, *prev = nullptr;
  /** Identifier of the panel. */
  char *idname = nullptr;
  LayoutPanelStateFlag flag = {};
  char _pad[3] = {};
  /**
   * A logical time set from #layout_panel_states_clock when the panel is used by the UI. This is
   * used to detect the least-recently-used panel states when some panel states should be removed.
   */
  uint32_t last_used = 0;
};

struct Panel {
  struct Panel *next = nullptr, *prev = nullptr;

  /** Runtime. */
  struct PanelType *type = nullptr;
  /** Runtime for drawing. */
  ui::Layout *layout = nullptr;

  char panelname[/*BKE_ST_MAXNAME*/ 64] = "";
  /** Panel name is identifier for restoring location. */
  char *drawname = nullptr;
  /** Offset within the region. */
  int ofsx = 0, ofsy = 0;
  /** Panel size including children. */
  int sizex = 0, sizey = 0;
  /** Panel size excluding children. */
  int blocksizex = 0, blocksizey = 0;
  short labelofs = 0;
  ePanel_Flag flag = {};
  short runtime_flag = 0;
  char _pad[6] = {};
  /** Panels are aligned according to increasing sort-order. */
  int sortorder = 0;
  /** Runtime for panel manipulation. */
  void *activedata = nullptr;
  /** Sub panels. */
  ListBaseT<Panel> children = {nullptr, nullptr};

  /**
   *  This stores the open-close-state of layout-panels created with
   * `layout.panel(...)` in Python. For more information on layout-panels, see
   * `ui::Layout::panel_prop`.
   */
  ListBaseT<LayoutPanelState> layout_panel_states = {nullptr, nullptr};
  /**
   * This is increased whenever a layout panel state is used by the UI. This is used to allow for
   * some garbage collection of panel states when #layout_panel_states becomes large. It works by
   * removing all least-recently-used panel states up to a certain threshold.
   */
  uint32_t layout_panel_states_clock = 0;
  char _pad2[4] = {};

  struct Panel_Runtime *runtime = nullptr;
};

/**
 * Notes on Panel Categories:
 *
 * - #ARegion.panels_category (#PanelCategoryDyn)
 *   is a runtime only list of categories collected during draw.
 *
 * - #ARegion.panels_category_active (#PanelCategoryStack)
 *   is basically a list of strings (category id's).
 *
 * Clicking on a tab moves it to the front of region->panels_category_active,
 * If the context changes so this tab is no longer displayed,
 * then the first-most tab in #ARegion.panels_category_active is used.
 *
 * This way you can change modes and always have the tab you last clicked on.
 */

/* region level tabs */
#
#
struct PanelCategoryDyn {
  struct PanelCategoryDyn *next = nullptr, *prev = nullptr;
  char idname[64] = "";
  int icon = 0;
};

/** Region stack of active tabs. */
struct PanelCategoryStack {
  struct PanelCategoryStack *next = nullptr, *prev = nullptr;
  char idname[64] = "";
};

/** #uiList.layout_type */
enum euiList_LayoutType : int {
  UILST_LAYOUT_DEFAULT = 0,
  UILST_LAYOUT_COMPACT = 1,
};

/** #uiList.flag */
enum euiList_Flag : int {
  /** Scroll list to make active item visible. */
  UILST_SCROLL_TO_ACTIVE_ITEM = 1 << 0,
};
ENUM_OPERATORS(euiList_Flag)

/** Value (in number of items) we have to go below minimum shown items to enable auto size. */
#define UI_LIST_AUTO_SIZE_THRESHOLD 1

/** uiList filter flags (dyn_data)
 *
 * \warning Lower 16 bits are meant for custom use in Python, don't use them here! Only use the
 *          higher 16 bits.
 * \warning Those values are used by integer RNA too, which does not handle well values > INT_MAX.
 *          So please do not use 32nd bit here.
 */
enum euiList_Filter : int {
  /* Don't use (1 << 0) to (1 << 15) here! See warning above. */

  /* Filtering returned #UI_LIST_ITEM_NEVER_SHOW. */
  UILST_FLT_ITEM_NEVER_SHOW = (1 << 16),
  UILST_FLT_ITEM = 1 << 30, /* This item has passed the filter process successfully. */
};
ENUM_OPERATORS(euiList_Filter)

/** #uiList.filter_flag */
enum euiList_FilterFlag : int {
  UILST_FLT_SHOW = 1 << 0,            /* Show filtering UI. */
  UILST_FLT_EXCLUDE = UILST_FLT_ITEM, /* Exclude filtered items, *must* use this same value. */
};
ENUM_OPERATORS(euiList_FilterFlag)

/** #uiList.filter_sort_flag */
enum euiList_FilterSortFlag : int {
  /* Plain values (only one is valid at a time, once masked with UILST_FLT_SORT_MASK). */
  /** Just for sake of consistency. */
  /* UILST_FLT_SORT_INDEX = 0, */ /* UNUSED */
  UILST_FLT_SORT_ALPHA = 1,

  /* Bitflags affecting behavior of any kind of sorting. */
  /** Special flag to indicate that order is locked (not user-changeable). */
  UILST_FLT_SORT_LOCK = int(1u << 30),
  /** Special value, bit-flag used to reverse order! */
  UILST_FLT_SORT_REVERSE = int(1u << 31),
};
ENUM_OPERATORS(euiList_FilterSortFlag)

#define UILST_FLT_SORT_MASK (((unsigned int)(UILST_FLT_SORT_REVERSE | UILST_FLT_SORT_LOCK)) - 1)

typedef void (*uiListFreeRuntimeDataFunc)(struct uiList *ui_list);

/* uiList dynamic data... */
/* These two lines with # tell `makesdna` this struct can be excluded. */
#
#
struct uiListDyn {
  /** Callback to free UI data when freeing UI-Lists in BKE. */
  uiListFreeRuntimeDataFunc free_runtime_data_fn = {};

  /** Number of rows needed to draw all elements. */
  int height = 0;
  /** Actual visual height of the list (in rows). */
  int visual_height = 0;
  /** Minimal visual height of the list (in rows). */
  int visual_height_min = 0;

  /** Number of items in collection. */
  int items_len = 0;
  /** Number of items actually visible after filtering. */
  int items_shown = 0;

  /* Those are temp data used during drag-resize with GRIP button
   * (they are in pixels, the meaningful data is the
   * difference between resize_prev and resize)...
   */
  int resize = 0;
  int resize_prev = 0;

  /** Allocated custom data. Freed together with the #uiList (and when re-assigning). */
  void *customdata = nullptr;

  /* Filtering data. */
  /** This bit-field is effectively exposed in Python, and scripts are explicitly allowed to assign
   * any own meaning to the lower 16 ones.
   * #items_len length. */
  int *items_filter_flags = nullptr;
  /** Org_idx -> new_idx, items_len length. */
  int *items_filter_neworder = nullptr;

  struct wmOperatorType *custom_drag_optype = nullptr;
};

struct uiList { /* some list UI data need to be saved in file */
  struct uiList *next = nullptr, *prev = nullptr;

  /** Runtime. */
  struct uiListType *type = nullptr;

  char list_id[/*UI_MAX_NAME_STR*/ 256] = "";

  /** How items are laid out in the list. */
  euiList_LayoutType layout_type = UILST_LAYOUT_DEFAULT;
  euiList_Flag flag = {};

  int list_scroll = 0;
  int list_grip = 0;
  int list_last_len = 0;
  int list_last_activei = 0;

  /* Filtering data. */
  /** Defined as . */
  char filter_byname[/*UI_MAX_NAME_STR*/ 256] = "";
  euiList_FilterFlag filter_flag = {};
  euiList_FilterSortFlag filter_sort_flag = {};

  /** Custom sub-classes properties. */
  IDProperty *properties = nullptr;

  /** Dynamic data (runtime). */
  uiListDyn *dyn_data = nullptr;
};

enum uiViewStateFlag : uint16_t {
  UI_VIEW_SHOW_FILTER_OPTIONS = (1 << 0),
  UI_VIEW_SORT_ALPHA = (1 << 1),
  UI_VIEW_FILTER_INVERT = (1 << 2),
};
ENUM_OPERATORS(uiViewStateFlag)

/** See #uiViewStateLink. */
struct uiViewState {
  /**
   * User set height of the view in unscaled pixels. A value of 0 means no custom height was set
   * and the default should be used.
   */
  int custom_height = 0;
  /**
   * Amount of vertical scrolling. View types decide on the unit:
   * - Tree views: Number of items scrolled out of view (#scroll_offset of 5 means 5 items are
   *   scrolled out of view).
   */
  int scroll_offset = 0;
  uiViewStateFlag flag = {};
  char _pad[5] = {};
  uint8_t invert_sort_type = 0;
  char search_string[/*UI_MAX_NAME_STR*/ 256] = "";
};

/**
 * Persistent storage for some state of views (#ui::AbstractView), for storage in a region. The
 * view state is matched to the view using the view's idname.
 *
 * The actual state is stored in #uiViewState, so views can manage this conveniently without having
 * to care about the idname and listbase pointers themselves.
 */
struct uiViewStateLink {
  struct uiViewStateLink *next = nullptr, *prev = nullptr;

  char idname[/*BKE_ST_MAXNAME*/ 64] = "";

  uiViewState state;
};

struct TransformOrientation {
  struct TransformOrientation *next = nullptr, *prev = nullptr;
  char name[/*MAX_NAME*/ 64] = "";
  float mat[3][3] = {};
  char _pad[4] = {};
};

enum uiPreviewTag : short {
  /** Preview needs re-rendering, handled in #ED_preview_draw(). */
  UI_PREVIEW_TAG_DIRTY = (1 << 0),
};
ENUM_OPERATORS(uiPreviewTag)

/** Some preview UI data need to be saved in file. */
struct uiPreview {
  struct uiPreview *next = nullptr, *prev = nullptr;

  char preview_id[/*BKE_ST_MAXNAME*/ 64] = "";
  short height = 0;

  /* Unset on file read. */
  uiPreviewTag tag = {};

  /** #ID.session_uid of the ID this preview is made for. Unset on file read. */
  unsigned int id_session_uid = 0;
};

/**
 * State storage for text-boxes (#ui::ButtonTextBox).
 */
struct TextboxState {
  int visible_lines = 3;
  int scroll = 0;
};

/**
 * Persistent storage for text-boxes (#ui::ButtonTextBox) in a region. The state is matched to the
 * textbox buttons using the RNA struct identifier + property name.
 *
 * The actual state is stored in #uiTextboxState, so textbox buttons can manage this conveniently
 * without having to care about the idname and listbase pointers themselves.
 */
struct uiTextboxStateLink {
  struct uiTextboxStateLink *next = nullptr, *prev = nullptr;
  char *idname = nullptr;
  TextboxState state;
};

enum GlobalAreaFlag : short {
  GLOBAL_AREA_IS_HIDDEN = (1 << 0),
};
ENUM_OPERATORS(GlobalAreaFlag)

enum GlobalAreaAlign : short {
  GLOBAL_AREA_ALIGN_TOP = 0,
  GLOBAL_AREA_ALIGN_BOTTOM = 1,
};

struct ScrGlobalAreaData {
  /**
   * Global areas have a non-dynamic size. That means, changing the window size doesn't
   * affect their size at all. However, they can still be 'collapsed', by changing this value.
   * Ignores DPI (#ED_area_global_size_y and winx/winy don't).
   */
  short cur_fixed_height = 0;
  /**
   * For global areas, this is the min and max size they can use depending on
   * if they are 'collapsed' or not.
   */
  short size_min = 0, size_max = 0;
  GlobalAreaAlign align = GLOBAL_AREA_ALIGN_TOP;

  GlobalAreaFlag flag = {};
  char _pad[2] = {};
};

/** #ScrArea.flag */
enum eScrArea_Flag : short {
  HEADER_NO_PULLDOWN = (1 << 0),
//  AREA_FLAG_UNUSED_1           = (1 << 1),
//  AREA_FLAG_UNUSED_2           = (1 << 2),
#ifdef DNA_DEPRECATED_ALLOW
  AREA_TEMP_INFO = (1 << 3), /* versioned to make slot reusable */
#endif
  /** Update size of regions within the area. */
  AREA_FLAG_REGION_SIZE_UPDATE = (1 << 3),
  AREA_FLAG_ACTIVE_TOOL_UPDATE = (1 << 4),
  // AREA_FLAG_UNUSED_5 = (1 << 5),

  AREA_FLAG_UNUSED_6 = (1 << 6), /* cleared */

  /**
   * For temporary full-screens (file browser, image editor render)
   * that are opened above user set full-screens.
   */
  AREA_FLAG_STACKED_FULLSCREEN = (1 << 7),
  /** Update action zones (even if the mouse is not intersecting them). */
  AREA_FLAG_ACTIONZONES_UPDATE = (1 << 8),
  /** For off-screen areas. */
  AREA_FLAG_OFFSCREEN = (1 << 9),
};
ENUM_OPERATORS(eScrArea_Flag)

#define AREAGRID 1
#define AREAMINX 29
#define HEADER_PADDING_Y 6
#define HEADERY (20 + HEADER_PADDING_Y)

struct ScrArea_Runtime {
  struct bToolRef *tool = nullptr;
  char is_tool_set = 0;
  char _pad0[7] = {};
};

struct ScrArea {
  DNA_DEFINE_CXX_METHODS(ScrArea)

  struct ScrArea *next = nullptr, *prev = nullptr;

  /** Ordered (bottom-left, top-left, top-right, bottom-right). */
  ScrVert *v1 = nullptr, *v2 = nullptr, *v3 = nullptr, *v4 = nullptr;
  /** If area==full, this is the parent. */
  bScreen *full = nullptr;

  /** Rect bound by v1 v2 v3 v4. */
  rcti totrct = {};

  /** eSpace_Type (SPACE_FOO). */
  char spacetype = 0;
  /**
   * eSpace_Type (SPACE_FOO).
   *
   * Temporarily used while switching area type, otherwise this should be SPACE_EMPTY.
   * Also, versioning uses it to nicely replace deprecated editors.
   * It's been there for ages, name doesn't fit any more.
   */
  char butspacetype = 0;
  short butspacetype_subtype = 0;

  /** Size. */
  short winx = 0, winy = 0;

  /** OLD! 0=no header, 1= down, 2= up. */
  DNA_DEPRECATED char headertype = 0;
  /** Private, for spacetype refresh callback. */
  char do_refresh = 0;
  eScrArea_Flag flag = {};
  /**
   * Index of last used region of 'RGN_TYPE_WINDOW'
   * runtime variable, updated by executing operators.
   */
  short region_active_win = 0;
  char _pad[2] = {};

  /** Callbacks for this space type. */
  struct SpaceType *type = nullptr;

  /** Non-NULL if this area is global. */
  ScrGlobalAreaData *global = nullptr;

  float quadview_ratio[2] = {};

  /**
   * A list of space links (editors) that were open in this area before. When
   * changing the editor type, we try to reuse old editor data from this list.
   * The first item is the active/visible one.
   */
  ListBaseT<SpaceLink> spacedata = {nullptr, nullptr};
  /**
   * \note This region list is the one from the active/visible editor (first item in
   * spacedata list). Use SpaceLink.regionbase if it's inactive (but only then)!
   */
  ListBaseT<ARegion> regionbase = {nullptr, nullptr};
  ListBaseT<struct wmEventHandler> handlers = {nullptr, nullptr};

  ListBaseT<struct AZone> actionzones = {nullptr, nullptr};

  ScrArea_Runtime runtime;
};

/**
 * regiontype, first two are the default set.
 * \warning Do NOT change order, append on end. Types are hard-coded needed.
 */
enum eRegion_Type : short {
  RGN_TYPE_WINDOW = 0,
  RGN_TYPE_HEADER = 1,
  RGN_TYPE_CHANNELS = 2,
  RGN_TYPE_TEMPORARY = 3,
  RGN_TYPE_UI = 4,
  RGN_TYPE_TOOLS = 5,
  RGN_TYPE_TOOL_PROPS = 6,
  RGN_TYPE_PREVIEW = 7,
  RGN_TYPE_HUD = 8,
  /* Region to navigate the main region from (RGN_TYPE_WINDOW). */
  RGN_TYPE_NAV_BAR = 9,
  /* A place for buttons to trigger execution of something that was set up in other regions. */
  RGN_TYPE_EXECUTE = 10,
  RGN_TYPE_FOOTER = 11,
  RGN_TYPE_TOOL_HEADER = 12,
  /* Region type used exclusively by internal code and add-ons to register draw callbacks to the XR
   * context (surface, mirror view). Does not represent any real region. */
  RGN_TYPE_XR = 13,
  RGN_TYPE_ASSET_SHELF = 14,
  RGN_TYPE_ASSET_SHELF_HEADER = 15,
  RGN_TYPE_SCRUBBING = 16,

#define RGN_TYPE_NUM (RGN_TYPE_SCRUBBING + 1)
};

/** Use for function args. */
#define RGN_TYPE_ANY -1

/** Check for any kind of header region. */
#define RGN_TYPE_IS_HEADER_ANY(regiontype) \
  (((1 << (regiontype)) & \
    ((1 << RGN_TYPE_HEADER) | 1 << (RGN_TYPE_TOOL_HEADER) | (1 << RGN_TYPE_FOOTER) | \
     (1 << RGN_TYPE_ASSET_SHELF_HEADER) | (1 << RGN_TYPE_SCRUBBING))) != 0)

/** #ARegion.alignment */
enum eRegion_Alignment : short {
  RGN_ALIGN_NONE = 0,
  RGN_ALIGN_TOP = 1,
  RGN_ALIGN_BOTTOM = 2,
  RGN_ALIGN_LEFT = 3,
  RGN_ALIGN_RIGHT = 4,
  RGN_ALIGN_HSPLIT = 5,
  RGN_ALIGN_VSPLIT = 6,
  RGN_ALIGN_FLOAT = 7,
  RGN_ALIGN_QSPLIT = 8,
  /* Maximum 15. */

  /* Flags start here. */
  /** Region is split into the previous one, they share the same space along a common edge.
   * Includes the #RGN_ALIGN_HIDE_WITH_PREV behavior. */
  RGN_SPLIT_PREV = 1 << 5,
  /** Always let scaling this region scale the previous region instead. Useful to let regions
   * appear like they are one (while having independent layout, scrolling, etc.). */
  RGN_SPLIT_SCALE_PREV = 1 << 6,
  /** Whenever the previous region is hidden, this region becomes invisible too. #RGN_FLAG_HIDDEN
   * should only be set for the previous region, not this. The evaluated visibility respecting this
   * flag can be queried via #ARegion.visible */
  RGN_ALIGN_HIDE_WITH_PREV = 1 << 7,
  /** Region scaling is handed off when reaching limits. When the previous region is at maximum
   * size, drag-outs are forwarded to this region, and when this region is at minimum size,
   * drag-ins are forwarded to the previous region. */
  RGN_STACK_ON_PREV = 1 << 8,
};
ENUM_OPERATORS(eRegion_Alignment)

/** Mask out flags so we can check the alignment. */
#define RGN_ALIGN_ENUM_FROM_MASK(align) ((align) & eRegion_Alignment((1 << 4) - 1))
#define RGN_ALIGN_FLAG_FROM_MASK(align) ((align) & ~eRegion_Alignment((1 << 4) - 1))

/** #ARegion.flag */
enum eRegion_Flag : short {
  RGN_FLAG_HIDDEN = (1 << 0),
  RGN_FLAG_TOO_SMALL = (1 << 1),
  /** Enable dynamically changing the region size in the #ARegionType::layout() callback. */
  RGN_FLAG_DYNAMIC_SIZE = (1 << 2),
  /** Region data is NULL'd on read, never written. */
  RGN_FLAG_TEMP_REGIONDATA = (1 << 3),
  /** Region resizing by the user is disabled, but the region edge can still be dragged to
   * hide/unhide the region. */
  RGN_FLAG_NO_USER_RESIZE = (1 << 4),
  /** Size has been clamped (floating regions only). */
  RGN_FLAG_SIZE_CLAMP_X = (1 << 5),
  RGN_FLAG_SIZE_CLAMP_Y = (1 << 6),
  /** When the user sets the region is hidden,
   * needed for floating regions that may be hidden for other reasons. */
  RGN_FLAG_HIDDEN_BY_USER = (1 << 7),
  /** Property search filter is active. */
  RGN_FLAG_SEARCH_FILTER_ACTIVE = (1 << 8),
  /**
   * Update the expansion of the region's panels and switch contexts. Only Set
   * temporarily when the search filter is updated and cleared at the end of the
   * region's layout pass. so that expansion is still interactive,
   */
  RGN_FLAG_SEARCH_FILTER_UPDATE = (1 << 9),
  /** #ARegionType.poll() failed for the current context, and the region should be treated as if it
   * wouldn't exist. Runtime only flag. */
  RGN_FLAG_POLL_FAILED = (1 << 10),
  RGN_FLAG_RESIZE_RESPECT_BUTTON_SECTIONS = (1 << 11),
  RGN_FLAG_INDICATE_OVERFLOW = (1 << 12),
};
ENUM_OPERATORS(eRegion_Flag)

/** #ARegion.do_draw */
enum eRegion_DrawFlag : int {
  /** Region must be fully redrawn. */
  RGN_DRAW = 1,
  /**
   * Redraw only part of region, for sculpting and painting to get smoother
   * stroke painting on heavy meshes.
   */
  RGN_DRAW_PARTIAL = 2,
  /**
   * For outliner, to do faster redraw without rebuilding outliner tree.
   * For 3D viewport, to display a new progressive render sample without
   * while other buffers and overlays remain unchanged.
   */
  RGN_DRAW_NO_REBUILD = 4,

  /** Set while region is being drawn. */
  RGN_DRAWING = 8,
  /** For popups, to refresh UI layout along with drawing. */
  RGN_REFRESH_UI = 16,

  /** Only editor overlays (currently gizmos only!) should be redrawn. */
  RGN_DRAW_EDITOR_OVERLAYS = 32,
};
ENUM_OPERATORS(eRegion_DrawFlag)

struct ARegion {
  struct ARegion *next = nullptr, *prev = nullptr;

  /** 2D-View scrolling/zoom info (most regions are 2d anyways). */
  View2D v2d;
  /** Coordinates of region. */
  rcti winrct = {};
  /** Size. */
  short winx = 0, winy = 0;
  /**
   * This is a Y offset on the panel tabs that represents pixels,
   * where zero represents no scroll - the first category always shows first at the top.
   */
  int category_scroll = 0;

  /** Window, header, etc. identifier for drawing. */
  eRegion_Type regiontype = RGN_TYPE_WINDOW;
  /** How it should split. */
  eRegion_Alignment alignment = RGN_ALIGN_NONE;
  /** Hide, .... */
  eRegion_Flag flag = {};

  /** Current split size in unscaled pixels (if zero it uses regiontype).
   * To convert to pixels use: `UI_SCALE_FAC * region->sizex + 0.5f`.
   * However to get the current region size, you should usually use winx/winy from above, not this!
   */
  short sizex = 0, sizey = 0;

  /** Private, set for indicate drawing overlapped. */
  short overlap = 0;
  /** Temporary copy of flag settings for clean full-screen. */
  short flagfullscreen = 0;

  char _pad[2] = {};

  ListBaseT<Panel> panels = {nullptr, nullptr};
  /** Stack of panel categories. */
  ListBaseT<PanelCategoryStack> panels_category_active = {nullptr, nullptr};
  ListBaseT<uiList> ui_lists = {nullptr, nullptr};
  ListBaseT<uiPreview> ui_previews = {nullptr, nullptr};
  /**
   * Permanent state storage of #ui::AbstractView instances, so hiding regions with views or
   * loading files remembers the view state.
   */
  ListBaseT<uiViewStateLink> view_states = {nullptr, nullptr};
  /**
   * Permanent state storage of #ui::ButtonTextBox instances, so hiding regions with textbox
   * buttons or loading files remembers the textbox state.
   */
  ListBaseT<uiTextboxStateLink> textbox_states = {nullptr, nullptr};

  /** XXX 2.50, need spacedata equivalent? */
  void *regiondata = nullptr;

  bke::ARegionRuntime *runtime = nullptr;
};

/* #AssetShelfSettings.display_flag */
enum AssetShelfSettings_DisplayFlag : short {
  ASSETSHELF_SHOW_NAMES = (1 << 0),
};
ENUM_OPERATORS(AssetShelfSettings_DisplayFlag)

/* #AssetShelfSettings.instance_flag */
enum AssetShelf_InstanceFlag : short {
  /**
   * Remember the last known region visibility state or this shelf, so it can be restored if the
   * shelf is reactivated. Practically this makes the shelf visibility be remembered per mode.
   * Continuously updated for the visible region.
   */
  ASSETSHELF_REGION_IS_HIDDEN = (1 << 0),
};
ENUM_OPERATORS(AssetShelf_InstanceFlag);

struct AssetShelfSettings {
  AssetLibraryReference asset_library_reference;

  ListBaseT<AssetCatalogPathLink> enabled_catalog_paths = {nullptr, nullptr};
  /** If not set (null or empty string), all assets will be displayed ("All" catalog behavior). */
  const char *active_catalog_path = nullptr;

  /** For filtering assets displayed in the asset view. */
  char search_string[64] = "";

  short preview_size = 0;
  AssetShelfSettings_DisplayFlag display_flag = {};
  char _pad1[4] = {};

#if defined(__cplusplus) && !defined(DNA_NO_EXTERNAL_CONSTRUCTORS)
  /* Zero initializes. */
  AssetShelfSettings();
  /* Proper deep copy. */
  AssetShelfSettings(const AssetShelfSettings &other);
  AssetShelfSettings &operator=(const AssetShelfSettings &other);
  ~AssetShelfSettings();
#endif
};

struct AssetShelf {
  DNA_DEFINE_CXX_METHODS(AssetShelf)

  struct AssetShelf *next = nullptr, *prev = nullptr;

  /** Identifier that matches the #AssetShelfType.idname this shelf was created with. Used to
   * restore the #AssetShelf.type pointer below on file read. */
  char idname[/*MAX_NAME*/ 64] = "";
  /** Runtime. */
  struct AssetShelfType *type = nullptr;

  AssetShelfSettings settings;

  /** Only for the permanent asset shelf regions, not asset shelves in temporary popups. */
  short preferred_row_count = 0;
  AssetShelf_InstanceFlag instance_flag = {};
  char _pad[4] = {};
};

/**
 * Region-data for the main asset shelf region (#RGN_TYPE_ASSET_SHELF). Managed by the asset shelf
 * internals.
 *
 * Contains storage for all previously activated asset shelf instances plus info on the currently
 * active one (only one can be active at any time).
 */
struct RegionAssetShelf {
  /** Owning list of previously activated asset shelves. */
  ListBaseT<AssetShelf> shelves = {nullptr, nullptr};
  /**
   * The currently active shelf, if any. Updated on redraw, so that context changes are reflected.
   * Note that this may still be set even though the shelf isn't available anymore
   * (#AssetShelfType.poll() fails). The pointer isn't necessarily unset when polling.
   */
  AssetShelf *active_shelf = nullptr; /* Non-owning. */
#ifdef __cplusplus
  static RegionAssetShelf *get_from_asset_shelf_region(const ARegion &region);
  /** Creates the asset shelf region data if necessary, and returns it. */
  static RegionAssetShelf *ensure_from_asset_shelf_region(ARegion &region);
#endif
};

struct FileHandler {
  DNA_DEFINE_CXX_METHODS(FileHandler)
  /** Runtime. */
  bke::FileHandlerType *type = nullptr;
};

}  // namespace blender
