Blender V5.3
interface_ops_color.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025-2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "interface_intern.hh"
10
11#include <optional>
12#include <string>
13
14#include "BLI_set.hh"
15#include "BLI_string.hh"
16#include "BLI_vector.hh"
17
18#include "BKE_context.hh"
19#include "BKE_screen.hh"
20
21#include "RNA_access.hh"
22
23#include "WM_api.hh"
24#include "WM_types.hh"
25
27
28#include "BLT_translation.hh"
29
30#include "OCIO_colorspace.hh"
31#include "OCIO_config.hh"
32
33namespace blender::ui {
34
35/* -------------------------------------------------------------------- */
38
39const char *COLORSPACE_MENU_ID = "UI_MT_color_space_select";
40
41static std::optional<StringRefNull> colorspace_rna_details_from_context(const bContext *C,
42 PointerRNA *ptr,
43 PropertyRNA **prop)
44{
45 std::optional<StringRefNull> parent_path = CTX_data_string_get(C, "colorspace_parent_path");
46 if (!parent_path) {
47 /* This is the first time called, so we use the active button to get the RNA properties */
48 int unused_index = 0;
49 context_active_but_prop_get(C, ptr, prop, &unused_index);
50 return std::nullopt;
51 }
52
53 /* Use the stored properties in the bContext */
54 std::optional<StringRefNull> prop_name = CTX_data_string_get(C, "colorspace_pointer_prop");
55 *ptr = CTX_data_pointer_get(C, "colorspace_pointer_rna");
56 *prop = RNA_struct_find_property(ptr, prop_name->c_str());
57 return parent_path;
58}
59
61 PointerRNA ptr,
62 PropertyRNA *prop,
63 const char *identifier,
64 const char *name,
65 const char *description)
66{
67 Block *block = layout.block();
68 Button *but = uiDefIconTextBut(block,
70 ICON_BLANK1,
71 name,
72 0,
73 0,
74 layout.width(),
76 nullptr,
77 std::nullopt);
78
79 const std::string prop_name = RNA_property_identifier(prop);
81
82 button_func_set(but, [ptr, prop_name, index](bContext &C) mutable {
83 PropertyRNA *prop = RNA_struct_find_property(&ptr, prop_name.c_str());
84 if (prop) {
85 RNA_property_enum_set(&ptr, prop, index);
86 RNA_property_update(&C, &ptr, prop);
87 }
88 });
89
90 if (description && description[0]) {
92 but, [tip = std::string(description)](const Button *) { return tip; });
93 }
94}
95
96static void colorspaces_menu_draw(const bContext *C, Menu *menu)
97{
98 /* Get access to the RNA object who we are drawing */
99 PointerRNA ptr;
100 PropertyRNA *prop = nullptr;
101 std::optional<StringRefNull> parent_path = colorspace_rna_details_from_context(C, &ptr, &prop);
102
103 /* In case menu is invoked from invalid context. */
104 if (!ptr || prop == nullptr) {
105 return;
106 }
107
108 Set<StringRef> subdirs_set;
109 Vector<const ColorSpace *> colorspaces_at_this_level;
110
111 /* Loop through color spaces in the config. */
113 for (const int colorspace_index : IndexRange(config.get_num_active_color_spaces())) {
114 const ColorSpace *colorspace = config.get_sorted_color_space_by_index(colorspace_index);
115 if (!colorspace) {
116 continue;
117 }
118 const char *family_cstr = colorspace->family().c_str();
119 StringRef remaining(family_cstr ? family_cstr : "");
120
121 if (parent_path) {
122 if (!remaining.startswith(*parent_path)) {
123 continue;
124 }
125 /* Strip the parent_path prefix. */
126 remaining = remaining.drop_known_prefix(*parent_path);
127 }
128
129 if (remaining.is_empty()) {
130 colorspaces_at_this_level.append(colorspace);
131 continue;
132 }
133
134 /* Skip sibling items with similar prefix. */
135 int64_t pos = remaining.find_first_not_of('/');
136 if (pos == 0) {
137 if (parent_path) {
138 continue;
139 }
140 }
141 else if (pos != StringRef::not_found) {
142 /* New subdir, so erase leading slash. */
143 remaining = remaining.drop_prefix(pos);
144 }
145
146 /* Found a new subdir. Remove any paths further down and add it. */
147 pos = remaining.find_first_of('/');
148 if (pos != StringRef::not_found) {
149 remaining = remaining.substr(0, pos);
150 }
151 if (!remaining.is_empty()) {
152 subdirs_set.add(remaining);
153 }
154 }
155
156 Vector<StringRef> subdirs(subdirs_set.begin(), subdirs_set.end());
157 std::ranges::sort(subdirs);
158
159 std::ranges::sort(colorspaces_at_this_level, [](const ColorSpace *a, const ColorSpace *b) {
162 });
163
164 Layout &col = menu->layout->column(false);
165
166 /* Set context variables so they can be re-used in submenus. */
167 const std::string root = (parent_path) ? std::string(parent_path->c_str()) + "/" : "";
168 col.context_string_set("colorspace_parent_path", root);
169 col.context_ptr_set("colorspace_pointer_rna", &ptr);
170 col.context_string_set("colorspace_pointer_prop", RNA_property_identifier(prop));
171
172 for (const StringRef &dir : subdirs) {
173 std::string path = root + std::string(dir);
174 col.context_string_set("colorspace_parent_path", path);
176 }
177
178 if (!subdirs.is_empty() && !colorspaces_at_this_level.is_empty()) {
179 col.separator();
180 }
181
182 for (const ColorSpace *cs : colorspaces_at_this_level) {
184 col,
185 ptr,
186 prop,
190 }
191
192 if (!parent_path) {
194 ptr,
195 prop,
196 "scene_linear",
198 TIP_("Working color space of the current file"));
199 }
200}
201
210
212
213} // namespace blender::ui
#define BLT_I18NCONTEXT_COLOR_MANAGEMENT
#define TIP_(msgid)
#define CTX_IFACE_(context, msgid)
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
constexpr IndexRange()=default
Iterator begin() const
Definition BLI_set.hh:499
Iterator end() const
Definition BLI_set.hh:509
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr StringRef drop_prefix(int64_t n) const
constexpr StringRef drop_known_prefix(StringRef prefix) const
constexpr StringRef()
constexpr int64_t find_first_of(StringRef chars, int64_t pos=0) const
constexpr bool startswith(StringRef prefix) const
static constexpr int64_t not_found
constexpr bool is_empty() const
constexpr int64_t find_first_not_of(StringRef chars, int64_t pos=0) const
constexpr StringRef substr(int64_t start, int64_t size) const
void append(const T &value)
virtual const ColorSpace * get_sorted_color_space_by_index(int index) const =0
virtual int get_num_active_color_spaces() const =0
Button * uiDefIconTextBut(Block *block, ButtonTypeWithPointerType but_and_ptr_type, int icon, StringRef str, int x, int y, short width, short height, void *poin, std::optional< StringRef > tip)
void button_func_set(Button *but, std::function< void(bContext &)> func)
static void colorspaces_menu_draw(const bContext *C, Menu *menu)
void button_func_quick_tooltip_set(Button *but, std::function< std::string(const Button *but)> func)
static void colorspaces_menu_add_button(ui::Layout &layout, PointerRNA ptr, PropertyRNA *prop, const char *identifier, const char *name, const char *description)
static std::optional< StringRefNull > colorspace_rna_details_from_context(const bContext *C, PointerRNA *ptr, PropertyRNA **prop)
const char * COLORSPACE_MENU_ID
MenuType * UI_MT_color_space_select()
Button * context_active_but_prop_get(const bContext *C, PointerRNA *r_ptr, PropertyRNA **r_prop, int *r_index)
__int64 int64_t
Definition BKE_text.h:90
char * STRNCPY(char(&dst)[N], const char *src)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
int IMB_colormanagement_colorspace_get_named_index(const char *name)
const char * RNA_property_identifier(const PropertyRNA *prop)
std::optional< StringRefNull > CTX_data_string_get(const bContext *C, const char *member)
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
PointerRNA CTX_data_pointer_get(const bContext *C, const char *member)
const char * IMB_colormanagement_colorspace_get_name(const ColorSpace *colorspace)
ocio::Config ColorManagedConfig
ColorManagedConfig & IMB_colormanagement_get_config()
void RNA_property_enum_set(PointerRNA *ptr, PropertyRNA *prop, int value)
const char * IMB_colormanagement_colorspace_get_description(const ColorSpace *colorspace)
ocio::ColorSpace ColorSpace
Definition BLF_api.hh:42
char idname[BKE_ST_MAXNAME]
MenuTypeFlag flag
void(*) draw(const bContext *C, Menu *menu)
ui::Layout * layout
Layout & column(bool align)
void context_string_set(StringRef name, StringRef value)