Blender V5.3
interface_region_search.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "MEM_guardedalloc.h"
12
13#include <cstdarg>
14#include <cstdlib>
15#include <cstring>
16
17#include "DNA_userdef_types.h"
18
19#include "BLI_listbase.hh"
20#include "BLI_math_base_c.hh"
21#include "BLI_rect.hh"
22#include "BLI_string.hh"
23#include "BLI_string_utf8.hh"
24#include "BLI_task.hh"
25#include "BLI_utildefines.hh"
26
27#include "BKE_context.hh"
28#include "BKE_screen.hh"
29
30#include "WM_api.hh"
31#include "WM_types.hh"
32
33#include "RNA_access.hh"
34
35#include "UI_interface_icons.hh"
36#include "UI_view2d.hh"
37
38#include "BLT_translation.hh"
39
40#include "ED_screen.hh"
41
42#include "BLF_api.hh"
43
44#include "GPU_state.hh"
45#include "interface_intern.hh"
47
48namespace blender::ui {
49
50/* -------------------------------------------------------------------- */
53
56
57 int offset, offset_i; /* offset for inserting in array */
58 int more; /* flag indicating there are more items */
59
60 char **names;
61 void **pointers;
62 int *icons;
65
68
70 void *active;
71};
72
101
102#define SEARCH_ITEMS 10
103
105 const StringRef name,
106 void *poin,
107 int iconid,
108 const int64_t but_flag,
109 const uint8_t name_prefix_offset)
110{
111 /* hijack for autocomplete */
112 if (items->autocpl) {
113 autocomplete_update_name(items->autocpl, name.drop_prefix(name_prefix_offset));
114 return true;
115 }
116
117 if (iconid) {
118 items->has_icon = true;
119 }
120
121 /* hijack for finding active item */
122 if (items->active) {
123 if (poin == items->active) {
124 items->offset_i = items->totitem;
125 }
126 items->totitem++;
127 return true;
128 }
129
130 if (items->totitem >= items->maxitem) {
131 items->more = 1;
132 return false;
133 }
134
135 /* skip first items in list */
136 if (items->offset_i > 0) {
137 items->offset_i--;
138 return true;
139 }
140
141 if (items->names) {
142 name.copy_utf8_truncated(items->names[items->totitem], items->maxstrlen);
143 }
144 if (items->pointers) {
145 items->pointers[items->totitem] = poin;
146 }
147 if (items->icons) {
148 items->icons[items->totitem] = iconid;
149 }
150
151 if (name_prefix_offset != 0) {
152 /* Lazy initialize, as this isn't used often. */
153 if (items->name_prefix_offsets == nullptr) {
155 }
156 items->name_prefix_offsets[items->totitem] = name_prefix_offset;
157 }
158
159 /* Limit flags that can be set so flags such as 'UI_SELECT' aren't accidentally set
160 * which will cause problems, add others as needed. */
162 if (items->but_flags) {
163 items->but_flags[items->totitem] = but_flag;
164 }
165
166 items->totitem++;
167
168 return true;
169}
170
172{
174}
175
177{
178 return 12 * UI_UNIT_X;
179}
180
182{
183 /* Compute the width of each item. */
184 Array<int> item_widths(items.totitem);
185 threading::parallel_for(item_widths.index_range(), 256, [&](const IndexRange range) {
186 for (const int i : range) {
187 const StringRefNull name = items.names[i];
188 const int icon = items.icons ? items.icons[i] : ICON_NONE;
189 const float text_width = BLF_width(BLF_default(), name.c_str(), name.size(), nullptr);
190 const float icon_with_padding = icon == ICON_NONE ? 0.0f : UI_ICON_SIZE + UI_UNIT_X;
191 const float padding = UI_UNIT_X;
192 item_widths[i] = int(text_width + padding + icon_with_padding);
193 }
194 });
195
196 /* Compute the final width of the search box. */
197 int box_width = searchbox_size_x();
198 for (const int width : item_widths) {
199 box_width = std::max(box_width, width);
200 }
201 /* Avoid extremely wide boxes. */
202 box_width = std::min(box_width, searchbox_size_x() * 5);
203 return box_width;
204}
205
206int searchbox_size_x_guess(const bContext *C, const ButtonSearchUpdateFn update_fn, void *arg)
207{
208 SearchItems items{};
209 /* Upper bound on the number of item names that are checked. */
210 items.maxitem = 1000;
211 items.maxstrlen = 256;
212
213 /* Prepare name buffers. */
214 Array<char> names_buffer(items.maxitem * items.maxstrlen);
215 Array<char *> names(items.maxitem);
216 Array<int> icons(items.maxitem);
217 items.names = names.data();
218 items.icons = icons.data();
219 for (int i : IndexRange(items.maxitem)) {
220 names[i] = names_buffer.data() + i * items.maxstrlen;
221 }
222
223 /* Gather the items shown in the search box. */
224 update_fn(C, arg, "", &items, true);
225
226 /* This is lazy-initialized in #search_item_add. */
227 MEM_SAFE_DELETE(items.name_prefix_offsets);
228
229 return searchbox_size_x_from_items(items);
230}
231
232int search_items_find_index(const SearchItems *items, const char *name)
233{
234 if (items->name_prefix_offsets != nullptr) {
235 for (int i = 0; i < items->totitem; i++) {
236 if (STREQ(name, items->names[i] + items->name_prefix_offsets[i])) {
237 return i;
238 }
239 }
240 }
241 else {
242 for (int i = 0; i < items->totitem; i++) {
243 if (STREQ(name, items->names[i])) {
244 return i;
245 }
246 }
247 }
248 return -1;
249}
250
251/* region is the search box itself */
252static void searchbox_select(bContext *C, ARegion *region, Button *but, int step)
253{
254 uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
255
256 /* apply step */
257 data->active += step;
258
259 if (data->items.totitem == 0) {
260 data->active = -1;
261 }
262 else if (data->active >= data->items.totitem) {
263 if (data->items.more) {
264 data->items.offset++;
265 data->active = data->items.totitem - 1;
266 searchbox_update(C, region, but, false);
267 }
268 else {
269 data->active = data->items.totitem - 1;
270 }
271 }
272 else if (data->active < 0) {
273 if (data->items.offset) {
274 data->items.offset--;
275 data->active = 0;
276 searchbox_update(C, region, but, false);
277 }
278 else {
279 /* only let users step into an 'unset' state for unlink buttons */
280 data->active = (but->flag & BUT_VALUE_CLEAR) ? -1 : 0;
281 }
282 }
283
284 ED_region_tag_redraw(region);
285}
286
287static void searchbox_butrect(rcti *r_rect, uiSearchboxData *data, int itemnr)
288{
289 const float tria_h = data->zoom * UI_SEARCHBOX_TRIA_H;
290
291 /* thumbnail preview */
292 if (data->preview) {
293 const int butw = BLI_rcti_size_x(&data->bbox) / data->prv_cols;
294 const int buth = (BLI_rcti_size_y(&data->bbox) - 2.0f * tria_h) / data->prv_rows;
295 int row, col;
296
297 *r_rect = data->bbox;
298
299 col = itemnr % data->prv_cols;
300 row = itemnr / data->prv_cols;
301
302 r_rect->xmin += col * butw;
303 r_rect->xmax = r_rect->xmin + butw;
304
305 r_rect->ymax -= tria_h + row * buth;
306 r_rect->ymin = r_rect->ymax - buth;
307 }
308 /* list view */
309 else {
310 const float buth = (BLI_rcti_size_y(&data->bbox) - 2.0f * tria_h) / SEARCH_ITEMS;
311
312 *r_rect = data->bbox;
313
314 r_rect->xmin = data->bbox.xmin;
315 r_rect->xmax = data->bbox.xmax;
316
317 r_rect->ymax = data->bbox.ymax - tria_h - itemnr * buth;
318 r_rect->ymin = r_rect->ymax - buth;
319 }
320}
321
322int searchbox_find_index(ARegion *region, const char *name)
323{
324 uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
325 return search_items_find_index(&data->items, name);
326}
327
328bool searchbox_inside(ARegion *region, const int xy[2])
329{
330 uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
331
332 return BLI_rcti_isect_pt(&data->bbox, xy[0] - region->winrct.xmin, xy[1] - region->winrct.ymin);
333}
334
335bool searchbox_apply(Button *but, ARegion *region)
336{
337 uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
338 ButtonSearch *search_but = static_cast<ButtonSearch *>(but);
339
341
342 search_but->item_active = nullptr;
343
344 if (data->active != -1) {
345 const char *name = data->items.names[data->active] +
346 /* Never include the prefix in the button. */
347 (data->items.name_prefix_offsets ?
348 data->items.name_prefix_offsets[data->active] :
349 0);
350
351 const char *name_sep = data->use_shortcut_sep ? strrchr(name, UI_SEP_CHAR) : nullptr;
352
353 /* Search button with dynamic string properties may have their own method of applying
354 * the search results, so only copy the result if there is a proper space for it. */
355 if (but->hardmax != 0) {
356 BLI_strncpy(but->editstr, name, name_sep ? (name_sep - name) + 1 : data->items.maxstrlen);
357 }
358
359 search_but->item_active = data->items.pointers[data->active];
360 MEM_SAFE_DELETE(search_but->item_active_str);
361 search_but->item_active_str = BLI_strdup(data->items.names[data->active]);
362
363 return true;
364 }
365 return false;
366}
367
369 bContext *C, ARegion *region, int * /*r_pass*/, double * /*pass_delay*/, bool *r_exit_on_event)
370{
371 *r_exit_on_event = true;
372
373 for (Block &block : region->runtime->uiblocks) {
374 for (Button &but : block.buttons()) {
375 if (but.type != ButtonType::SearchMenu) {
376 continue;
377 }
378
379 ButtonSearch *search_but = static_cast<ButtonSearch *>(&but);
380 if (!search_but->item_tooltip_fn) {
381 continue;
382 }
383
384 ARegion *searchbox_region = region_searchbox_region_get(region);
385 uiSearchboxData *data = static_cast<uiSearchboxData *>(searchbox_region->regiondata);
386
387 BLI_assert(data->items.pointers[data->active] == search_but->item_active);
388
389 rcti rect;
390 searchbox_butrect(&rect, data, data->active);
391
392 return search_but->item_tooltip_fn(
393 C, region, &rect, search_but->arg.get(), search_but->item_active);
394 }
395 }
396 return nullptr;
397}
398
400 bContext *C, ARegion *region, Button *but, ARegion *butregion, const wmEvent *event)
401{
402 uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
403 ButtonSearch *search_but = static_cast<ButtonSearch *>(but);
404 int type = event->type, val = event->val;
405 bool handled = false;
406 bool tooltip_timer_started = false;
407
409
410 if (type == MOUSEPAN) {
411 pan_to_scroll(event, &type, &val);
412 }
413
414 switch (type) {
415 case WHEELUPMOUSE:
416 case EVT_UPARROWKEY:
417 searchbox_select(C, region, but, -1);
418 handled = true;
419 break;
420 case WHEELDOWNMOUSE:
421 case EVT_DOWNARROWKEY:
422 searchbox_select(C, region, but, 1);
423 handled = true;
424 break;
425 case RIGHTMOUSE:
426 if (val) {
427 if (search_but->item_context_menu_fn) {
428 if (data->active != -1) {
429 /* Check the cursor is over the active element
430 * (a little confusing if this isn't the case, although it does work). */
431 rcti rect;
432 searchbox_butrect(&rect, data, data->active);
434 &rect, event->xy[0] - region->winrct.xmin, event->xy[1] - region->winrct.ymin))
435 {
436
437 void *active = data->items.pointers[data->active];
438 if (search_but->item_context_menu_fn(C, search_but->arg.get(), active, event)) {
439 handled = true;
440 }
441 }
442 }
443 }
444 }
445 break;
446 case MOUSEMOVE: {
447 /* Ignore the mouse event, in case the search popup is created underneath the cursor.
448 * We always want the first result to be selected by default. See: #144168 */
449 if (event->xy[0] == event->prev_xy[0] && event->xy[1] == event->prev_xy[1]) {
450 searchbox_select(C, region, but, 0);
451 handled = true;
452 break;
453 }
454
455 bool is_inside = false;
456
457 if (BLI_rcti_isect_pt(&region->winrct, event->xy[0], event->xy[1])) {
458 rcti rect;
459 int a;
460
461 for (a = 0; a < data->items.totitem; a++) {
462 searchbox_butrect(&rect, data, a);
464 &rect, event->xy[0] - region->winrct.xmin, event->xy[1] - region->winrct.ymin))
465 {
466 is_inside = true;
467 if (data->active != a) {
468 data->active = a;
469 searchbox_select(C, region, but, 0);
470 handled = true;
471 break;
472 }
473 }
474 }
475 }
476
477 if (U.flag & USER_TOOLTIPS) {
478 if (is_inside) {
479 if (data->active != -1) {
480 ScrArea *area = CTX_wm_area(C);
481 search_but->item_active = data->items.pointers[data->active];
483 tooltip_timer_started = true;
484 }
485 }
486 }
487
488 break;
489 }
490 }
491
492 if (handled && (tooltip_timer_started == false)) {
493 wmWindow *win = CTX_wm_window(C);
494 WM_tooltip_clear(C, win);
495 }
496
497 return handled;
498}
499
502 ButtonSearch *but,
503 const char *str,
504 SearchItems *items)
505{
506 /* While the button is in text editing mode (searchbox open), remove tooltips on every update. */
507 if (but->editstr) {
508 wmWindow *win = CTX_wm_window(C);
509 WM_tooltip_clear(C, win);
510 }
511 const bool is_first_search = !but->changed;
512 but->items_update_fn(C, but->arg.get(), str, items, is_first_search);
513}
514
515void searchbox_update(bContext *C, ARegion *region, Button *but, const bool reset)
516{
517 ButtonSearch *search_but = static_cast<ButtonSearch *>(but);
518 uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
519
521
522 /* reset vars */
523 data->items.totitem = 0;
524 data->items.more = 0;
525 if (!reset) {
526 data->items.offset_i = data->items.offset;
527 }
528 else {
529 data->items.offset_i = data->items.offset = 0;
530 data->active = -1;
531
532 /* On init, find and center active item. */
533 const bool is_first_search = !but->changed;
534 if (is_first_search && search_but->items_update_fn && search_but->item_active) {
535 data->items.active = search_but->item_active;
536 searchbox_update_fn(C, search_but, but->editstr, &data->items);
537 data->items.active = nullptr;
538
539 /* found active item, calculate real offset by centering it */
540 if (data->items.totitem) {
541 /* first case, begin of list */
542 if (data->items.offset_i < data->items.maxitem) {
543 data->active = data->items.offset_i;
544 data->items.offset_i = 0;
545 }
546 else {
547 /* second case, end of list */
548 if (data->items.totitem - data->items.offset_i <= data->items.maxitem) {
549 data->active = data->items.offset_i - data->items.totitem + data->items.maxitem;
550 data->items.offset_i = data->items.totitem - data->items.maxitem;
551 }
552 else {
553 /* center active item */
554 data->items.offset_i -= data->items.maxitem / 2;
555 data->active = data->items.maxitem / 2;
556 }
557 }
558 }
559 data->items.offset = data->items.offset_i;
560 data->items.totitem = 0;
561 }
562 }
563
564 /* callback */
565 if (search_but->items_update_fn) {
566 searchbox_update_fn(C, search_but, but->editstr, &data->items);
567 }
568
569 /* handle case where editstr is equal to one of items */
570 if (reset && data->active == -1) {
571 for (int a = 0; a < data->items.totitem; a++) {
572 const char *name = data->items.names[a] +
573 /* Never include the prefix in the button. */
574 (data->items.name_prefix_offsets ? data->items.name_prefix_offsets[a] :
575 0);
576 const char *name_sep = data->use_shortcut_sep ? strrchr(name, UI_SEP_CHAR) : nullptr;
577 if (STREQLEN(but->editstr, name, name_sep ? (name_sep - name) : data->items.maxstrlen)) {
578 data->active = a;
579 break;
580 }
581 }
582 if (data->items.totitem == 1 && but->editstr[0]) {
583 data->active = 0;
584 }
585 }
586
587 /* Nothing active, check at mouse location. */
588 if (data->active == -1 && data->search_but && data->search_but->editstr[0] == '\0') {
589 wmWindow *win = CTX_wm_window(C);
590 if (win && win->runtime && win->runtime->eventstate) {
591 const int cursor_x = win->runtime->eventstate->xy[0];
592 const int cursor_y = win->runtime->eventstate->xy[1];
593 if (BLI_rcti_isect_pt(&region->winrct, cursor_x, cursor_y)) {
594 rcti rect;
595 for (int a = 0; a < data->items.totitem; a++) {
596 searchbox_butrect(&rect, data, a);
598 &rect, cursor_x - region->winrct.xmin, cursor_y - region->winrct.ymin))
599 {
600 data->active = a;
601 break;
602 }
603 }
604 }
605 }
606 }
607
608 /* validate selected item */
609 searchbox_select(C, region, but, 0);
610
611 ED_region_tag_redraw(region);
612}
613
615{
616 ButtonSearch *search_but = static_cast<ButtonSearch *>(but);
617 uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
618 int match = AUTOCOMPLETE_NO_MATCH;
619
621
622 if (str[0]) {
623 int maxncpy = button_string_get_maxncpy(but);
624 if (maxncpy == 0) {
625 /* The string length is dynamic, just assume a reasonable length. */
626 maxncpy = strlen(str) + 1024;
627 }
628 data->items.autocpl = autocomplete_begin(str, maxncpy);
629
630 searchbox_update_fn(C, search_but, but->editstr, &data->items);
631
632 match = autocomplete_end(data->items.autocpl, str);
633 data->items.autocpl = nullptr;
634 }
635
636 return match;
637}
638
644static void searchbox_draw_clip_tri_down(rcti *rect, const float zoom)
645{
646 const float x = BLI_rcti_cent_x(rect) - (0.5f * zoom * UI_ICON_SIZE);
647 const float y = rect->ymin - (0.5f * zoom * (UI_SEARCHBOX_TRIA_H - UI_ICON_SIZE) - U.pixelsize) -
648 zoom * UI_ICON_SIZE;
649 const float aspect = U.inv_scale_factor / zoom;
651 icon_draw_ex(x, y, ICON_TRIA_DOWN, aspect, 1.0f, 0.0f, nullptr, false, UI_NO_ICON_OVERLAY_TEXT);
653}
654
660static void searchbox_draw_clip_tri_up(rcti *rect, const float zoom)
661{
662 const float x = BLI_rcti_cent_x(rect) - (0.5f * zoom * UI_ICON_SIZE);
663 const float y = rect->ymax + (0.5f * zoom * (UI_SEARCHBOX_TRIA_H - UI_ICON_SIZE) - U.pixelsize);
664 const float aspect = U.inv_scale_factor / zoom;
666 icon_draw_ex(x, y, ICON_TRIA_UP, aspect, 1.0f, 0.0f, nullptr, false, UI_NO_ICON_OVERLAY_TEXT);
668}
669
670static void searchbox_region_draw_fn(const bContext *C, ARegion *region)
671{
672 uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
673
674 /* pixel space */
676
677 if (data->noback == false) {
678 draw_widget_menu_back(&data->bbox, true);
679 }
680
681 /* draw text */
682 if (data->items.totitem) {
683 rcti rect;
684
685 if (data->preview) {
686 /* draw items */
687 for (int a = 0; a < data->items.totitem; a++) {
688 const int64_t but_flag = ((a == data->active) ? UI_HOVER : 0) | data->items.but_flags[a];
689
690 /* ensure icon is up-to-date */
691 icon_ensure_deferred(C, data->items.icons[a], data->preview);
692
693 searchbox_butrect(&rect, data, a);
694
695 /* widget itself */
696 draw_preview_item(&data->fstyle,
697 &rect,
698 data->zoom,
699 data->items.names[a],
700 data->items.icons[a],
701 but_flag,
703 }
704
705 /* indicate more */
706 if (data->items.more || data->items.offset) {
707 rcti rect_first_item;
708 searchbox_butrect(&rect_first_item, data, 0);
709 rcti rect_max_item;
710 searchbox_butrect(&rect_max_item, data, data->items.maxitem - 1);
711
712 if (data->items.offset) {
713 /* The first item is in the top left corner. Adjust width so the icon is centered. */
714 rect_first_item.xmax = rect_max_item.xmax;
715 searchbox_draw_clip_tri_up(&rect_first_item, data->zoom);
716 }
717
718 if (data->items.more) {
719 /* The last item is in the bottom right corner. Adjust width so the icon is centered. */
720 rect_max_item.xmin = rect_first_item.xmin;
721 searchbox_draw_clip_tri_down(&rect_max_item, data->zoom);
722 }
723 }
724 }
725 else {
726 const int search_sep_len = data->sep_string ? strlen(data->sep_string) : 0;
727 /* draw items */
728 for (int a = 0; a < data->items.totitem; a++) {
729 const int64_t but_flag = ((a == data->active) ? UI_HOVER : 0) | data->items.but_flags[a];
730 const char *name = data->items.names[a];
731 int icon = data->items.icons[a];
732 char *name_sep_test = nullptr;
733
735 if (data->use_shortcut_sep) {
736 separator_type = UI_MENU_ITEM_SEPARATOR_SHORTCUT;
737 }
738 /* Only set for displaying additional hint (e.g. library name of a linked data-block). */
739 else if (but_flag & BUT_HAS_SEP_CHAR) {
740 separator_type = UI_MENU_ITEM_SEPARATOR_HINT;
741 }
742
743 searchbox_butrect(&rect, data, a);
744
745 /* widget itself */
746 if ((search_sep_len == 0) ||
747 !(name_sep_test = strstr(data->items.names[a], data->sep_string)))
748 {
749 if (!icon && data->items.has_icon) {
750 /* If there is any icon item, make sure all items line up. */
751 icon = ICON_BLANK1;
752 }
753
754 /* Simple menu item. */
755 draw_menu_item(&data->fstyle,
756 &rect,
757 &rect,
758 data->zoom,
759 data->noback,
760 name,
761 icon,
762 but_flag,
763 separator_type,
764 nullptr);
765 }
766 else {
767 /* Split menu item, faded text before the separator. */
768 char *name_sep = nullptr;
769 do {
770 name_sep = name_sep_test;
771 name_sep_test = strstr(name_sep + search_sep_len, data->sep_string);
772 } while (name_sep_test != nullptr);
773
774 name_sep += search_sep_len;
775 const char name_sep_prev = *name_sep;
776 *name_sep = '\0';
777 int name_width = 0;
778 draw_menu_item(&data->fstyle,
779 &rect,
780 &rect,
781 data->zoom,
782 data->noback,
783 name,
784 ICON_NONE,
785 but_flag | BUT_INACTIVE,
787 &name_width);
788 *name_sep = name_sep_prev;
789 rect.xmin += name_width;
790 rect.xmin += UI_UNIT_X / 4;
791
792 if (icon == ICON_BLANK1) {
793 icon = ICON_NONE;
794 }
795 if (icon != ICON_NONE) {
796 rect.xmin += UI_UNIT_X / 8;
797 }
798
799 /* The previous menu item draws the active selection. */
800 draw_menu_item(&data->fstyle,
801 &rect,
802 nullptr,
803 data->zoom,
804 data->noback,
805 name_sep,
806 icon,
807 but_flag,
808 separator_type,
809 nullptr);
810 }
811 }
812 /* indicate more */
813 if (data->items.more) {
814 searchbox_butrect(&rect, data, data->items.maxitem - 1);
816 }
817 if (data->items.offset) {
818 searchbox_butrect(&rect, data, 0);
819 searchbox_draw_clip_tri_up(&rect, data->zoom);
820 }
821 }
822 }
823 else {
824 rcti rect;
825 searchbox_butrect(&rect, data, 0);
826 draw_menu_item(&data->fstyle,
827 &rect,
828 &rect,
829 data->zoom,
830 data->noback,
831 IFACE_("No results found"),
832 0,
833 0,
835 nullptr);
836 }
837}
838
840{
841 uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
842
843 /* free search data */
844 for (int a = 0; a < data->items.maxitem; a++) {
845 MEM_delete(data->items.names[a]);
846 }
847 MEM_delete(data->items.names);
848 MEM_delete(data->items.pointers);
849 MEM_delete(data->items.icons);
850 MEM_delete(data->items.but_flags);
851
852 if (data->items.name_prefix_offsets != nullptr) {
853 MEM_delete(data->items.name_prefix_offsets);
854 }
855
856 MEM_delete(data);
857 region->regiondata = nullptr;
858}
859
861{
862 uiSearchboxData *data = static_cast<uiSearchboxData *>(params->region->regiondata);
863 if (data->search_listener) {
864 data->search_listener(params, data->search_arg);
865 }
866}
867
868static void searchbox_region_layout_fn(const bContext *C, ARegion *region)
869{
870 uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
871
872 if (data->size_set) {
873 /* Already set. */
874 return;
875 }
876
877 ButtonSearch *but = data->search_but;
878 ARegion *butregion = data->butregion;
879 const int margin = UI_POPUP_MARGIN;
880 wmWindow *win = CTX_wm_window(C);
881
882 /* compute position */
883 if (but->block->flag & BLOCK_SEARCH_MENU) {
884 /* this case is search menu inside other menu */
885 /* we copy region size */
886
887 region->winrct = butregion->winrct;
888
889 /* Align menu items with the search button. */
890 const float zoom = data->zoom;
891 const int padding = zoom * UI_SEARCHBOX_BOUNDS - (data->preview ? 0 : U.pixelsize);
892 const int search_but_h = BLI_rctf_size_y(&but->rect) + zoom * UI_SEARCHBOX_BOUNDS;
893
894 /* widget rect, in region coords */
895 data->bbox.xmin = margin + padding;
896 data->bbox.xmax = BLI_rcti_size_x(&region->winrct) - (margin + padding);
897 data->bbox.ymin = margin;
898 data->bbox.ymax = BLI_rcti_size_y(&region->winrct) - UI_POPUP_MENU_TOP;
899
900 /* check if button is lower half */
901 if (but->rect.ymax < BLI_rctf_cent_y(&but->block->rect)) {
902 data->bbox.ymin += search_but_h;
903 }
904 else {
905 data->bbox.ymax -= search_but_h;
906 }
907 }
908 else {
909 const int searchbox_width = searchbox_size_x_from_items(data->items);
910
911 rctf rect_fl;
912 rect_fl.xmin = but->rect.xmin;
913 rect_fl.xmax = but->rect.xmax;
914 rect_fl.ymax = but->rect.ymin;
915 rect_fl.ymin = rect_fl.ymax - searchbox_size_y();
916
917 const int ofsx = (but->block->panel) ? but->block->panel->ofsx : 0;
918 const int ofsy = (but->block->panel) ? but->block->panel->ofsy : 0;
919
920 BLI_rctf_translate(&rect_fl, ofsx, ofsy);
921
922 /* minimal width */
923 if (BLI_rctf_size_x(&rect_fl) < searchbox_width) {
924 rect_fl.xmax = rect_fl.xmin + searchbox_width;
925 }
926
927 /* copy to int, gets projected if possible too */
928 rcti rect_i;
929 BLI_rcti_rctf_copy(&rect_i, &rect_fl);
930
931 if (butregion->v2d.cur.xmin != butregion->v2d.cur.xmax) {
932 view2d_view_to_region_rcti(&butregion->v2d, &rect_fl, &rect_i);
933 }
934
935 BLI_rcti_translate(&rect_i, butregion->winrct.xmin, butregion->winrct.ymin);
936
937 int winx = WM_window_native_pixel_x(win);
938 // winy = WM_window_pixels_y(win); /* UNUSED */
939 // wm_window_get_size(win, &winx, &winy);
940
941 if (rect_i.xmax > winx) {
942 /* super size */
943 if (rect_i.xmax > winx + rect_i.xmin) {
944 rect_i.xmax = winx;
945 rect_i.xmin = 0;
946 }
947 else {
948 rect_i.xmin -= rect_i.xmax - winx;
949 rect_i.xmax = winx;
950 }
951 }
952
953 if (rect_i.ymin < 0) {
954 int newy1 = but->rect.ymax + ofsy;
955
956 if (butregion->v2d.cur.xmin != butregion->v2d.cur.xmax) {
957 newy1 = view2d_view_to_region_y(&butregion->v2d, newy1);
958 }
959
960 newy1 += butregion->winrct.ymin;
961
962 rect_i.ymax = BLI_rcti_size_y(&rect_i) + newy1;
963 rect_i.ymin = newy1;
964 }
965
966 /* widget rect, in region coords */
967 data->bbox.xmin = margin;
968 data->bbox.xmax = BLI_rcti_size_x(&rect_i) + margin;
969 data->bbox.ymin = margin;
970 data->bbox.ymax = BLI_rcti_size_y(&rect_i) + margin;
971
972 /* region bigger for shadow */
973 region->winrct.xmin = rect_i.xmin - margin;
974 region->winrct.xmax = rect_i.xmax + margin;
975 region->winrct.ymin = rect_i.ymin - margin;
976 region->winrct.ymax = rect_i.ymax;
977 }
978
979 region->winx = region->winrct.xmax - region->winrct.xmin + 1;
980 region->winy = region->winrct.ymax - region->winrct.ymin + 1;
981
982 data->size_set = true;
983}
984
986 ARegion *butregion,
987 ButtonSearch *but,
988 const bool use_shortcut_sep)
989{
990 const uiStyle *style = style_get();
991 const float aspect = but->block->aspect;
992
993 /* create area region */
995
996 static ARegionType type;
997 memset(&type, 0, sizeof(ARegionType));
1003 region->runtime->type = &type;
1004
1005 /* Create search-box data. */
1006 uiSearchboxData *data = MEM_new<uiSearchboxData>(__func__);
1007 data->search_arg = but->arg.get();
1008 data->search_but = but;
1009 data->butregion = butregion;
1010 data->size_set = false;
1011 data->search_listener = but->listen_fn;
1012 data->zoom = 1.0f / aspect;
1013
1014 /* Set font, get the bounding-box. */
1015 data->fstyle = style->widget; /* copy struct */
1016 fontscale(&data->fstyle.points, aspect);
1017 fontstyle_set(&data->fstyle);
1018
1019 region->regiondata = data;
1020
1021 /* Special case, hard-coded feature, not draw backdrop when called from menus,
1022 * assume for design that popup already added it. */
1023 if (but->block->flag & BLOCK_SEARCH_MENU) {
1024 data->noback = true;
1025 }
1026
1027 if (but->preview_rows > 0 && but->preview_cols > 0) {
1028 data->preview = true;
1029 data->prv_rows = but->preview_rows;
1030 data->prv_cols = but->preview_cols;
1031 }
1032
1033 if (but->optype != nullptr || use_shortcut_sep) {
1034 data->use_shortcut_sep = true;
1035 }
1036 data->sep_string = but->item_sep_string;
1037
1038 /* Adds sub-window. */
1040
1041 /* notify change and redraw */
1042 ED_region_tag_redraw(region);
1043
1044 /* prepare search data */
1045 if (data->preview) {
1046 data->items.maxitem = data->prv_rows * data->prv_cols;
1047 }
1048 else {
1049 data->items.maxitem = SEARCH_ITEMS;
1050 }
1051 /* In case the button's string is dynamic, make sure there are buffers available. */
1052 data->items.maxstrlen = but->hardmax == 0 ? UI_MAX_NAME_STR : but->hardmax;
1053 data->items.totitem = 0;
1054 data->items.names = MEM_new_array_zeroed<char *>(data->items.maxitem, __func__);
1055 data->items.pointers = MEM_new_array_zeroed<void *>(data->items.maxitem, __func__);
1056 data->items.icons = MEM_new_array_zeroed<int>(data->items.maxitem, __func__);
1057 data->items.but_flags = MEM_new_array_zeroed<int64_t>(data->items.maxitem, __func__);
1058 data->items.name_prefix_offsets = nullptr; /* Lazy initialized as needed. */
1059 for (int i = 0; i < data->items.maxitem; i++) {
1060 data->items.names[i] = MEM_new_array_zeroed<char>(data->items.maxstrlen + 1, __func__);
1061 }
1062
1063 return region;
1064}
1065
1067{
1068 return searchbox_create_generic_ex(C, butregion, search_but, false);
1069}
1070
1077static void str_tolower_titlecaps_ascii(char *str, const size_t len)
1078{
1079 bool prev_delim = true;
1080
1081 for (size_t i = 0; (i < len) && str[i]; i++) {
1082 if (str[i] >= 'A' && str[i] <= 'Z') {
1083 if (prev_delim == false) {
1084 str[i] += 'a' - 'A';
1085 }
1086 }
1087 else if (str[i] == '_') {
1088 str[i] = ' ';
1089 }
1090
1091 prev_delim = ELEM(str[i], ' ') || (str[i] >= '0' && str[i] <= '9');
1092 }
1093}
1094
1095static void searchbox_region_draw_cb__operator(const bContext * /*C*/, ARegion *region)
1096{
1097 uiSearchboxData *data = static_cast<uiSearchboxData *>(region->regiondata);
1098
1099 /* pixel space */
1101
1102 if (data->noback == false) {
1103 draw_widget_menu_back(&data->bbox, true);
1104 }
1105
1106 /* draw text */
1107 if (data->items.totitem) {
1108 rcti rect;
1109
1110 /* draw items */
1111 for (int a = 0; a < data->items.totitem; a++) {
1112 rcti rect_pre, rect_post;
1113 searchbox_butrect(&rect, data, a);
1114
1115 rect_pre = rect;
1116 rect_post = rect;
1117
1118 rect_pre.xmax = rect_post.xmin = rect.xmin + ((rect.xmax - rect.xmin) / 4);
1119
1120 /* widget itself */
1121 /* NOTE: i18n messages extracting tool does the same, please keep it in sync. */
1122 {
1123 const int64_t but_flag = ((a == data->active) ? UI_HOVER : 0) | data->items.but_flags[a];
1124
1125 wmOperatorType *ot = static_cast<wmOperatorType *>(data->items.pointers[a]);
1126 char text_pre[128];
1127 const char *text_pre_p = strstr(ot->idname, "_OT_");
1128 if (text_pre_p == nullptr) {
1129 text_pre[0] = '\0';
1130 }
1131 else {
1132 int text_pre_len;
1133 text_pre_p += 1;
1134 text_pre_len = BLI_strncpy_utf8_rlen(
1135 text_pre, ot->idname, min_ii(sizeof(text_pre), text_pre_p - ot->idname));
1136 text_pre[text_pre_len] = ':';
1137 text_pre[text_pre_len + 1] = '\0';
1138 str_tolower_titlecaps_ascii(text_pre, sizeof(text_pre));
1139 }
1140
1141 draw_menu_item(&data->fstyle,
1142 &rect_pre,
1143 &rect,
1144 data->zoom,
1145 data->noback,
1147 data->items.icons[a],
1148 but_flag,
1150 nullptr);
1151 draw_menu_item(&data->fstyle,
1152 &rect_post,
1153 nullptr,
1154 data->zoom,
1155 data->noback,
1156 data->items.names[a],
1157 0,
1158 but_flag,
1159 data->use_shortcut_sep ? UI_MENU_ITEM_SEPARATOR_SHORTCUT :
1161 nullptr);
1162 }
1163 }
1164 /* indicate more */
1165 if (data->items.more) {
1166 searchbox_butrect(&rect, data, data->items.maxitem - 1);
1167 searchbox_draw_clip_tri_down(&rect, data->zoom);
1168 }
1169 if (data->items.offset) {
1170 searchbox_butrect(&rect, data, 0);
1171 searchbox_draw_clip_tri_up(&rect, data->zoom);
1172 }
1173 }
1174 else {
1175 rcti rect;
1176 searchbox_butrect(&rect, data, 0);
1177 draw_menu_item(&data->fstyle,
1178 &rect,
1179 &rect,
1180 data->zoom,
1181 data->noback,
1182 IFACE_("No results found"),
1183 0,
1184 0,
1186 nullptr);
1187 }
1188}
1189
1191{
1192 ARegion *region = searchbox_create_generic_ex(C, butregion, search_but, true);
1193
1195
1196 return region;
1197}
1198
1200{
1202}
1203
1204static void searchbox_region_draw_cb__menu(const bContext * /*C*/, ARegion * /*region*/)
1205{
1206 /* Currently unused. */
1207}
1208
1210{
1211 ARegion *region = searchbox_create_generic_ex(C, butregion, search_but, true);
1212
1213 if (false) {
1215 }
1216
1217 return region;
1218}
1219
1221{
1222 /* possibly very large lists (such as ID datablocks) only
1223 * only validate string RNA buts (not pointers) */
1224 if (but->rnaprop && RNA_property_type(but->rnaprop) != PROP_STRING) {
1225 return;
1226 }
1227
1228 SearchItems *items = MEM_new<SearchItems>(__func__);
1229
1230 /* setup search struct */
1231 items->maxitem = 10;
1232 items->maxstrlen = 256;
1233 items->names = MEM_new_array_zeroed<char *>(items->maxitem, __func__);
1234 for (int i = 0; i < items->maxitem; i++) {
1235 items->names[i] = MEM_new_array_zeroed<char>(but->hardmax + 1, __func__);
1236 }
1237
1239 static_cast<bContext *>(but->block->evil_C), but, but->drawstr.c_str(), items);
1240
1241 if (!but->results_are_suggestions) {
1242 /* Only red-alert when we are sure of it, this can miss cases when >10 matches. */
1243 if (items->totitem == 0) {
1245 }
1246 else if (items->more == 0) {
1247 if (search_items_find_index(items, but->drawstr.c_str()) == -1) {
1249 }
1250 }
1251 }
1252
1253 for (int i = 0; i < items->maxitem; i++) {
1254 MEM_delete(items->names[i]);
1255 }
1256 MEM_delete(items->names);
1257 MEM_delete(items);
1258}
1259
1261
1262} // namespace blender::ui
#define CTX_IFACE_(context, msgid)
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
#define IFACE_(msgid)
#define UI_ICON_SIZE
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_SEP_CHAR
#define AUTOCOMPLETE_NO_MATCH
#define UI_SEARCHBOX_BOUNDS
#define UI_UNIT_X
#define UI_SEARCHBOX_TRIA_H
#define UI_NO_ICON_OVERLAY_TEXT
#define UI_MAX_NAME_STR
#define STREQLEN(a, b, n)
#define ELEM(...)
#define STREQ(a, b)
void reset()
const T * data() const
Definition BLI_array.hh:323
IndexRange index_range() const
Definition BLI_array.hh:371
constexpr StringRef drop_prefix(int64_t n) const
void copy_utf8_truncated(char *dst, int64_t dst_size) const
Definition string_ref.cc:28
#define str(s)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
#define UI_POPUP_MENU_TOP
#define UI_POPUP_MARGIN
#define SEARCH_ITEMS
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
void draw_preview_item(const uiFontStyle *fstyle, rcti *rect, float zoom, const char *name, int iconid, int64_t but_flag, FontStyleAlign text_align)
static void searchbox_region_layout_fn(const bContext *C, ARegion *region)
void button_search_refresh(ButtonSearch *but)
int button_string_get_maxncpy(Button *but)
ARegion * searchbox_create_menu(bContext *C, ARegion *butregion, ButtonSearch *search_but)
static ARegion * searchbox_create_generic_ex(bContext *C, ARegion *butregion, ButtonSearch *but, const bool use_shortcut_sep)
static void searchbox_region_draw_cb__menu(const bContext *, ARegion *)
void draw_widget_menu_back(const rcti *rect, bool use_shadow)
const uiStyle * style_get()
void searchbox_update(bContext *C, ARegion *region, Button *but, bool reset)
static void searchbox_draw_clip_tri_up(rcti *rect, const float zoom)
static void searchbox_update_fn(bContext *C, ButtonSearch *but, const char *str, SearchItems *items)
bool searchbox_event(bContext *C, ARegion *region, Button *but, ARegion *butregion, const wmEvent *event)
float view2d_view_to_region_y(const View2D *v2d, float y)
Definition view2d.cc:1678
void region_temp_remove(bContext *C, bScreen *screen, ARegion *region)
void icon_draw_ex(float x, float y, int icon_id, float aspect, float alpha, float desaturate, const uchar mono_color[4], bool mono_border, const IconTextOverlay *text_overlay, const bool inverted=false, float scale=1.0f)
void fontstyle_set(const uiFontStyle *fs)
void autocomplete_update_name(AutoComplete *autocpl, StringRef name)
static void searchbox_region_draw_fn(const bContext *C, ARegion *region)
void view2d_view_to_region_rcti(const View2D *v2d, const rctf *rect_src, rcti *rect_dst) ATTR_NONNULL()
Definition view2d.cc:1767
ARegion * region_searchbox_region_get(const ARegion *button_region)
void searchbox_free(bContext *C, ARegion *region)
void fontscale(float *points, float aspect)
int searchbox_size_x_guess(const bContext *C, const ButtonSearchUpdateFn update_fn, void *arg)
static void searchbox_region_free_fn(ARegion *region)
static void searchbox_region_draw_cb__operator(const bContext *, ARegion *region)
ARegion * region_temp_add(bScreen *screen)
void button_flag_enable(Button *but, int64_t flag)
ARegion * searchbox_create_operator(bContext *C, ARegion *butregion, ButtonSearch *search_but)
int search_items_find_index(const SearchItems *items, const char *name)
static void str_tolower_titlecaps_ascii(char *str, const size_t len)
bool search_item_add(SearchItems *items, StringRef name, void *poin, int iconid, int64_t but_flag, uint8_t name_prefix_offset)
static ARegion * wm_searchbox_tooltip_init(bContext *C, ARegion *region, int *, double *, bool *r_exit_on_event)
static int searchbox_size_x_from_items(const SearchItems &items)
bool searchbox_inside(ARegion *region, const int xy[2]) ATTR_NONNULL(1
void pan_to_scroll(const wmEvent *event, int *type, int *val)
bool searchbox_apply(Button *but, ARegion *region)
static void searchbox_draw_clip_tri_down(rcti *rect, const float zoom)
ARegion * searchbox_create_generic(bContext *C, ARegion *butregion, ButtonSearch *search_but)
static void searchbox_select(bContext *C, ARegion *region, Button *but, int step)
int searchbox_autocomplete(bContext *C, ARegion *region, Button *but, char *str)
void(*)(const bContext *C, void *arg, const char *str, SearchItems *items, bool is_first) ButtonSearchUpdateFn
static void searchbox_butrect(rcti *r_rect, uiSearchboxData *data, int itemnr)
bool int searchbox_find_index(ARegion *region, const char *name)
AutoComplete * autocomplete_begin(const char *startname, size_t maxncpy)
void icon_ensure_deferred(const bContext *C, int icon_id, bool big)
static void searchbox_region_listen_fn(const wmRegionListenerParams *params)
void draw_menu_item(const uiFontStyle *fstyle, rcti *rect, rcti *back_rect, float zoom, bool use_unpadded, const char *name, int iconid, int64_t but_flag, MenuItemSeparatorType separator_type, int *r_xmax)
void(*)(const wmRegionListenerParams *params, void *arg) ButtonSearchListenFn
int autocomplete_end(AutoComplete *autocpl, char *autoname)
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
ScrArea * CTX_wm_area(const bContext *C)
__int64 int64_t
Definition BKE_text.h:90
void wmOrtho2_region_pixelspace(const ARegion *region)
uint padding(uint offset, uint alignment)
char size_t BLI_strncpy_utf8_rlen(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
void BLI_rcti_rctf_copy(struct rcti *dst, const struct rctf *src)
wmWindow * CTX_wm_window(const bContext *C)
int WM_window_native_pixel_x(const wmWindow *win)
void WM_tooltip_clear(bContext *C, wmWindow *win)
Definition wm_tooltip.cc:84
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
void WM_tooltip_timer_init(bContext *C, wmWindow *win, ScrArea *area, ARegion *region, wmTooltipInitFn init)
Definition wm_tooltip.cc:66
@ GPU_BLEND_NONE
Definition GPU_state.hh:88
@ GPU_BLEND_ALPHA
Definition GPU_state.hh:90
PropertyType RNA_property_type(PropertyRNA *prop)
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
@ PROP_STRING
Definition RNA_types.hh:213
BMesh const char void * data
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
unsigned char uint8_t
Definition BKE_text.h:79
void BLI_rcti_translate(struct rcti *rect, int x, int y)
Definition rct.cc:580
void ED_region_floating_init(ARegion *region)
Definition area.cc:2544
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
BLI_INLINE float BLI_rctf_cent_y(const struct rctf *rct)
Definition BLI_rect.hh:198
void BLI_rctf_translate(struct rctf *rect, float x, float y)
Definition rct.cc:587
bScreen * CTX_wm_screen(const bContext *C)
void GPU_blend(GPUBlend blend)
Definition gpu_state.cc:44
bool BLI_rcti_isect_pt(const struct rcti *rect, int x, int y)
BLI_INLINE int BLI_rcti_cent_x(const struct rcti *rct)
Definition BLI_rect.hh:186
MINLINE int min_ii(int a, int b)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void(*) free(ARegion *)
void(*) draw(const bContext *C, ARegion *region)
void(*) listener(const wmRegionListenerParams *params)
void(*) layout(const bContext *C, ARegion *region)
bke::ARegionRuntime * runtime
ListBaseT< ui::Block > uiblocks
struct ARegionType * type
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< void > arg
ButtonSearchListenFn listen_fn
ButtonSearchContextMenuFn item_context_menu_fn
ButtonSearchTooltipFn item_tooltip_fn
ButtonSearchUpdateFn items_update_fn
wmOperatorType * optype
bke::WindowRuntime * runtime
uiFontStyle widget