Blender V5.3
interface_textbox.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <fmt/format.h>
10
11#include "BKE_screen.hh"
12
13#include "BLF_api.hh"
14
15#include "BLI_listbase.hh"
17#include "BLI_rect.hh"
18#include "BLI_string.hh"
19
20#include "DNA_screen_types.h"
21
22#include "RNA_access.hh"
23
24#include "UI_interface_c.hh"
25
26#include "interface_intern.hh"
27#include "interface_textbox.hh"
28
29namespace blender::ui {
30
32{
33 for (Block &block : region.runtime->uiblocks) {
34 block.text_wrap_cache.clear();
35 block.markdown_layout_cache.clear();
36 for (Button &button : block.buttons()) {
37 if (button.type == ButtonType::TextBox) {
38 auto &textbox = static_cast<ButtonTextBox &>(button);
39 textbox.wrap_cache.reset();
40 textbox.placeholder_wrap_cache.reset();
41 }
42 if (button.type == ButtonType::Label) {
43 auto &label = static_cast<ButtonLabel &>(button);
44 label.wrap_cache.reset();
45 label.markdown_cache.reset();
46 }
47 }
48 }
49}
50
52{
53 textbox_wrap_lines(textbox);
54 textbox->line_scroll_set(textbox->line_scroll() + step);
55}
56
58{
59 const Vector<StringRef> lines = textbox_wrap_lines(textbox);
60 int line_cursor = 0;
61 int but_pos = textbox->pos;
62#ifdef WITH_INPUT_IME
63 /* Include the ime composition string when scrolling to the cursor. */
64 const wmIMEData *ime_data = button_ime_data_get(textbox);
65 if (ime_data && !ime_data->composite.empty() && ime_data->cursor_pos != -1) {
66 but_pos += ime_data->cursor_pos;
67 }
68#endif
69 const char *cursor = lines[0].begin() + but_pos;
70 for (const StringRef line : lines) {
71 if (line.begin() > cursor) {
72 line_cursor = std::max(0, line_cursor - 1);
73 break;
74 }
75 line_cursor++;
76 }
77 const int visible_bounds[] = {textbox->line_scroll(),
78 textbox->line_scroll() + textbox->visible_lines()};
79 if (visible_bounds[0] <= line_cursor && line_cursor < visible_bounds[1]) {
80 return;
81 }
82 if (visible_bounds[0] > line_cursor) {
83 textbox_add_scroll(textbox, line_cursor - visible_bounds[0]);
84 }
85 else {
86 textbox_add_scroll(textbox, line_cursor - visible_bounds[1] + 1);
87 }
88}
89
91 const ARegion *region,
92 const float2 xy)
93{
94
95 /* Don't include grip bounds when selecting text with the mouse. */
96 float2 start = {textbox->rect.xmin, textbox->rect.ymin};
97 float2 end = {textbox->rect.xmax, textbox->rect.ymax};
98
99 block_to_window_fl(region, textbox->block, &start.x, &start.y);
100 block_to_window_fl(region, textbox->block, &end.x, &end.y);
101
102 start.y += textbox_vertical_padding() / textbox->block->aspect;
103 end.y -= textbox_vertical_padding() / textbox->block->aspect;
104
105 const Vector<StringRef> lines = textbox_wrap_lines(textbox);
106 uiFontStyle fstyle = style_get()->widget;
107 const float aspect = textbox->block->aspect;
108 fontscale(&fstyle.points, aspect);
109 fontstyle_set(&fstyle);
110 int line_under_mouse = textbox->line_scroll() +
111 (end.y - xy.y) / (end.y - start.y) * (textbox->visible_lines());
112 line_under_mouse = std::clamp<int>(line_under_mouse,
113 textbox->line_scroll(),
114 textbox->line_scroll() + textbox->visible_lines() - 1);
115 line_under_mouse = std::clamp<int>(line_under_mouse, 0, lines.size() - 1);
116
117 const StringRef line = lines[line_under_mouse];
118
119 start.x -= U.pixelsize / aspect;
120 if (!(textbox->drawflag & BUT_NO_TEXT_PADDING)) {
121 start.x += button_text_padding(textbox);
122 }
123 const int offset = BLF_str_offset_from_cursor_position(
124 fstyle.uifont_id, line.data(), line.size(), int(xy.x - start.x));
125 int position = line.begin() - lines[0].data() + offset;
126#ifdef WITH_INPUT_IME
127 /* Text-box text wrap includes the IME composition string, remove the IME string pad from the
128 * selection. */
129 const wmIMEData *ime_data = button_ime_data_get(textbox);
130 if (ime_data && position > int(textbox->pos)) {
131 position = std::max<int>(int(textbox->pos), position - int(ime_data->composite.size()));
132 }
133#endif
134 textbox->pos = position;
135 /* Do not scroll to cursor now, wait the HandleButtonData::text_select_auto_scroll timer or the
136 * #LEFTMOUSE release event for scrolling to the cursor. */
137}
138
140{
141 const char *dest = lines.first().begin() + offset;
142 int i = 0;
143 for (const StringRef line : lines) {
144 if (line.begin() > dest) {
145 i = i - 1;
146 break;
147 }
148 i++;
149 }
150 i = std::clamp<int>(i, 0, lines.size() - 1);
151 return i;
152}
153
155 eStrCursorJumpDirection direction,
156 const bool select)
157{
158 button_update(textbox);
159 if (textbox->selend == textbox->selsta) {
160 textbox->selsta = textbox->selend = textbox->pos;
161 }
162 const Vector<StringRef> lines = textbox_wrap_lines(textbox);
163 const char *str = lines.first().begin();
164 const bool append_selection = textbox->selend == textbox->pos;
165 const int line_cursor = textbox_wrapped_line_index_from_char_offset(lines, textbox->pos);
166 uiFontStyle fstyle = style_get()->widget;
167 fontscale(&fstyle.points, textbox->block->aspect);
168 fontstyle_set(&fstyle);
169 const int fontid = fstyle.uifont_id;
170 int offset = BLF_str_offset_to_cursor(fontid,
171 lines[line_cursor].begin(),
172 lines[line_cursor].size(),
173 textbox->pos - (lines[line_cursor].begin() - str),
174 0);
175 StringRef dest_line = nullptr;
176 if (direction == STRCUR_DIR_NEXT) {
177 if (line_cursor == lines.size() - 1) {
178 textbox->pos = lines.last().end() - str;
179 }
180 else {
181 dest_line = lines[line_cursor + 1];
182 }
183 }
184 else {
185 if (line_cursor == 0) {
186 textbox->pos = 0;
187 }
188 else {
189 dest_line = lines[line_cursor - 1];
190 }
191 }
192 if (dest_line.data()) {
193 textbox->pos = dest_line.begin() - str +
195 fontid, dest_line.data(), dest_line.size(), offset);
196 }
197 if (!select) {
198 textbox->selsta = textbox->selend = textbox->pos;
199 return;
200 }
201
202 if (append_selection) {
203 textbox->selend = textbox->pos;
204 }
205 else {
206 textbox->selsta = textbox->pos;
207 }
208 if (textbox->selend < textbox->selsta) {
209 std::swap(textbox->selend, textbox->selsta);
210 }
211}
212
214{
215 uiFontStyle fstyle = style_get()->widget;
216 const float aspect = textbox->block->aspect;
217 const int width = std::max<int>(std::ceil(BLI_rctf_size_x(&textbox->rect) -
218 2.0f * UI_TEXT_MARGIN_X * float(U.widget_unit) - 2.0f),
219 0) /
220 aspect;
221 StringRef text = textbox->drawstr;
222#ifdef WITH_INPUT_IME
223 std::string ime_text;
224 const wmIMEData *ime_data = button_ime_data_get(textbox);
225 if (ime_data && !ime_data->composite.empty()) {
226 StringRef edit_str = textbox->editstr;
227 StringRef l = edit_str.is_empty() ? StringRef("") : edit_str.substr(0, textbox->pos);
228 StringRef r = edit_str.is_empty() ? StringRef("") : edit_str.substr(textbox->pos);
229 ime_text = fmt::format("{}{}{}", l, ime_data->composite, r);
230 text = ime_text;
231 }
232 else
233#endif
234 if (textbox->editstr)
235 {
236 text = textbox->editstr;
237 }
238 if (!textbox->wrap_cache) {
239 textbox->wrap_cache = std::make_unique<TextWrapCache>();
240 }
241 TextWrapCache &cache = *textbox->wrap_cache;
242 if (cache.aspect == aspect && cache.wrap_width == width && text == cache.text) {
243 textbox->last_total_lines = cache.wrapped_lines.size();
244 return cache.wrapped_lines;
245 }
246 cache.text = text;
247 text = cache.text;
248 cache.wrap_width = width;
249 cache.aspect = aspect;
250
251 fontscale(&fstyle.points, aspect);
252 fontstyle_set(&fstyle);
255 if (lines.is_empty()) {
256 lines.append(text);
257 }
258 /* Add empty trailing line to put cursor in a new line. */
259 if (text.endswith("\n")) {
260 lines.append(StringRef(text.end(), text.end()));
261 }
262 textbox->last_total_lines = lines.size();
263
264 /* WORKAROUND: Text-box event handling and drawing requires lines to not include line breaks,
265 * but sometimes text wrap adds them and other times not. */
266 for (int i : lines.index_range()) {
267 if (lines[i].endswith("\n")) {
268 lines[i] = lines[i].drop_suffix(1);
269 }
270 }
271
272 cache.wrapped_lines = lines;
273
274 return lines;
275}
276
278{
279 BLI_assert(textbox->placeholder);
280 uiFontStyle fstyle = style_get()->widget;
281 const float aspect = textbox->block->aspect;
282 const int width = std::max<int>(std::ceil(BLI_rctf_size_x(&textbox->rect) -
283 2.0f * UI_TEXT_MARGIN_X * float(U.widget_unit) - 2.0f),
284 0) /
285 aspect;
286 StringRef text = textbox->placeholder;
287
288 if (!textbox->placeholder_wrap_cache) {
289 textbox->placeholder_wrap_cache = std::make_unique<TextWrapCache>();
290 }
291 TextWrapCache &cache = *textbox->placeholder_wrap_cache;
292 if (cache.aspect == aspect && cache.wrap_width == width && text == cache.text) {
293 return cache.wrapped_lines;
294 }
295 cache.text = text;
296 cache.wrap_width = width;
297 cache.aspect = aspect;
298
299 fontscale(&fstyle.points, aspect);
300 fontstyle_set(&fstyle);
301
304
305 return cache.wrapped_lines;
306}
307
309{
310 return UI_UNIT_Y * 0.55f;
311}
312
314{
315 this->state->scroll = line_scroll;
316 /* Clamp stored value. */
317 this->state->scroll = this->line_scroll();
318}
319
321{
322 /* Allow aligning text buttons with single line text-box buttons. */
324}
325
327 StringRefNull idname,
328 const int initial_visible_lines)
329{
330 for (uiTextboxStateLink &link : region->textbox_states) {
331 if (link.idname == idname) {
332 return &link.state;
333 }
334 }
335 uiTextboxStateLink *link = MEM_new<uiTextboxStateLink>(__func__);
336 link->idname = BLI_strdupn(idname.data(), idname.size());
337 link->state.visible_lines = std::max(initial_visible_lines, textbox_minimum_visible_lines);
338 BLI_addtail(&region->textbox_states, link);
339 return &link->state;
340}
341
343{
344 const int max_scroll = std::max(this->last_total_lines - this->visible_lines(), 0);
345 return std::clamp(this->state->scroll, 0, max_scroll);
346}
347
349{
350 return std::max<int>(this->state->visible_lines, textbox_minimum_visible_lines);
351}
352
354{
355 const int visible_lines = std::max(state.visible_lines, textbox_minimum_visible_lines);
356 const float line_height = fontstyle_height_max(UI_FSTYLE_WIDGET);
357 return std::max<int>(
358 UI_UNIT_Y, int(std::round(line_height * visible_lines + textbox_vertical_padding() * 2.0f)));
359}
360
362 PointerRNA *ptr,
363 StringRefNull propname,
365 const int x,
366 const int y,
367 const short width)
368{
370
372 if (!prop) {
374 "string property not found: %s.%s", RNA_struct_identifier(ptr->type), propname.c_str());
375 return nullptr;
376 }
377
378 state->visible_lines = std::max(state->visible_lines, textbox_minimum_visible_lines);
379
380 Button *but = uiDefButR_prop(block,
383 x,
384 y,
385 width,
387 ptr,
388 prop,
389 0,
390 0,
391 0,
392 std::nullopt);
393
394 ButtonTextBox *textbox = static_cast<ButtonTextBox *>(but);
395 textbox->state = state;
396
399 }
400
401 return but;
402}
403
404} // namespace blender::ui
#define RNA_warning(format,...)
#define UI_UNIT_Y
#define UI_FSTYLE_WIDGET
constexpr const char * c_str() const
constexpr StringRef()
constexpr const char * end() const
constexpr const char * begin() const
constexpr bool is_empty() const
constexpr int64_t size() const
constexpr StringRef substr(int64_t start, int64_t size) const
constexpr const char * data() const
constexpr bool endswith(StringRef suffix) const
#define str(s)
#define position
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
#define UI_TEXT_MARGIN_X
const int state
void textbox_add_scroll(ButtonTextBox *textbox, int step)
int textbox_but_height(const TextboxState &state)
Vector< StringRef > textbox_wrap_lines(ButtonTextBox *textbox)
const uiStyle * style_get()
Vector< StringRef > textbox_wrap_placeholder(ButtonTextBox *textbox)
void block_to_window_fl(const ARegion *region, const Block *block, float *x, float *y)
Definition interface.cc:144
void fontstyle_set(const uiFontStyle *fs)
void invalidate_text_wrap_cache(const ARegion &region)
void fontscale(float *points, float aspect)
void textbox_textedit_set_cursor_pos(ButtonTextBox *textbox, const ARegion *region, const float2 xy)
void button_flag_enable(Button *but, int64_t flag)
Button * uiDefButR_prop(Block *block, ButtonType type, std::optional< StringRef > str, int x, int y, short width, short height, PointerRNA *ptr, PropertyRNA *prop, int index, float min, float max, std::optional< StringRef > tip)
constexpr int textbox_minimum_visible_lines
int textbox_wrapped_line_index_from_char_offset(Span< StringRef > lines, int offset)
Button * uiDefButTextBoxR(Block *block, PointerRNA *ptr, StringRefNull propname, TextboxState *state, int x, int y, short width)
int fontstyle_height_max(const uiFontStyle *fs)
int button_text_padding(const Button *button)
float textbox_grip_height()
TextboxState * textbox_ensure_state(ARegion *region, StringRefNull idname, const int initial_visible_lines)
float textbox_vertical_padding()
void button_update(Button *but)
void textbox_scroll_to_cursor(ButtonTextBox *textbox)
void textbox_jump_line(ButtonTextBox *textbox, eStrCursorJumpDirection direction, const bool select)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
ATTR_WARN_UNUSED_RESULT const BMLoop * l
PropertyRNA * RNA_struct_find_property_check(PointerRNA &props, const char *name, const PropertyType property_type_check)
char * BLI_strdupn(const char *str, size_t len) ATTR_MALLOC ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
VecBase< float, 2 > float2
@ PROP_STRING
Definition RNA_types.hh:213
int RNA_property_flag(PropertyRNA *prop)
Vector< StringRef > BLF_string_wrap(int fontid, StringRef str, const int max_pixel_width, BLFWrapMode mode=BLFWrapMode::Minimal)
Definition blf.cc:1103
const char * RNA_struct_identifier(const StructRNA *type)
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
iter begin(iter)
bool int BLF_str_offset_to_cursor(int fontid, const char *str, size_t str_len, size_t str_offset, int cursor_width)
Definition blf.cc:738
@ PROP_TEXTEDIT_UPDATE
Definition RNA_types.hh:396
size_t BLF_str_offset_from_cursor_position(int fontid, const char *str, size_t str_len, int location_x) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2)
Definition blf.cc:713
const char * RNA_property_ui_name(const PropertyRNA *prop, const PointerRNA *ptr)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
bke::ARegionRuntime * runtime
ListBaseT< uiTextboxStateLink > textbox_states
StructRNA * type
Definition RNA_types.hh:63
ListBaseT< ui::Block > uiblocks
Vector< std::shared_ptr< MarkdownLayoutCache > > markdown_layout_cache
Vector< std::shared_ptr< TextWrapCache > > text_wrap_cache
std::ranges::transform_view< std::ranges::ref_view< const Vector< std::unique_ptr< Button > > >, Button &(*)(const std::unique_ptr< Button > &)> buttons() const
std::shared_ptr< TextWrapCache > wrap_cache
void line_scroll_set(int line_scroll)
std::unique_ptr< TextWrapCache > placeholder_wrap_cache
std::unique_ptr< TextWrapCache > wrap_cache
Vector< StringRef > wrapped_lines
int size() const
Definition vector.hh:130
uiFontStyle widget