1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup edinterface
22  */
23 
24 #include <ctype.h>
25 #include <float.h>
26 #include <limits.h>
27 #include <math.h>
28 #include <stddef.h> /* offsetof() */
29 #include <string.h>
30 
31 #include "MEM_guardedalloc.h"
32 
33 #include "DNA_object_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_screen_types.h"
36 #include "DNA_userdef_types.h"
37 #include "DNA_workspace_types.h"
38 
39 #include "BLI_alloca.h"
40 #include "BLI_listbase.h"
41 #include "BLI_math.h"
42 #include "BLI_rect.h"
43 #include "BLI_string.h"
44 #include "BLI_string_search.h"
45 #include "BLI_string_utf8.h"
46 
47 #include "BLI_utildefines.h"
48 
49 #include "BLO_readfile.h"
50 
51 #include "BKE_animsys.h"
52 #include "BKE_context.h"
53 #include "BKE_idprop.h"
54 #include "BKE_main.h"
55 #include "BKE_scene.h"
56 #include "BKE_screen.h"
57 #include "BKE_unit.h"
58 
59 #include "GPU_matrix.h"
60 #include "GPU_state.h"
61 
62 #include "BLF_api.h"
63 #include "BLT_translation.h"
64 
65 #include "UI_interface.h"
66 #include "UI_interface_icons.h"
67 #include "UI_view2d.h"
68 
69 #include "IMB_imbuf.h"
70 
71 #include "WM_api.h"
72 #include "WM_message.h"
73 #include "WM_types.h"
74 
75 #include "RNA_access.h"
76 
77 #ifdef WITH_PYTHON
78 #  include "BPY_extern_run.h"
79 #endif
80 
81 #include "ED_numinput.h"
82 #include "ED_screen.h"
83 
84 #include "IMB_colormanagement.h"
85 
86 #include "DEG_depsgraph_query.h"
87 
88 #include "interface_intern.h"
89 
90 /* prototypes. */
91 static void ui_but_to_pixelrect(struct rcti *rect,
92                                 const struct ARegion *region,
93                                 struct uiBlock *block,
94                                 struct uiBut *but);
95 static void ui_def_but_rna__menu(bContext *UNUSED(C), uiLayout *layout, void *but_p);
96 static void ui_def_but_rna__panel_type(bContext *UNUSED(C), uiLayout *layout, void *but_p);
97 static void ui_def_but_rna__menu_type(bContext *UNUSED(C), uiLayout *layout, void *but_p);
98 
99 /* avoid unneeded calls to ui_but_value_get */
100 #define UI_BUT_VALUE_UNSET DBL_MAX
101 #define UI_GET_BUT_VALUE_INIT(_but, _value) \
102   if (_value == DBL_MAX) { \
103     (_value) = ui_but_value_get(_but); \
104   } \
105   ((void)0)
106 
107 #define B_NOP -1
108 
109 /**
110  * a full doc with API notes can be found in 'blender/doc/guides/interface_API.txt'
111  *
112  * `uiBlahBlah()`   external function.
113  * `ui_blah_blah()` internal function.
114  */
115 
116 static void ui_but_free(const bContext *C, uiBut *but);
117 
ui_but_is_unit_radians_ex(UnitSettings * unit,const int unit_type)118 static bool ui_but_is_unit_radians_ex(UnitSettings *unit, const int unit_type)
119 {
120   return (unit->system_rotation == USER_UNIT_ROT_RADIANS && unit_type == PROP_UNIT_ROTATION);
121 }
122 
ui_but_is_unit_radians(const uiBut * but)123 static bool ui_but_is_unit_radians(const uiBut *but)
124 {
125   UnitSettings *unit = but->block->unit;
126   const int unit_type = UI_but_unit_type_get(but);
127 
128   return ui_but_is_unit_radians_ex(unit, unit_type);
129 }
130 
131 /* ************* window matrix ************** */
132 
ui_block_to_window_fl(const ARegion * region,uiBlock * block,float * r_x,float * r_y)133 void ui_block_to_window_fl(const ARegion *region, uiBlock *block, float *r_x, float *r_y)
134 {
135   int getsizex = BLI_rcti_size_x(&region->winrct) + 1;
136   int getsizey = BLI_rcti_size_y(&region->winrct) + 1;
137   int sx = region->winrct.xmin;
138   int sy = region->winrct.ymin;
139 
140   float gx = *r_x;
141   float gy = *r_y;
142 
143   if (block->panel) {
144     gx += block->panel->ofsx;
145     gy += block->panel->ofsy;
146   }
147 
148   *r_x = ((float)sx) +
149          ((float)getsizex) * (0.5f + 0.5f * (gx * block->winmat[0][0] + gy * block->winmat[1][0] +
150                                              block->winmat[3][0]));
151   *r_y = ((float)sy) +
152          ((float)getsizey) * (0.5f + 0.5f * (gx * block->winmat[0][1] + gy * block->winmat[1][1] +
153                                              block->winmat[3][1]));
154 }
155 
ui_block_to_window(const ARegion * region,uiBlock * block,int * r_x,int * r_y)156 void ui_block_to_window(const ARegion *region, uiBlock *block, int *r_x, int *r_y)
157 {
158   float fx = *r_x;
159   float fy = *r_y;
160 
161   ui_block_to_window_fl(region, block, &fx, &fy);
162 
163   *r_x = (int)(fx + 0.5f);
164   *r_y = (int)(fy + 0.5f);
165 }
166 
ui_block_to_window_rctf(const ARegion * region,uiBlock * block,rctf * rct_dst,const rctf * rct_src)167 void ui_block_to_window_rctf(const ARegion *region,
168                              uiBlock *block,
169                              rctf *rct_dst,
170                              const rctf *rct_src)
171 {
172   *rct_dst = *rct_src;
173   ui_block_to_window_fl(region, block, &rct_dst->xmin, &rct_dst->ymin);
174   ui_block_to_window_fl(region, block, &rct_dst->xmax, &rct_dst->ymax);
175 }
176 
ui_block_to_window_scale(const ARegion * region,uiBlock * block)177 float ui_block_to_window_scale(const ARegion *region, uiBlock *block)
178 {
179   /* We could have function for this to avoid dummy arg. */
180   float min_y = 0, max_y = 1;
181   float dummy_x = 0.0f;
182   ui_block_to_window_fl(region, block, &dummy_x, &min_y);
183   dummy_x = 0.0f;
184   ui_block_to_window_fl(region, block, &dummy_x, &max_y);
185   return max_y - min_y;
186 }
187 
188 /* for mouse cursor */
ui_window_to_block_fl(const ARegion * region,uiBlock * block,float * r_x,float * r_y)189 void ui_window_to_block_fl(const ARegion *region, uiBlock *block, float *r_x, float *r_y)
190 {
191   int getsizex = BLI_rcti_size_x(&region->winrct) + 1;
192   int getsizey = BLI_rcti_size_y(&region->winrct) + 1;
193   int sx = region->winrct.xmin;
194   int sy = region->winrct.ymin;
195 
196   float a = 0.5f * ((float)getsizex) * block->winmat[0][0];
197   float b = 0.5f * ((float)getsizex) * block->winmat[1][0];
198   float c = 0.5f * ((float)getsizex) * (1.0f + block->winmat[3][0]);
199 
200   float d = 0.5f * ((float)getsizey) * block->winmat[0][1];
201   float e = 0.5f * ((float)getsizey) * block->winmat[1][1];
202   float f = 0.5f * ((float)getsizey) * (1.0f + block->winmat[3][1]);
203 
204   float px = *r_x - sx;
205   float py = *r_y - sy;
206 
207   *r_y = (a * (py - f) + d * (c - px)) / (a * e - d * b);
208   *r_x = (px - b * (*r_y) - c) / a;
209 
210   if (block->panel) {
211     *r_x -= block->panel->ofsx;
212     *r_y -= block->panel->ofsy;
213   }
214 }
215 
ui_window_to_block_rctf(const struct ARegion * region,uiBlock * block,rctf * rct_dst,const rctf * rct_src)216 void ui_window_to_block_rctf(const struct ARegion *region,
217                              uiBlock *block,
218                              rctf *rct_dst,
219                              const rctf *rct_src)
220 {
221   *rct_dst = *rct_src;
222   ui_window_to_block_fl(region, block, &rct_dst->xmin, &rct_dst->ymin);
223   ui_window_to_block_fl(region, block, &rct_dst->xmax, &rct_dst->ymax);
224 }
225 
ui_window_to_block(const ARegion * region,uiBlock * block,int * r_x,int * r_y)226 void ui_window_to_block(const ARegion *region, uiBlock *block, int *r_x, int *r_y)
227 {
228   float fx = *r_x;
229   float fy = *r_y;
230 
231   ui_window_to_block_fl(region, block, &fx, &fy);
232 
233   *r_x = (int)(fx + 0.5f);
234   *r_y = (int)(fy + 0.5f);
235 }
236 
ui_window_to_region(const ARegion * region,int * r_x,int * r_y)237 void ui_window_to_region(const ARegion *region, int *r_x, int *r_y)
238 {
239   *r_x -= region->winrct.xmin;
240   *r_y -= region->winrct.ymin;
241 }
242 
ui_window_to_region_rcti(const ARegion * region,rcti * rect_dst,const rcti * rct_src)243 void ui_window_to_region_rcti(const ARegion *region, rcti *rect_dst, const rcti *rct_src)
244 {
245   rect_dst->xmin = rct_src->xmin - region->winrct.xmin;
246   rect_dst->xmax = rct_src->xmax - region->winrct.xmin;
247   rect_dst->ymin = rct_src->ymin - region->winrct.ymin;
248   rect_dst->ymax = rct_src->ymax - region->winrct.ymin;
249 }
250 
ui_region_to_window(const ARegion * region,int * r_x,int * r_y)251 void ui_region_to_window(const ARegion *region, int *r_x, int *r_y)
252 {
253   *r_x += region->winrct.xmin;
254   *r_y += region->winrct.ymin;
255 }
256 
ui_update_flexible_spacing(const ARegion * region,uiBlock * block)257 static void ui_update_flexible_spacing(const ARegion *region, uiBlock *block)
258 {
259   int sepr_flex_len = 0;
260   LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
261     if (but->type == UI_BTYPE_SEPR_SPACER) {
262       sepr_flex_len++;
263     }
264   }
265 
266   if (sepr_flex_len == 0) {
267     return;
268   }
269 
270   rcti rect;
271   ui_but_to_pixelrect(&rect, region, block, block->buttons.last);
272   const float buttons_width = (float)rect.xmax + UI_HEADER_OFFSET;
273   const float region_width = (float)region->sizex * U.dpi_fac;
274 
275   if (region_width <= buttons_width) {
276     return;
277   }
278 
279   /* We could get rid of this loop if we agree on a max number of spacer */
280   int *spacers_pos = alloca(sizeof(*spacers_pos) * (size_t)sepr_flex_len);
281   int i = 0;
282   LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
283     if (but->type == UI_BTYPE_SEPR_SPACER) {
284       ui_but_to_pixelrect(&rect, region, block, but);
285       spacers_pos[i] = rect.xmax + UI_HEADER_OFFSET;
286       i++;
287     }
288   }
289 
290   const float view_scale_x = UI_view2d_scale_get_x(&region->v2d);
291   const float segment_width = region_width / (float)sepr_flex_len;
292   float offset = 0, remaining_space = region_width - buttons_width;
293   i = 0;
294   LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
295     BLI_rctf_translate(&but->rect, offset / view_scale_x, 0);
296     if (but->type == UI_BTYPE_SEPR_SPACER) {
297       /* How much the next block overlap with the current segment */
298       int overlap = ((i == sepr_flex_len - 1) ? buttons_width - spacers_pos[i] :
299                                                 (spacers_pos[i + 1] - spacers_pos[i]) / 2);
300       const int segment_end = segment_width * (i + 1);
301       const int spacer_end = segment_end - overlap;
302       const int spacer_sta = spacers_pos[i] + offset;
303       if (spacer_end > spacer_sta) {
304         const float step = min_ff(remaining_space, spacer_end - spacer_sta);
305         remaining_space -= step;
306         offset += step;
307       }
308       i++;
309     }
310   }
311   ui_block_bounds_calc(block);
312 }
313 
ui_update_window_matrix(const wmWindow * window,const ARegion * region,uiBlock * block)314 static void ui_update_window_matrix(const wmWindow *window, const ARegion *region, uiBlock *block)
315 {
316   /* window matrix and aspect */
317   if (region && region->visible) {
318     /* Get projection matrix which includes View2D translation and zoom. */
319     GPU_matrix_projection_get(block->winmat);
320     block->aspect = 2.0f / fabsf(region->winx * block->winmat[0][0]);
321   }
322   else {
323     /* No subwindow created yet, for menus for example, so we use the main
324      * window instead, since buttons are created there anyway. */
325     const int width = WM_window_pixels_x(window);
326     const int height = WM_window_pixels_y(window);
327     const rcti winrct = {0, width - 1, 0, height - 1};
328 
329     wmGetProjectionMatrix(block->winmat, &winrct);
330     block->aspect = 2.0f / fabsf(width * block->winmat[0][0]);
331   }
332 }
333 
334 /**
335  * Popups will add a margin to #ARegion.winrct for shadow,
336  * for interactivity (point-inside tests for eg), we want the winrct without the margin added.
337  */
ui_region_winrct_get_no_margin(const struct ARegion * region,struct rcti * r_rect)338 void ui_region_winrct_get_no_margin(const struct ARegion *region, struct rcti *r_rect)
339 {
340   uiBlock *block = region->uiblocks.first;
341   if (block && (block->flag & UI_BLOCK_LOOP) && (block->flag & UI_BLOCK_RADIAL) == 0) {
342     BLI_rcti_rctf_copy_floor(r_rect, &block->rect);
343     BLI_rcti_translate(r_rect, region->winrct.xmin, region->winrct.ymin);
344   }
345   else {
346     *r_rect = region->winrct;
347   }
348 }
349 
350 /* ******************* block calc ************************* */
351 
UI_block_translate(uiBlock * block,int x,int y)352 void UI_block_translate(uiBlock *block, int x, int y)
353 {
354   LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
355     BLI_rctf_translate(&but->rect, x, y);
356   }
357 
358   BLI_rctf_translate(&block->rect, x, y);
359 }
360 
ui_but_is_row_alignment_group(const uiBut * left,const uiBut * right)361 static bool ui_but_is_row_alignment_group(const uiBut *left, const uiBut *right)
362 {
363   const bool is_same_align_group = (left->alignnr && (left->alignnr == right->alignnr));
364   return is_same_align_group && (left->rect.xmin < right->rect.xmin);
365 }
366 
ui_block_bounds_calc_text(uiBlock * block,float offset)367 static void ui_block_bounds_calc_text(uiBlock *block, float offset)
368 {
369   const uiStyle *style = UI_style_get();
370   uiBut *col_bt;
371   int i = 0, j, x1addval = offset;
372 
373   UI_fontstyle_set(&style->widget);
374 
375   uiBut *init_col_bt = block->buttons.first;
376   LISTBASE_FOREACH (uiBut *, bt, &block->buttons) {
377     if (!ELEM(bt->type, UI_BTYPE_SEPR, UI_BTYPE_SEPR_LINE, UI_BTYPE_SEPR_SPACER)) {
378       j = BLF_width(style->widget.uifont_id, bt->drawstr, sizeof(bt->drawstr));
379 
380       if (j > i) {
381         i = j;
382       }
383     }
384 
385     /* Skip all buttons that are in a horizontal alignment group.
386      * We don't want to split them apart (but still check the row's width and apply current
387      * offsets). */
388     if (bt->next && ui_but_is_row_alignment_group(bt, bt->next)) {
389       int width = 0;
390       int alignnr = bt->alignnr;
391       for (col_bt = bt; col_bt && col_bt->alignnr == alignnr; col_bt = col_bt->next) {
392         width += BLI_rctf_size_x(&col_bt->rect);
393         col_bt->rect.xmin += x1addval;
394         col_bt->rect.xmax += x1addval;
395       }
396       if (width > i) {
397         i = width;
398       }
399       /* Give the following code the last button in the alignment group, there might have to be a
400        * split immediately after. */
401       bt = col_bt ? col_bt->prev : NULL;
402     }
403 
404     if (bt && bt->next && bt->rect.xmin < bt->next->rect.xmin) {
405       /* End of this column, and it's not the last one. */
406       for (col_bt = init_col_bt; col_bt->prev != bt; col_bt = col_bt->next) {
407         col_bt->rect.xmin = x1addval;
408         col_bt->rect.xmax = x1addval + i + block->bounds;
409 
410         ui_but_update(col_bt); /* clips text again */
411       }
412 
413       /* And we prepare next column. */
414       x1addval += i + block->bounds;
415       i = 0;
416       init_col_bt = col_bt;
417     }
418   }
419 
420   /* Last column. */
421   for (col_bt = init_col_bt; col_bt; col_bt = col_bt->next) {
422     /* Recognize a horizontally arranged alignment group and skip its items. */
423     if (col_bt->next && ui_but_is_row_alignment_group(col_bt, col_bt->next)) {
424       int alignnr = col_bt->alignnr;
425       for (; col_bt && col_bt->alignnr == alignnr; col_bt = col_bt->next) {
426         /* pass */
427       }
428     }
429     if (!col_bt) {
430       break;
431     }
432 
433     col_bt->rect.xmin = x1addval;
434     col_bt->rect.xmax = max_ff(x1addval + i + block->bounds, offset + block->minbounds);
435 
436     ui_but_update(col_bt); /* clips text again */
437   }
438 }
439 
ui_block_bounds_calc(uiBlock * block)440 void ui_block_bounds_calc(uiBlock *block)
441 {
442   if (BLI_listbase_is_empty(&block->buttons)) {
443     if (block->panel) {
444       block->rect.xmin = 0.0;
445       block->rect.xmax = block->panel->sizex;
446       block->rect.ymin = 0.0;
447       block->rect.ymax = block->panel->sizey;
448     }
449   }
450   else {
451 
452     BLI_rctf_init_minmax(&block->rect);
453 
454     LISTBASE_FOREACH (uiBut *, bt, &block->buttons) {
455       BLI_rctf_union(&block->rect, &bt->rect);
456     }
457 
458     block->rect.xmin -= block->bounds;
459     block->rect.ymin -= block->bounds;
460     block->rect.xmax += block->bounds;
461     block->rect.ymax += block->bounds;
462   }
463 
464   block->rect.xmax = block->rect.xmin + max_ff(BLI_rctf_size_x(&block->rect), block->minbounds);
465 
466   /* hardcoded exception... but that one is annoying with larger safety */
467   uiBut *bt = block->buttons.first;
468   int xof = (bt && STREQLEN(bt->str, "ERROR", 5)) ? 10 : 40;
469 
470   block->safety.xmin = block->rect.xmin - xof;
471   block->safety.ymin = block->rect.ymin - xof;
472   block->safety.xmax = block->rect.xmax + xof;
473   block->safety.ymax = block->rect.ymax + xof;
474 }
475 
ui_block_bounds_calc_centered(wmWindow * window,uiBlock * block)476 static void ui_block_bounds_calc_centered(wmWindow *window, uiBlock *block)
477 {
478   /* note: this is used for the splash where window bounds event has not been
479    * updated by ghost, get the window bounds from ghost directly */
480 
481   int xmax = WM_window_pixels_x(window);
482   int ymax = WM_window_pixels_y(window);
483 
484   ui_block_bounds_calc(block);
485 
486   int width = BLI_rctf_size_x(&block->rect);
487   int height = BLI_rctf_size_y(&block->rect);
488 
489   int startx = (xmax * 0.5f) - (width * 0.5f);
490   int starty = (ymax * 0.5f) - (height * 0.5f);
491 
492   UI_block_translate(block, startx - block->rect.xmin, starty - block->rect.ymin);
493 
494   /* now recompute bounds and safety */
495   ui_block_bounds_calc(block);
496 }
497 
ui_block_bounds_calc_centered_pie(uiBlock * block)498 static void ui_block_bounds_calc_centered_pie(uiBlock *block)
499 {
500   const int xy[2] = {
501       block->pie_data.pie_center_spawned[0],
502       block->pie_data.pie_center_spawned[1],
503   };
504 
505   UI_block_translate(block, xy[0], xy[1]);
506 
507   /* now recompute bounds and safety */
508   ui_block_bounds_calc(block);
509 }
510 
ui_block_bounds_calc_popup(wmWindow * window,uiBlock * block,eBlockBoundsCalc bounds_calc,const int xy[2],int r_xy[2])511 static void ui_block_bounds_calc_popup(
512     wmWindow *window, uiBlock *block, eBlockBoundsCalc bounds_calc, const int xy[2], int r_xy[2])
513 {
514   int oldbounds = block->bounds;
515 
516   /* compute mouse position with user defined offset */
517   ui_block_bounds_calc(block);
518 
519   int xmax = WM_window_pixels_x(window);
520   int ymax = WM_window_pixels_y(window);
521 
522   int oldwidth = BLI_rctf_size_x(&block->rect);
523   int oldheight = BLI_rctf_size_y(&block->rect);
524 
525   /* first we ensure wide enough text bounds */
526   if (bounds_calc == UI_BLOCK_BOUNDS_POPUP_MENU) {
527     if (block->flag & UI_BLOCK_LOOP) {
528       block->bounds = 2.5f * UI_UNIT_X;
529       ui_block_bounds_calc_text(block, block->rect.xmin);
530     }
531   }
532 
533   /* next we recompute bounds */
534   block->bounds = oldbounds;
535   ui_block_bounds_calc(block);
536 
537   /* and we adjust the position to fit within window */
538   int width = BLI_rctf_size_x(&block->rect);
539   int height = BLI_rctf_size_y(&block->rect);
540 
541   /* avoid divide by zero below, caused by calling with no UI, but better not crash */
542   oldwidth = oldwidth > 0 ? oldwidth : MAX2(1, width);
543   oldheight = oldheight > 0 ? oldheight : MAX2(1, height);
544 
545   /* offset block based on mouse position, user offset is scaled
546    * along in case we resized the block in ui_block_bounds_calc_text */
547   rcti rect;
548   int raw_x = rect.xmin = xy[0] + block->rect.xmin + (block->bounds_offset[0] * width) / oldwidth;
549   int raw_y = rect.ymin = xy[1] + block->rect.ymin +
550                           (block->bounds_offset[1] * height) / oldheight;
551   rect.xmax = rect.xmin + width;
552   rect.ymax = rect.ymin + height;
553 
554   rcti rect_bounds;
555   const int margin = UI_SCREEN_MARGIN;
556   rect_bounds.xmin = margin;
557   rect_bounds.ymin = margin;
558   rect_bounds.xmax = xmax - margin;
559   rect_bounds.ymax = ymax - UI_POPUP_MENU_TOP;
560 
561   int ofs_dummy[2];
562   BLI_rcti_clamp(&rect, &rect_bounds, ofs_dummy);
563   UI_block_translate(block, rect.xmin - block->rect.xmin, rect.ymin - block->rect.ymin);
564 
565   /* now recompute bounds and safety */
566   ui_block_bounds_calc(block);
567 
568   /* If given, adjust input coordinates such that they would generate real final popup position.
569    * Needed to handle correctly floating panels once they have been dragged around,
570    * see T52999. */
571   if (r_xy) {
572     r_xy[0] = xy[0] + block->rect.xmin - raw_x;
573     r_xy[1] = xy[1] + block->rect.ymin - raw_y;
574   }
575 }
576 
577 /* used for various cases */
UI_block_bounds_set_normal(uiBlock * block,int addval)578 void UI_block_bounds_set_normal(uiBlock *block, int addval)
579 {
580   if (block == NULL) {
581     return;
582   }
583 
584   block->bounds = addval;
585   block->bounds_type = UI_BLOCK_BOUNDS;
586 }
587 
588 /* used for pulldowns */
UI_block_bounds_set_text(uiBlock * block,int addval)589 void UI_block_bounds_set_text(uiBlock *block, int addval)
590 {
591   block->bounds = addval;
592   block->bounds_type = UI_BLOCK_BOUNDS_TEXT;
593 }
594 
595 /* used for block popups */
UI_block_bounds_set_popup(uiBlock * block,int addval,const int bounds_offset[2])596 void UI_block_bounds_set_popup(uiBlock *block, int addval, const int bounds_offset[2])
597 {
598   block->bounds = addval;
599   block->bounds_type = UI_BLOCK_BOUNDS_POPUP_MOUSE;
600   if (bounds_offset != NULL) {
601     block->bounds_offset[0] = bounds_offset[0];
602     block->bounds_offset[1] = bounds_offset[1];
603   }
604   else {
605     block->bounds_offset[0] = 0;
606     block->bounds_offset[1] = 0;
607   }
608 }
609 
610 /* used for menu popups */
UI_block_bounds_set_menu(uiBlock * block,int addval,const int bounds_offset[2])611 void UI_block_bounds_set_menu(uiBlock *block, int addval, const int bounds_offset[2])
612 {
613   block->bounds = addval;
614   block->bounds_type = UI_BLOCK_BOUNDS_POPUP_MENU;
615   if (bounds_offset != NULL) {
616     copy_v2_v2_int(block->bounds_offset, bounds_offset);
617   }
618   else {
619     zero_v2_int(block->bounds_offset);
620   }
621 }
622 
623 /* used for centered popups, i.e. splash */
UI_block_bounds_set_centered(uiBlock * block,int addval)624 void UI_block_bounds_set_centered(uiBlock *block, int addval)
625 {
626   block->bounds = addval;
627   block->bounds_type = UI_BLOCK_BOUNDS_POPUP_CENTER;
628 }
629 
UI_block_bounds_set_explicit(uiBlock * block,int minx,int miny,int maxx,int maxy)630 void UI_block_bounds_set_explicit(uiBlock *block, int minx, int miny, int maxx, int maxy)
631 {
632   block->rect.xmin = minx;
633   block->rect.ymin = miny;
634   block->rect.xmax = maxx;
635   block->rect.ymax = maxy;
636   block->bounds_type = UI_BLOCK_BOUNDS_NONE;
637 }
638 
ui_but_get_float_precision(uiBut * but)639 static float ui_but_get_float_precision(uiBut *but)
640 {
641   if (but->type == UI_BTYPE_NUM) {
642     return ((uiButNumber *)but)->precision;
643   }
644 
645   return but->a2;
646 }
647 
ui_but_calc_float_precision(uiBut * but,double value)648 static int ui_but_calc_float_precision(uiBut *but, double value)
649 {
650   int prec = (int)ui_but_get_float_precision(but);
651 
652   /* first check for various special cases:
653    * * If button is radians, we want additional precision (see T39861).
654    * * If prec is not set, we fallback to a simple default */
655   if (ui_but_is_unit_radians(but) && prec < 5) {
656     prec = 5;
657   }
658   else if (prec == -1) {
659     prec = (but->hardmax < 10.001f) ? 3 : 2;
660   }
661   else {
662     CLAMP(prec, 0, UI_PRECISION_FLOAT_MAX);
663   }
664 
665   return UI_calc_float_precision(prec, value);
666 }
667 
668 /* ************** BLOCK ENDING FUNCTION ************* */
669 
ui_but_rna_equals(const uiBut * a,const uiBut * b)670 bool ui_but_rna_equals(const uiBut *a, const uiBut *b)
671 {
672   return ui_but_rna_equals_ex(a, &b->rnapoin, b->rnaprop, b->rnaindex);
673 }
674 
ui_but_rna_equals_ex(const uiBut * but,const PointerRNA * ptr,const PropertyRNA * prop,int index)675 bool ui_but_rna_equals_ex(const uiBut *but,
676                           const PointerRNA *ptr,
677                           const PropertyRNA *prop,
678                           int index)
679 {
680   if (but->rnapoin.data != ptr->data) {
681     return false;
682   }
683   if (but->rnaprop != prop || but->rnaindex != index) {
684     return false;
685   }
686 
687   return true;
688 }
689 
690 /* NOTE: if but->poin is allocated memory for every defbut, things fail... */
ui_but_equals_old(const uiBut * but,const uiBut * oldbut)691 static bool ui_but_equals_old(const uiBut *but, const uiBut *oldbut)
692 {
693   /* various properties are being compared here, hopefully sufficient
694    * to catch all cases, but it is simple to add more checks later */
695   if (but->retval != oldbut->retval) {
696     return false;
697   }
698   if (!ui_but_rna_equals(but, oldbut)) {
699     return false;
700   }
701   if (but->func != oldbut->func) {
702     return false;
703   }
704   if (but->funcN != oldbut->funcN) {
705     return false;
706   }
707   if (oldbut->func_arg1 != oldbut && but->func_arg1 != oldbut->func_arg1) {
708     return false;
709   }
710   if (oldbut->func_arg2 != oldbut && but->func_arg2 != oldbut->func_arg2) {
711     return false;
712   }
713   if (!but->funcN && ((but->poin != oldbut->poin && (uiBut *)oldbut->poin != oldbut) ||
714                       (but->pointype != oldbut->pointype))) {
715     return false;
716   }
717   if (but->optype != oldbut->optype) {
718     return false;
719   }
720 
721   return true;
722 }
723 
ui_but_find_old(uiBlock * block_old,const uiBut * but_new)724 uiBut *ui_but_find_old(uiBlock *block_old, const uiBut *but_new)
725 {
726   LISTBASE_FOREACH (uiBut *, but, &block_old->buttons) {
727     if (ui_but_equals_old(but_new, but)) {
728       return but;
729     }
730   }
731   return NULL;
732 }
733 
ui_but_find_new(uiBlock * block_new,const uiBut * but_old)734 uiBut *ui_but_find_new(uiBlock *block_new, const uiBut *but_old)
735 {
736   LISTBASE_FOREACH (uiBut *, but, &block_new->buttons) {
737     if (ui_but_equals_old(but, but_old)) {
738       return but;
739     }
740   }
741   return NULL;
742 }
743 
ui_but_extra_icons_equals_old(const uiButExtraOpIcon * new_extra_icon,const uiButExtraOpIcon * old_extra_icon)744 static bool ui_but_extra_icons_equals_old(const uiButExtraOpIcon *new_extra_icon,
745                                           const uiButExtraOpIcon *old_extra_icon)
746 {
747   return (new_extra_icon->optype_params->optype == old_extra_icon->optype_params->optype) &&
748          (new_extra_icon->icon == old_extra_icon->icon);
749 }
750 
ui_but_extra_icon_find_old(const uiButExtraOpIcon * new_extra_icon,const uiBut * old_but)751 static uiButExtraOpIcon *ui_but_extra_icon_find_old(const uiButExtraOpIcon *new_extra_icon,
752                                                     const uiBut *old_but)
753 {
754   LISTBASE_FOREACH (uiButExtraOpIcon *, op_icon, &old_but->extra_op_icons) {
755     if (ui_but_extra_icons_equals_old(new_extra_icon, op_icon)) {
756       return op_icon;
757     }
758   }
759   return NULL;
760 }
761 
ui_but_extra_icons_update_from_old_but(const uiBut * new_but,const uiBut * old_but)762 static void ui_but_extra_icons_update_from_old_but(const uiBut *new_but, const uiBut *old_but)
763 {
764   /* Specifically for keeping some state info for the active button. */
765   BLI_assert(old_but->active);
766 
767   LISTBASE_FOREACH (uiButExtraOpIcon *, new_extra_icon, &new_but->extra_op_icons) {
768     uiButExtraOpIcon *old_extra_icon = ui_but_extra_icon_find_old(new_extra_icon, old_but);
769     /* Keep the highlighting state, and let handling update it later. */
770     if (old_extra_icon) {
771       new_extra_icon->highlighted = old_extra_icon->highlighted;
772     }
773   }
774 }
775 
776 /**
777  * \return true when \a but_p is set (only done for active buttons).
778  */
ui_but_update_from_old_block(const bContext * C,uiBlock * block,uiBut ** but_p,uiBut ** but_old_p)779 static bool ui_but_update_from_old_block(const bContext *C,
780                                          uiBlock *block,
781                                          uiBut **but_p,
782                                          uiBut **but_old_p)
783 {
784   const int drawflag_copy = 0; /* None currently. */
785 
786   uiBlock *oldblock = block->oldblock;
787   uiBut *oldbut = NULL, *but = *but_p;
788   bool found_active = false;
789 
790 #if 0
791   /* simple/stupid - search every time */
792   oldbut = ui_but_find_old(oldblock, but);
793   (void)but_old_p;
794 #else
795   BLI_assert(*but_old_p == NULL || BLI_findindex(&oldblock->buttons, *but_old_p) != -1);
796 
797   /* Fast-path - avoid loop-in-loop, calling #ui_but_find_old
798    * as long as old/new buttons are aligned. */
799   if (LIKELY(*but_old_p && ui_but_equals_old(but, *but_old_p))) {
800     oldbut = *but_old_p;
801   }
802   else {
803     /* fallback to block search */
804     oldbut = ui_but_find_old(oldblock, but);
805   }
806   (*but_old_p) = oldbut ? oldbut->next : NULL;
807 #endif
808 
809   if (!oldbut) {
810     return found_active;
811   }
812 
813   if (oldbut->active) {
814     /* flags from the buttons we want to refresh, may want to add more here... */
815     const int flag_copy = UI_BUT_REDALERT | UI_HAS_ICON;
816 
817     found_active = true;
818 
819 #if 0
820     but->flag = oldbut->flag;
821     but->active = oldbut->active;
822     but->pos = oldbut->pos;
823     but->ofs = oldbut->ofs;
824     but->editstr = oldbut->editstr;
825     but->editval = oldbut->editval;
826     but->editvec = oldbut->editvec;
827     but->selsta = oldbut->selsta;
828     but->selend = oldbut->selend;
829     but->softmin = oldbut->softmin;
830     but->softmax = oldbut->softmax;
831     oldbut->active = NULL;
832 #endif
833 
834     /* move button over from oldblock to new block */
835     BLI_remlink(&oldblock->buttons, oldbut);
836     BLI_insertlinkafter(&block->buttons, but, oldbut);
837     /* Add the old button to the button groups in the new block. */
838     ui_button_group_replace_but_ptr(block, but, oldbut);
839     oldbut->block = block;
840     *but_p = oldbut;
841 
842     /* still stuff needs to be copied */
843     oldbut->rect = but->rect;
844     oldbut->context = but->context; /* set by Layout */
845 
846     /* drawing */
847     oldbut->icon = but->icon;
848     oldbut->iconadd = but->iconadd;
849     oldbut->alignnr = but->alignnr;
850 
851     /* typically the same pointers, but not on undo/redo */
852     /* XXX some menu buttons store button itself in but->poin. Ugly */
853     if (oldbut->poin != (char *)oldbut) {
854       SWAP(char *, oldbut->poin, but->poin);
855       SWAP(void *, oldbut->func_argN, but->func_argN);
856     }
857 
858     /* Move tooltip from new to old. */
859     SWAP(uiButToolTipFunc, oldbut->tip_func, but->tip_func);
860     SWAP(void *, oldbut->tip_argN, but->tip_argN);
861 
862     oldbut->flag = (oldbut->flag & ~flag_copy) | (but->flag & flag_copy);
863     oldbut->drawflag = (oldbut->drawflag & ~drawflag_copy) | (but->drawflag & drawflag_copy);
864 
865     ui_but_extra_icons_update_from_old_but(but, oldbut);
866     SWAP(ListBase, but->extra_op_icons, oldbut->extra_op_icons);
867 
868     if (oldbut->type == UI_BTYPE_SEARCH_MENU) {
869       uiButSearch *search_oldbut = (uiButSearch *)oldbut, *search_but = (uiButSearch *)but;
870 
871       SWAP(uiButSearchArgFreeFn, search_oldbut->arg_free_fn, search_but->arg_free_fn);
872       SWAP(void *, search_oldbut->arg, search_but->arg);
873     }
874 
875     /* copy hardmin for list rows to prevent 'sticking' highlight to mouse position
876      * when scrolling without moving mouse (see T28432) */
877     if (ELEM(oldbut->type, UI_BTYPE_ROW, UI_BTYPE_LISTROW)) {
878       oldbut->hardmax = but->hardmax;
879     }
880 
881     if (oldbut->type == UI_BTYPE_PROGRESS_BAR) {
882       uiButProgressbar *progress_oldbut = (uiButProgressbar *)oldbut;
883       uiButProgressbar *progress_but = (uiButProgressbar *)but;
884       progress_oldbut->progress = progress_but->progress;
885     }
886 
887     if (!BLI_listbase_is_empty(&block->butstore)) {
888       UI_butstore_register_update(block, oldbut, but);
889     }
890 
891     /* move/copy string from the new button to the old */
892     /* needed for alt+mouse wheel over enums */
893     if (but->str != but->strdata) {
894       if (oldbut->str != oldbut->strdata) {
895         SWAP(char *, but->str, oldbut->str);
896       }
897       else {
898         oldbut->str = but->str;
899         but->str = but->strdata;
900       }
901     }
902     else {
903       if (oldbut->str != oldbut->strdata) {
904         MEM_freeN(oldbut->str);
905         oldbut->str = oldbut->strdata;
906       }
907       BLI_strncpy(oldbut->strdata, but->strdata, sizeof(oldbut->strdata));
908     }
909 
910     if (but->dragpoin && (but->dragflag & UI_BUT_DRAGPOIN_FREE)) {
911       SWAP(void *, but->dragpoin, oldbut->dragpoin);
912     }
913 
914     BLI_remlink(&block->buttons, but);
915     ui_but_free(C, but);
916 
917     /* note: if layout hasn't been applied yet, it uses old button pointers... */
918   }
919   else {
920     const int flag_copy = UI_BUT_DRAG_MULTI;
921 
922     but->flag = (but->flag & ~flag_copy) | (oldbut->flag & flag_copy);
923 
924     /* ensures one button can get activated, and in case the buttons
925      * draw are the same this gives O(1) lookup for each button */
926     BLI_remlink(&oldblock->buttons, oldbut);
927     ui_but_free(C, oldbut);
928   }
929 
930   return found_active;
931 }
932 
933 /* needed for temporarily rename buttons, such as in outliner or file-select,
934  * they should keep calling uiDefButs to keep them alive */
935 /* returns 0 when button removed */
UI_but_active_only_ex(const bContext * C,ARegion * region,uiBlock * block,uiBut * but,const bool remove_on_failure)936 bool UI_but_active_only_ex(
937     const bContext *C, ARegion *region, uiBlock *block, uiBut *but, const bool remove_on_failure)
938 {
939   bool activate = false, found = false, isactive = false;
940 
941   uiBlock *oldblock = block->oldblock;
942   if (!oldblock) {
943     activate = true;
944   }
945   else {
946     uiBut *oldbut = ui_but_find_old(oldblock, but);
947     if (oldbut) {
948       found = true;
949 
950       if (oldbut->active) {
951         isactive = true;
952       }
953     }
954   }
955   if ((activate == true) || (found == false)) {
956     /* There might still be another active button. */
957     uiBut *old_active = ui_region_find_active_but(region);
958     if (old_active) {
959       ui_but_active_free(C, old_active);
960     }
961 
962     ui_but_activate_event((bContext *)C, region, but);
963   }
964   else if ((found == true) && (isactive == false)) {
965     if (remove_on_failure) {
966       BLI_remlink(&block->buttons, but);
967       ui_but_free(C, but);
968     }
969     return false;
970   }
971 
972   return true;
973 }
974 
UI_but_active_only(const bContext * C,ARegion * region,uiBlock * block,uiBut * but)975 bool UI_but_active_only(const bContext *C, ARegion *region, uiBlock *block, uiBut *but)
976 {
977   return UI_but_active_only_ex(C, region, block, but, true);
978 }
979 
980 /**
981  * \warning This must run after other handlers have been added,
982  * otherwise the handler wont be removed, see: T71112.
983  */
UI_block_active_only_flagged_buttons(const bContext * C,ARegion * region,uiBlock * block)984 bool UI_block_active_only_flagged_buttons(const bContext *C, ARegion *region, uiBlock *block)
985 {
986   bool done = false;
987   LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
988     if (but->flag & UI_BUT_ACTIVATE_ON_INIT) {
989       but->flag &= ~UI_BUT_ACTIVATE_ON_INIT;
990       if (ui_but_is_editable(but)) {
991         if (UI_but_active_only_ex(C, region, block, but, false)) {
992           done = true;
993           break;
994         }
995       }
996     }
997   }
998 
999   if (done) {
1000     /* Run this in a second pass since it's possible activating the button
1001      * removes the buttons being looped over. */
1002     LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
1003       but->flag &= ~UI_BUT_ACTIVATE_ON_INIT;
1004     }
1005   }
1006 
1007   return done;
1008 }
1009 
1010 /* simulate button click */
UI_but_execute(const bContext * C,ARegion * region,uiBut * but)1011 void UI_but_execute(const bContext *C, ARegion *region, uiBut *but)
1012 {
1013   void *active_back;
1014   ui_but_execute_begin((bContext *)C, region, but, &active_back);
1015   /* Value is applied in begin. No further action required. */
1016   ui_but_execute_end((bContext *)C, region, but, active_back);
1017 }
1018 
1019 /* use to check if we need to disable undo, but don't make any changes
1020  * returns false if undo needs to be disabled. */
ui_but_is_rna_undo(const uiBut * but)1021 static bool ui_but_is_rna_undo(const uiBut *but)
1022 {
1023   if (but->rnapoin.owner_id) {
1024     /* avoid undo push for buttons who's ID are screen or wm level
1025      * we could disable undo for buttons with no ID too but may have
1026      * unforeseen consequences, so best check for ID's we _know_ are not
1027      * handled by undo - campbell */
1028     ID *id = but->rnapoin.owner_id;
1029     if (ID_CHECK_UNDO(id) == false) {
1030       return false;
1031     }
1032     return true;
1033   }
1034   if (but->rnapoin.type && !RNA_struct_undo_check(but->rnapoin.type)) {
1035     return false;
1036   }
1037 
1038   return true;
1039 }
1040 
1041 /* assigns automatic keybindings to menu items for fast access
1042  * (underline key in menu) */
ui_menu_block_set_keyaccels(uiBlock * block)1043 static void ui_menu_block_set_keyaccels(uiBlock *block)
1044 {
1045   uint menu_key_mask = 0;
1046   int tot_missing = 0;
1047 
1048   /* only do it before bounding */
1049   if (block->rect.xmin != block->rect.xmax) {
1050     return;
1051   }
1052 
1053   for (int pass = 0; pass < 2; pass++) {
1054     /* 2 Passes: One for first letter only, second for any letter if the first pass fails.
1055      * Run first pass on all buttons so first word chars always get first priority. */
1056 
1057     LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
1058       if (!ELEM(but->type,
1059                 UI_BTYPE_BUT,
1060                 UI_BTYPE_BUT_MENU,
1061                 UI_BTYPE_MENU,
1062                 UI_BTYPE_BLOCK,
1063                 UI_BTYPE_PULLDOWN,
1064                 /* For PIE-menus. */
1065                 UI_BTYPE_ROW) ||
1066           (but->flag & UI_HIDDEN)) {
1067         continue;
1068       }
1069 
1070       if (but->menu_key != '\0') {
1071         continue;
1072       }
1073 
1074       if (but->str == NULL || but->str[0] == '\0') {
1075         continue;
1076       }
1077 
1078       const char *str_pt = but->str;
1079       uchar menu_key;
1080       do {
1081         menu_key = tolower(*str_pt);
1082         if ((menu_key >= 'a' && menu_key <= 'z') && !(menu_key_mask & 1 << (menu_key - 'a'))) {
1083           menu_key_mask |= 1 << (menu_key - 'a');
1084           break;
1085         }
1086 
1087         if (pass == 0) {
1088           /* Skip to next delimiter on first pass (be picky) */
1089           while (isalpha(*str_pt)) {
1090             str_pt++;
1091           }
1092 
1093           if (*str_pt) {
1094             str_pt++;
1095           }
1096         }
1097         else {
1098           /* just step over every char second pass and find first usable key */
1099           str_pt++;
1100         }
1101       } while (*str_pt);
1102 
1103       if (*str_pt) {
1104         but->menu_key = menu_key;
1105       }
1106       else {
1107         /* run second pass */
1108         tot_missing++;
1109       }
1110 
1111       /* if all keys have been used just exit, unlikely */
1112       if (menu_key_mask == (1 << 26) - 1) {
1113         return;
1114       }
1115     }
1116 
1117     /* check if second pass is needed */
1118     if (!tot_missing) {
1119       break;
1120     }
1121   }
1122 }
1123 
1124 /* XXX, this code will shorten any allocated string to 'UI_MAX_NAME_STR'
1125  * since this is really long its unlikely to be an issue,
1126  * but this could be supported */
ui_but_add_shortcut(uiBut * but,const char * shortcut_str,const bool do_strip)1127 void ui_but_add_shortcut(uiBut *but, const char *shortcut_str, const bool do_strip)
1128 {
1129   if (do_strip && (but->flag & UI_BUT_HAS_SEP_CHAR)) {
1130     char *cpoin = strrchr(but->str, UI_SEP_CHAR);
1131     if (cpoin) {
1132       *cpoin = '\0';
1133     }
1134     but->flag &= ~UI_BUT_HAS_SEP_CHAR;
1135   }
1136 
1137   /* without this, just allow stripping of the shortcut */
1138   if (shortcut_str == NULL) {
1139     return;
1140   }
1141 
1142   char *butstr_orig;
1143   if (but->str != but->strdata) {
1144     butstr_orig = but->str; /* free after using as source buffer */
1145   }
1146   else {
1147     butstr_orig = BLI_strdup(but->str);
1148   }
1149   BLI_snprintf(
1150       but->strdata, sizeof(but->strdata), "%s" UI_SEP_CHAR_S "%s", butstr_orig, shortcut_str);
1151   MEM_freeN(butstr_orig);
1152   but->str = but->strdata;
1153   but->flag |= UI_BUT_HAS_SEP_CHAR;
1154   but->drawflag |= UI_BUT_HAS_SHORTCUT;
1155   ui_but_update(but);
1156 }
1157 
1158 /* -------------------------------------------------------------------- */
1159 /** \name Find Key Shortcut for Button
1160  *
1161  * - #ui_but_event_operator_string (and helpers)
1162  * - #ui_but_event_property_operator_string
1163  * \{ */
1164 
ui_but_event_operator_string_from_operator(const bContext * C,uiBut * but,char * buf,const size_t buf_len)1165 static bool ui_but_event_operator_string_from_operator(const bContext *C,
1166                                                        uiBut *but,
1167                                                        char *buf,
1168                                                        const size_t buf_len)
1169 {
1170   BLI_assert(but->optype != NULL);
1171   bool found = false;
1172   IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
1173 
1174   if (WM_key_event_operator_string(
1175           C, but->optype->idname, but->opcontext, prop, true, buf, buf_len)) {
1176     found = true;
1177   }
1178   return found;
1179 }
1180 
ui_but_event_operator_string_from_menu(const bContext * C,uiBut * but,char * buf,const size_t buf_len)1181 static bool ui_but_event_operator_string_from_menu(const bContext *C,
1182                                                    uiBut *but,
1183                                                    char *buf,
1184                                                    const size_t buf_len)
1185 {
1186   MenuType *mt = UI_but_menutype_get(but);
1187   BLI_assert(mt != NULL);
1188 
1189   bool found = false;
1190 
1191   /* annoying, create a property */
1192   const IDPropertyTemplate val = {0};
1193   IDProperty *prop_menu = IDP_New(IDP_GROUP, &val, __func__); /* dummy, name is unimportant  */
1194   IDP_AddToGroup(prop_menu, IDP_NewString(mt->idname, "name", sizeof(mt->idname)));
1195 
1196   if (WM_key_event_operator_string(
1197           C, "WM_OT_call_menu", WM_OP_INVOKE_REGION_WIN, prop_menu, true, buf, buf_len)) {
1198     found = true;
1199   }
1200 
1201   IDP_FreeProperty(prop_menu);
1202   return found;
1203 }
1204 
ui_but_event_operator_string_from_panel(const bContext * C,uiBut * but,char * buf,const size_t buf_len)1205 static bool ui_but_event_operator_string_from_panel(const bContext *C,
1206                                                     uiBut *but,
1207                                                     char *buf,
1208                                                     const size_t buf_len)
1209 {
1210   /** Nearly exact copy of #ui_but_event_operator_string_from_menu */
1211   PanelType *pt = UI_but_paneltype_get(but);
1212   BLI_assert(pt != NULL);
1213 
1214   bool found = false;
1215 
1216   /* annoying, create a property */
1217   const IDPropertyTemplate val = {0};
1218   IDProperty *prop_panel = IDP_New(IDP_GROUP, &val, __func__); /* dummy, name is unimportant  */
1219   IDP_AddToGroup(prop_panel, IDP_NewString(pt->idname, "name", sizeof(pt->idname)));
1220   IDP_AddToGroup(prop_panel,
1221                  IDP_New(IDP_INT,
1222                          &(IDPropertyTemplate){
1223                              .i = pt->space_type,
1224                          },
1225                          "space_type"));
1226   IDP_AddToGroup(prop_panel,
1227                  IDP_New(IDP_INT,
1228                          &(IDPropertyTemplate){
1229                              .i = pt->region_type,
1230                          },
1231                          "region_type"));
1232 
1233   for (int i = 0; i < 2; i++) {
1234     /* FIXME(campbell): We can't reasonably search all configurations - long term. */
1235     IDP_ReplaceInGroup(prop_panel,
1236                        IDP_New(IDP_INT,
1237                                &(IDPropertyTemplate){
1238                                    .i = i,
1239                                },
1240                                "keep_open"));
1241     if (WM_key_event_operator_string(
1242             C, "WM_OT_call_panel", WM_OP_INVOKE_REGION_WIN, prop_panel, true, buf, buf_len)) {
1243       found = true;
1244       break;
1245     }
1246   }
1247 
1248   IDP_FreeProperty(prop_panel);
1249   return found;
1250 }
1251 
ui_but_event_operator_string(const bContext * C,uiBut * but,char * buf,const size_t buf_len)1252 static bool ui_but_event_operator_string(const bContext *C,
1253                                          uiBut *but,
1254                                          char *buf,
1255                                          const size_t buf_len)
1256 {
1257   bool found = false;
1258 
1259   if (but->optype != NULL) {
1260     found = ui_but_event_operator_string_from_operator(C, but, buf, buf_len);
1261   }
1262   else if (UI_but_menutype_get(but) != NULL) {
1263     found = ui_but_event_operator_string_from_menu(C, but, buf, buf_len);
1264   }
1265   else if (UI_but_paneltype_get(but) != NULL) {
1266     found = ui_but_event_operator_string_from_panel(C, but, buf, buf_len);
1267   }
1268 
1269   return found;
1270 }
1271 
ui_but_event_property_operator_string(const bContext * C,uiBut * but,char * buf,const size_t buf_len)1272 static bool ui_but_event_property_operator_string(const bContext *C,
1273                                                   uiBut *but,
1274                                                   char *buf,
1275                                                   const size_t buf_len)
1276 {
1277   /* context toggle operator names to check... */
1278 
1279   /* This function could use a refactor to generalize button type to operator relationship
1280    * as well as which operators use properties.
1281    * - Campbell
1282    * */
1283   const char *ctx_toggle_opnames[] = {
1284       "WM_OT_context_toggle",
1285       "WM_OT_context_toggle_enum",
1286       "WM_OT_context_cycle_int",
1287       "WM_OT_context_cycle_enum",
1288       "WM_OT_context_cycle_array",
1289       "WM_OT_context_menu_enum",
1290       NULL,
1291   };
1292 
1293   const char *ctx_enum_opnames[] = {
1294       "WM_OT_context_set_enum",
1295       NULL,
1296   };
1297 
1298   const char *ctx_enum_opnames_for_Area_ui_type[] = {
1299       "SCREEN_OT_space_type_set_or_cycle",
1300       NULL,
1301   };
1302 
1303   const char **opnames = ctx_toggle_opnames;
1304   int opnames_len = ARRAY_SIZE(ctx_toggle_opnames);
1305 
1306   int prop_enum_value = -1;
1307   bool prop_enum_value_ok = false;
1308   bool prop_enum_value_is_int = false;
1309   const char *prop_enum_value_id = "value";
1310   PointerRNA *ptr = &but->rnapoin;
1311   PropertyRNA *prop = but->rnaprop;
1312   if ((but->type == UI_BTYPE_BUT_MENU) && (but->block->handle != NULL)) {
1313     uiBut *but_parent = but->block->handle->popup_create_vars.but;
1314     if ((but->type == UI_BTYPE_BUT_MENU) && (but_parent && but_parent->rnaprop) &&
1315         (RNA_property_type(but_parent->rnaprop) == PROP_ENUM) &&
1316         ELEM(but_parent->menu_create_func,
1317              ui_def_but_rna__menu,
1318              ui_def_but_rna__panel_type,
1319              ui_def_but_rna__menu_type)) {
1320       prop_enum_value = (int)but->hardmin;
1321       ptr = &but_parent->rnapoin;
1322       prop = but_parent->rnaprop;
1323       prop_enum_value_ok = true;
1324 
1325       opnames = ctx_enum_opnames;
1326       opnames_len = ARRAY_SIZE(ctx_enum_opnames);
1327     }
1328   }
1329   /* Don't use the button again. */
1330   but = NULL;
1331 
1332   if (prop == NULL) {
1333     return false;
1334   }
1335 
1336   /* this version is only for finding hotkeys for properties
1337    * (which get set via context using operators) */
1338   /* to avoid massive slowdowns on property panels, for now, we only check the
1339    * hotkeys for Editor / Scene settings...
1340    *
1341    * TODO: userpref settings?
1342    */
1343   char *data_path = NULL;
1344 
1345   if (ptr->owner_id) {
1346     ID *id = ptr->owner_id;
1347 
1348     if (GS(id->name) == ID_SCR) {
1349       /* screen/editor property
1350        * NOTE: in most cases, there is actually no info for backwards tracing
1351        * how to get back to ID from the editor data we may be dealing with
1352        */
1353       if (RNA_struct_is_a(ptr->type, &RNA_Space)) {
1354         /* data should be directly on here... */
1355         data_path = BLI_sprintfN("space_data.%s", RNA_property_identifier(prop));
1356       }
1357       else if (RNA_struct_is_a(ptr->type, &RNA_Area)) {
1358         /* data should be directly on here... */
1359         const char *prop_id = RNA_property_identifier(prop);
1360         /* Hack since keys access 'type', UI shows 'ui_type'. */
1361         if (STREQ(prop_id, "ui_type")) {
1362           prop_id = "type";
1363           prop_enum_value >>= 16;
1364           prop = RNA_struct_find_property(ptr, prop_id);
1365 
1366           opnames = ctx_enum_opnames_for_Area_ui_type;
1367           opnames_len = ARRAY_SIZE(ctx_enum_opnames_for_Area_ui_type);
1368           prop_enum_value_id = "space_type";
1369           prop_enum_value_is_int = true;
1370         }
1371         else {
1372           data_path = BLI_sprintfN("area.%s", prop_id);
1373         }
1374       }
1375       else {
1376         /* special exceptions for common nested data in editors... */
1377         if (RNA_struct_is_a(ptr->type, &RNA_DopeSheet)) {
1378           /* dopesheet filtering options... */
1379           data_path = BLI_sprintfN("space_data.dopesheet.%s", RNA_property_identifier(prop));
1380         }
1381         else if (RNA_struct_is_a(ptr->type, &RNA_FileSelectParams)) {
1382           /* Filebrowser options... */
1383           data_path = BLI_sprintfN("space_data.params.%s", RNA_property_identifier(prop));
1384         }
1385       }
1386     }
1387     else if (GS(id->name) == ID_SCE) {
1388       if (RNA_struct_is_a(ptr->type, &RNA_ToolSettings)) {
1389         /* Tool-settings property:
1390          * NOTE: tool-settings is usually accessed directly (i.e. not through scene). */
1391         data_path = RNA_path_from_ID_to_property(ptr, prop);
1392       }
1393       else {
1394         /* scene property */
1395         char *path = RNA_path_from_ID_to_property(ptr, prop);
1396 
1397         if (path) {
1398           data_path = BLI_sprintfN("scene.%s", path);
1399           MEM_freeN(path);
1400         }
1401 #if 0
1402           else {
1403             printf("ERROR in %s(): Couldn't get path for scene property - %s\n",
1404                    __func__,
1405                    RNA_property_identifier(prop));
1406           }
1407 #endif
1408       }
1409     }
1410     else {
1411       // puts("other id");
1412     }
1413 
1414     // printf("prop shortcut: '%s' (%s)\n", RNA_property_identifier(prop), data_path);
1415   }
1416 
1417   /* we have a datapath! */
1418   bool found = false;
1419   if (data_path || (prop_enum_value_ok && prop_enum_value_id)) {
1420     /* create a property to host the "datapath" property we're sending to the operators */
1421     IDProperty *prop_path;
1422 
1423     const IDPropertyTemplate val = {0};
1424     prop_path = IDP_New(IDP_GROUP, &val, __func__);
1425     if (data_path) {
1426       IDP_AddToGroup(prop_path, IDP_NewString(data_path, "data_path", strlen(data_path) + 1));
1427     }
1428     if (prop_enum_value_ok) {
1429       const EnumPropertyItem *item;
1430       bool free;
1431       RNA_property_enum_items((bContext *)C, ptr, prop, &item, NULL, &free);
1432       const int index = RNA_enum_from_value(item, prop_enum_value);
1433       if (index != -1) {
1434         IDProperty *prop_value;
1435         if (prop_enum_value_is_int) {
1436           const int value = item[index].value;
1437           prop_value = IDP_New(IDP_INT,
1438                                &(IDPropertyTemplate){
1439                                    .i = value,
1440                                },
1441                                prop_enum_value_id);
1442         }
1443         else {
1444           const char *id = item[index].identifier;
1445           prop_value = IDP_NewString(id, prop_enum_value_id, strlen(id) + 1);
1446         }
1447         IDP_AddToGroup(prop_path, prop_value);
1448       }
1449       else {
1450         opnames_len = 0; /* Do nothing. */
1451       }
1452       if (free) {
1453         MEM_freeN((void *)item);
1454       }
1455     }
1456 
1457     /* check each until one works... */
1458 
1459     for (int i = 0; (i < opnames_len) && (opnames[i]); i++) {
1460       if (WM_key_event_operator_string(
1461               C, opnames[i], WM_OP_INVOKE_REGION_WIN, prop_path, false, buf, buf_len)) {
1462         found = true;
1463         break;
1464       }
1465     }
1466 
1467     /* cleanup */
1468     IDP_FreeProperty(prop_path);
1469     if (data_path) {
1470       MEM_freeN(data_path);
1471     }
1472   }
1473 
1474   return found;
1475 }
1476 
1477 /** \} */
1478 
1479 /**
1480  * This goes in a seemingly weird pattern:
1481  *
1482  * <pre>
1483  *     4
1484  *  5     6
1485  * 1       2
1486  *  7     8
1487  *     3
1488  * </pre>
1489  *
1490  * but it's actually quite logical. It's designed to be 'upwards compatible'
1491  * for muscle memory so that the menu item locations are fixed and don't move
1492  * as new items are added to the menu later on. It also optimizes efficiency -
1493  * a radial menu is best kept symmetrical, with as large an angle between
1494  * items as possible, so that the gestural mouse movements can be fast and inexact.
1495  *
1496  * It starts off with two opposite sides for the first two items
1497  * then joined by the one below for the third (this way, even with three items,
1498  * the menu seems to still be 'in order' reading left to right). Then the fourth is
1499  * added to complete the compass directions. From here, it's just a matter of
1500  * subdividing the rest of the angles for the last 4 items.
1501  *
1502  * --Matt 07/2006
1503  */
1504 const char ui_radial_dir_order[8] = {
1505     UI_RADIAL_W,
1506     UI_RADIAL_E,
1507     UI_RADIAL_S,
1508     UI_RADIAL_N,
1509     UI_RADIAL_NW,
1510     UI_RADIAL_NE,
1511     UI_RADIAL_SW,
1512     UI_RADIAL_SE,
1513 };
1514 
1515 const char ui_radial_dir_to_numpad[8] = {8, 9, 6, 3, 2, 1, 4, 7};
1516 const short ui_radial_dir_to_angle[8] = {90, 45, 0, 315, 270, 225, 180, 135};
1517 
ui_but_pie_direction_string(uiBut * but,char * buf,int size)1518 static void ui_but_pie_direction_string(uiBut *but, char *buf, int size)
1519 {
1520   BLI_assert(but->pie_dir < ARRAY_SIZE(ui_radial_dir_to_numpad));
1521   BLI_snprintf(buf, size, "%d", ui_radial_dir_to_numpad[but->pie_dir]);
1522 }
1523 
ui_menu_block_set_keymaps(const bContext * C,uiBlock * block)1524 static void ui_menu_block_set_keymaps(const bContext *C, uiBlock *block)
1525 {
1526   char buf[128];
1527 
1528   BLI_assert(block->flag & (UI_BLOCK_LOOP | UI_BLOCK_SHOW_SHORTCUT_ALWAYS));
1529 
1530   /* only do it before bounding */
1531   if (block->rect.xmin != block->rect.xmax) {
1532     return;
1533   }
1534   if (STREQ(block->name, "splash")) {
1535     return;
1536   }
1537 
1538   if (block->flag & UI_BLOCK_RADIAL) {
1539     LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
1540       if (but->pie_dir != UI_RADIAL_NONE) {
1541         ui_but_pie_direction_string(but, buf, sizeof(buf));
1542         ui_but_add_shortcut(but, buf, false);
1543       }
1544     }
1545   }
1546   else {
1547     LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
1548       if (block->flag & UI_BLOCK_SHOW_SHORTCUT_ALWAYS) {
1549         /* Skip icon-only buttons (as used in the toolbar). */
1550         if (but->drawstr[0] == '\0') {
1551           continue;
1552         }
1553         if (((block->flag & UI_BLOCK_POPOVER) == 0) && UI_but_is_tool(but)) {
1554           /* For non-popovers, shown in shortcut only
1555            * (has special shortcut handling code). */
1556           continue;
1557         }
1558       }
1559       else if (but->emboss != UI_EMBOSS_PULLDOWN) {
1560         continue;
1561       }
1562 
1563       if (ui_but_event_operator_string(C, but, buf, sizeof(buf))) {
1564         ui_but_add_shortcut(but, buf, false);
1565       }
1566       else if (ui_but_event_property_operator_string(C, but, buf, sizeof(buf))) {
1567         ui_but_add_shortcut(but, buf, false);
1568       }
1569     }
1570   }
1571 }
1572 
ui_but_override_flag(Main * bmain,uiBut * but)1573 void ui_but_override_flag(Main *bmain, uiBut *but)
1574 {
1575   const uint override_status = RNA_property_override_library_status(
1576       bmain, &but->rnapoin, but->rnaprop, but->rnaindex);
1577 
1578   if (override_status & RNA_OVERRIDE_STATUS_OVERRIDDEN) {
1579     but->flag |= UI_BUT_OVERRIDEN;
1580   }
1581   else {
1582     but->flag &= ~UI_BUT_OVERRIDEN;
1583   }
1584 }
1585 
1586 /** \name Button Extra Operator Icons
1587  *
1588  * Extra icons are shown on the right hand side of buttons. They can be clicked to invoke custom
1589  * operators.
1590  * There are some predefined here, which get added to buttons automatically based on button data
1591  * (type, flags, state, etc).
1592  * \{ */
1593 
1594 /**
1595  * Predefined types for generic extra operator icons (uiButExtraOpIcon).
1596  */
1597 typedef enum PredefinedExtraOpIconType {
1598   PREDEFINED_EXTRA_OP_ICON_NONE = 1,
1599   PREDEFINED_EXTRA_OP_ICON_CLEAR,
1600   PREDEFINED_EXTRA_OP_ICON_EYEDROPPER,
1601 } PredefinedExtraOpIconType;
1602 
ui_but_extra_operator_icon_add_ptr(uiBut * but,wmOperatorType * optype,short opcontext,int icon)1603 static PointerRNA *ui_but_extra_operator_icon_add_ptr(uiBut *but,
1604                                                       wmOperatorType *optype,
1605                                                       short opcontext,
1606                                                       int icon)
1607 {
1608   uiButExtraOpIcon *extra_op_icon = MEM_mallocN(sizeof(*extra_op_icon), __func__);
1609 
1610   extra_op_icon->icon = (BIFIconID)icon;
1611   extra_op_icon->optype_params = MEM_callocN(sizeof(*extra_op_icon->optype_params),
1612                                              "uiButExtraOpIcon.optype_hook");
1613   extra_op_icon->optype_params->optype = optype;
1614   extra_op_icon->optype_params->opptr = MEM_callocN(sizeof(*extra_op_icon->optype_params->opptr),
1615                                                     "uiButExtraOpIcon.optype_hook.opptr");
1616   WM_operator_properties_create_ptr(extra_op_icon->optype_params->opptr,
1617                                     extra_op_icon->optype_params->optype);
1618   extra_op_icon->optype_params->opcontext = opcontext;
1619   extra_op_icon->highlighted = false;
1620 
1621   BLI_addtail(&but->extra_op_icons, extra_op_icon);
1622 
1623   return extra_op_icon->optype_params->opptr;
1624 }
1625 
ui_but_extra_operator_icon_free(uiButExtraOpIcon * extra_icon)1626 static void ui_but_extra_operator_icon_free(uiButExtraOpIcon *extra_icon)
1627 {
1628   WM_operator_properties_free(extra_icon->optype_params->opptr);
1629   MEM_freeN(extra_icon->optype_params->opptr);
1630   MEM_freeN(extra_icon->optype_params);
1631   MEM_freeN(extra_icon);
1632 }
1633 
ui_but_extra_operator_icons_free(uiBut * but)1634 void ui_but_extra_operator_icons_free(uiBut *but)
1635 {
1636   LISTBASE_FOREACH_MUTABLE (uiButExtraOpIcon *, op_icon, &but->extra_op_icons) {
1637     ui_but_extra_operator_icon_free(op_icon);
1638   }
1639   BLI_listbase_clear(&but->extra_op_icons);
1640 }
1641 
UI_but_extra_operator_icon_add(uiBut * but,const char * opname,short opcontext,int icon)1642 PointerRNA *UI_but_extra_operator_icon_add(uiBut *but,
1643                                            const char *opname,
1644                                            short opcontext,
1645                                            int icon)
1646 {
1647   wmOperatorType *optype = WM_operatortype_find(opname, false);
1648 
1649   if (optype) {
1650     return ui_but_extra_operator_icon_add_ptr(but, optype, opcontext, icon);
1651   }
1652 
1653   return NULL;
1654 }
1655 
ui_but_icon_extra_is_visible_text_clear(const uiBut * but)1656 static bool ui_but_icon_extra_is_visible_text_clear(const uiBut *but)
1657 {
1658   BLI_assert(but->type == UI_BTYPE_TEXT);
1659   return ((but->flag & UI_BUT_VALUE_CLEAR) && but->drawstr[0]);
1660 }
1661 
ui_but_icon_extra_is_visible_search_unlink(const uiBut * but)1662 static bool ui_but_icon_extra_is_visible_search_unlink(const uiBut *but)
1663 {
1664   BLI_assert(ELEM(but->type, UI_BTYPE_SEARCH_MENU));
1665   return ((but->editstr == NULL) && (but->drawstr[0] != '\0') && (but->flag & UI_BUT_VALUE_CLEAR));
1666 }
1667 
ui_but_icon_extra_is_visible_search_eyedropper(uiBut * but)1668 static bool ui_but_icon_extra_is_visible_search_eyedropper(uiBut *but)
1669 {
1670   BLI_assert(but->type == UI_BTYPE_SEARCH_MENU && (but->flag & UI_BUT_VALUE_CLEAR));
1671 
1672   if (but->rnaprop == NULL) {
1673     return false;
1674   }
1675 
1676   StructRNA *type = RNA_property_pointer_type(&but->rnapoin, but->rnaprop);
1677   short idcode = RNA_type_to_ID_code(type);
1678 
1679   return ((but->editstr == NULL) && (idcode == ID_OB || OB_DATA_SUPPORT_ID(idcode)));
1680 }
1681 
ui_but_icon_extra_get(uiBut * but)1682 static PredefinedExtraOpIconType ui_but_icon_extra_get(uiBut *but)
1683 {
1684   switch (but->type) {
1685     case UI_BTYPE_TEXT:
1686       if (ui_but_icon_extra_is_visible_text_clear(but)) {
1687         return PREDEFINED_EXTRA_OP_ICON_CLEAR;
1688       }
1689       break;
1690     case UI_BTYPE_SEARCH_MENU:
1691       if ((but->flag & UI_BUT_VALUE_CLEAR) == 0) {
1692         /* pass */
1693       }
1694       else if (ui_but_icon_extra_is_visible_search_unlink(but)) {
1695         return PREDEFINED_EXTRA_OP_ICON_CLEAR;
1696       }
1697       else if (ui_but_icon_extra_is_visible_search_eyedropper(but)) {
1698         return PREDEFINED_EXTRA_OP_ICON_EYEDROPPER;
1699       }
1700       break;
1701     default:
1702       break;
1703   }
1704 
1705   return PREDEFINED_EXTRA_OP_ICON_NONE;
1706 }
1707 
1708 /**
1709  * While some extra operator icons have to be set explicitly upon button creating, this code adds
1710  * some generic ones based on button data. Currently these are mutually exclusive, so there's only
1711  * ever one predefined extra icon.
1712  */
ui_but_predefined_extra_operator_icons_add(uiBut * but)1713 static void ui_but_predefined_extra_operator_icons_add(uiBut *but)
1714 {
1715   const PredefinedExtraOpIconType extra_icon = ui_but_icon_extra_get(but);
1716   wmOperatorType *optype = NULL;
1717   BIFIconID icon = ICON_NONE;
1718 
1719   switch (extra_icon) {
1720     case PREDEFINED_EXTRA_OP_ICON_EYEDROPPER: {
1721       static wmOperatorType *id_eyedropper_ot = NULL;
1722       if (!id_eyedropper_ot) {
1723         id_eyedropper_ot = WM_operatortype_find("UI_OT_eyedropper_id", false);
1724       }
1725       BLI_assert(id_eyedropper_ot);
1726 
1727       optype = id_eyedropper_ot;
1728       icon = ICON_EYEDROPPER;
1729 
1730       break;
1731     }
1732     case PREDEFINED_EXTRA_OP_ICON_CLEAR: {
1733       static wmOperatorType *clear_ot = NULL;
1734       if (!clear_ot) {
1735         clear_ot = WM_operatortype_find("UI_OT_button_string_clear", false);
1736       }
1737       BLI_assert(clear_ot);
1738 
1739       optype = clear_ot;
1740       icon = ICON_PANEL_CLOSE;
1741 
1742       break;
1743     }
1744     default:
1745       break;
1746   }
1747 
1748   if (optype) {
1749     LISTBASE_FOREACH (uiButExtraOpIcon *, op_icon, &but->extra_op_icons) {
1750       if ((op_icon->optype_params->optype == optype) && (op_icon->icon == icon)) {
1751         /* Don't add the same operator icon twice (happens if button is kept alive while active).
1752          */
1753         return;
1754       }
1755     }
1756     ui_but_extra_operator_icon_add_ptr(but, optype, WM_OP_INVOKE_DEFAULT, (int)icon);
1757   }
1758 }
1759 
1760 /** \} */
1761 
UI_block_update_from_old(const bContext * C,uiBlock * block)1762 void UI_block_update_from_old(const bContext *C, uiBlock *block)
1763 {
1764   if (!block->oldblock) {
1765     return;
1766   }
1767 
1768   uiBut *but_old = block->oldblock->buttons.first;
1769 
1770   if (BLI_listbase_is_empty(&block->oldblock->butstore) == false) {
1771     UI_butstore_update(block);
1772   }
1773 
1774   LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
1775     if (ui_but_update_from_old_block(C, block, &but, &but_old)) {
1776       ui_but_update(but);
1777 
1778       /* redraw dynamic tooltip if we have one open */
1779       if (but->tip_func) {
1780         UI_but_tooltip_refresh((bContext *)C, but);
1781       }
1782     }
1783   }
1784 
1785   block->auto_open = block->oldblock->auto_open;
1786   block->auto_open_last = block->oldblock->auto_open_last;
1787   block->tooltipdisabled = block->oldblock->tooltipdisabled;
1788   BLI_movelisttolist(&block->color_pickers.list, &block->oldblock->color_pickers.list);
1789 
1790   block->oldblock = NULL;
1791 }
1792 
1793 #ifndef NDEBUG
1794 /**
1795  * Extra sanity checks for invariants (debug builds only).
1796  */
ui_but_validate(const uiBut * but)1797 static void ui_but_validate(const uiBut *but)
1798 {
1799   /* Number buttons must have a click-step,
1800    * assert instead of correcting the value to ensure the caller knows what they're doing.  */
1801   if (but->type == UI_BTYPE_NUM) {
1802     uiButNumber *number_but = (uiButNumber *)but;
1803 
1804     if (ELEM(but->pointype, UI_BUT_POIN_CHAR, UI_BUT_POIN_SHORT, UI_BUT_POIN_INT)) {
1805       BLI_assert((int)number_but->step_size > 0);
1806     }
1807   }
1808 }
1809 #endif
1810 
UI_block_end_ex(const bContext * C,uiBlock * block,const int xy[2],int r_xy[2])1811 void UI_block_end_ex(const bContext *C, uiBlock *block, const int xy[2], int r_xy[2])
1812 {
1813   wmWindow *window = CTX_wm_window(C);
1814   Scene *scene = CTX_data_scene(C);
1815   ARegion *region = CTX_wm_region(C);
1816   Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
1817 
1818   BLI_assert(block->active);
1819 
1820   /* Extend button data. This needs to be done before the block updating. */
1821   LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
1822     ui_but_predefined_extra_operator_icons_add(but);
1823   }
1824 
1825   UI_block_update_from_old(C, block);
1826 
1827   /* inherit flags from 'old' buttons that was drawn here previous, based
1828    * on matching buttons, we need this to make button event handling non
1829    * blocking, while still allowing buttons to be remade each redraw as it
1830    * is expected by blender code */
1831   LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
1832     /* temp? Proper check for graying out */
1833     if (but->optype) {
1834       wmOperatorType *ot = but->optype;
1835 
1836       if (but->context) {
1837         CTX_store_set((bContext *)C, but->context);
1838       }
1839 
1840       if (ot == NULL || WM_operator_poll_context((bContext *)C, ot, but->opcontext) == 0) {
1841         but->flag |= UI_BUT_DISABLED;
1842       }
1843 
1844       if (but->context) {
1845         CTX_store_set((bContext *)C, NULL);
1846       }
1847     }
1848 
1849     const AnimationEvalContext anim_eval_context = BKE_animsys_eval_context_construct(
1850         depsgraph, (scene) ? scene->r.cfra : 0.0f);
1851     ui_but_anim_flag(but, &anim_eval_context);
1852     ui_but_override_flag(CTX_data_main(C), but);
1853     if (UI_but_is_decorator(but)) {
1854       ui_but_anim_decorate_update_from_flag((uiButDecorator *)but);
1855     }
1856 
1857 #ifndef NDEBUG
1858     ui_but_validate(but);
1859 #endif
1860   }
1861 
1862   /* handle pending stuff */
1863   if (block->layouts.first) {
1864     UI_block_layout_resolve(block, NULL, NULL);
1865   }
1866   ui_block_align_calc(block, CTX_wm_region(C));
1867   if ((block->flag & UI_BLOCK_LOOP) && (block->flag & UI_BLOCK_NUMSELECT)) {
1868     ui_menu_block_set_keyaccels(block); /* could use a different flag to check */
1869   }
1870 
1871   if (block->flag & (UI_BLOCK_LOOP | UI_BLOCK_SHOW_SHORTCUT_ALWAYS)) {
1872     ui_menu_block_set_keymaps(C, block);
1873   }
1874 
1875   /* after keymaps! */
1876   switch (block->bounds_type) {
1877     case UI_BLOCK_BOUNDS_NONE:
1878       break;
1879     case UI_BLOCK_BOUNDS:
1880       ui_block_bounds_calc(block);
1881       break;
1882     case UI_BLOCK_BOUNDS_TEXT:
1883       ui_block_bounds_calc_text(block, 0.0f);
1884       break;
1885     case UI_BLOCK_BOUNDS_POPUP_CENTER:
1886       ui_block_bounds_calc_centered(window, block);
1887       break;
1888     case UI_BLOCK_BOUNDS_PIE_CENTER:
1889       ui_block_bounds_calc_centered_pie(block);
1890       break;
1891 
1892       /* fallback */
1893     case UI_BLOCK_BOUNDS_POPUP_MOUSE:
1894     case UI_BLOCK_BOUNDS_POPUP_MENU:
1895       ui_block_bounds_calc_popup(window, block, block->bounds_type, xy, r_xy);
1896       break;
1897   }
1898 
1899   if (block->rect.xmin == 0.0f && block->rect.xmax == 0.0f) {
1900     UI_block_bounds_set_normal(block, 0);
1901   }
1902   if (block->flag & UI_BUT_ALIGN) {
1903     UI_block_align_end(block);
1904   }
1905 
1906   ui_update_flexible_spacing(region, block);
1907 
1908   block->endblock = 1;
1909 }
1910 
UI_block_end(const bContext * C,uiBlock * block)1911 void UI_block_end(const bContext *C, uiBlock *block)
1912 {
1913   wmWindow *window = CTX_wm_window(C);
1914 
1915   UI_block_end_ex(C, block, &window->eventstate->x, NULL);
1916 }
1917 
1918 /* ************** BLOCK DRAWING FUNCTION ************* */
1919 
ui_fontscale(short * points,float aspect)1920 void ui_fontscale(short *points, float aspect)
1921 {
1922   if (aspect < 0.9f || aspect > 1.1f) {
1923     float pointsf = *points;
1924 
1925     /* for some reason scaling fonts goes too fast compared to widget size */
1926     /* XXX not true anymore? (ton) */
1927     // aspect = sqrt(aspect);
1928     pointsf /= aspect;
1929 
1930     if (aspect > 1.0f) {
1931       *points = ceilf(pointsf);
1932     }
1933     else {
1934       *points = floorf(pointsf);
1935     }
1936   }
1937 }
1938 
1939 /* project button or block (but==NULL) to pixels in regionspace */
ui_but_to_pixelrect(rcti * rect,const ARegion * region,uiBlock * block,uiBut * but)1940 static void ui_but_to_pixelrect(rcti *rect, const ARegion *region, uiBlock *block, uiBut *but)
1941 {
1942   rctf rectf;
1943 
1944   ui_block_to_window_rctf(region, block, &rectf, (but) ? &but->rect : &block->rect);
1945   BLI_rcti_rctf_copy_round(rect, &rectf);
1946   BLI_rcti_translate(rect, -region->winrct.xmin, -region->winrct.ymin);
1947 }
1948 
1949 /* uses local copy of style, to scale things down, and allow widgets to change stuff */
UI_block_draw(const bContext * C,uiBlock * block)1950 void UI_block_draw(const bContext *C, uiBlock *block)
1951 {
1952   uiStyle style = *UI_style_get_dpi(); /* XXX pass on as arg */
1953 
1954   /* get menu region or area region */
1955   ARegion *region = CTX_wm_menu(C);
1956   if (!region) {
1957     region = CTX_wm_region(C);
1958   }
1959 
1960   if (!block->endblock) {
1961     UI_block_end(C, block);
1962   }
1963 
1964   /* we set this only once */
1965   GPU_blend(GPU_BLEND_ALPHA);
1966 
1967   /* scale fonts */
1968   ui_fontscale(&style.paneltitle.points, block->aspect);
1969   ui_fontscale(&style.grouplabel.points, block->aspect);
1970   ui_fontscale(&style.widgetlabel.points, block->aspect);
1971   ui_fontscale(&style.widget.points, block->aspect);
1972 
1973   /* scale block min/max to rect */
1974   rcti rect;
1975   ui_but_to_pixelrect(&rect, region, block, NULL);
1976 
1977   /* pixel space for AA widgets */
1978   GPU_matrix_push_projection();
1979   GPU_matrix_push();
1980   GPU_matrix_identity_set();
1981 
1982   wmOrtho2_region_pixelspace(region);
1983 
1984   /* back */
1985   if (block->flag & UI_BLOCK_RADIAL) {
1986     ui_draw_pie_center(block);
1987   }
1988   else if (block->flag & UI_BLOCK_POPOVER) {
1989     ui_draw_popover_back(region, &style, block, &rect);
1990   }
1991   else if (block->flag & UI_BLOCK_LOOP) {
1992     ui_draw_menu_back(&style, block, &rect);
1993   }
1994   else if (block->panel) {
1995     bool show_background = region->alignment != RGN_ALIGN_FLOAT;
1996     if (show_background) {
1997       if (block->panel->type && (block->panel->type->flag & PNL_NO_HEADER)) {
1998         if (region->regiontype == RGN_TYPE_TOOLS) {
1999           /* We never want a background around active tools. */
2000           show_background = false;
2001         }
2002         else {
2003           /* Without a header there is no background except for region overlap. */
2004           show_background = region->overlap != 0;
2005         }
2006       }
2007     }
2008     ui_draw_aligned_panel(&style,
2009                           block,
2010                           &rect,
2011                           UI_panel_category_is_visible(region),
2012                           show_background,
2013                           region->flag & RGN_FLAG_SEARCH_FILTER_ACTIVE);
2014   }
2015 
2016   BLF_batch_draw_begin();
2017   UI_icon_draw_cache_begin();
2018   UI_widgetbase_draw_cache_begin();
2019 
2020   /* widgets */
2021   LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
2022     if (!(but->flag & (UI_HIDDEN | UI_SCROLLED))) {
2023       ui_but_to_pixelrect(&rect, region, block, but);
2024 
2025       /* XXX: figure out why invalid coordinates happen when closing render window */
2026       /* and material preview is redrawn in main window (temp fix for bug T23848) */
2027       if (rect.xmin < rect.xmax && rect.ymin < rect.ymax) {
2028         ui_draw_but(C, region, &style, but, &rect);
2029       }
2030     }
2031   }
2032 
2033   UI_widgetbase_draw_cache_end();
2034   UI_icon_draw_cache_end();
2035   BLF_batch_draw_end();
2036 
2037   /* restore matrix */
2038   GPU_matrix_pop_projection();
2039   GPU_matrix_pop();
2040 }
2041 
ui_block_message_subscribe(ARegion * region,struct wmMsgBus * mbus,uiBlock * block)2042 static void ui_block_message_subscribe(ARegion *region, struct wmMsgBus *mbus, uiBlock *block)
2043 {
2044   uiBut *but_prev = NULL;
2045   /* possibly we should keep the region this block is contained in? */
2046   LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
2047     if (but->rnapoin.type && but->rnaprop) {
2048       /* quick check to avoid adding buttons representing a vector, multiple times. */
2049       if ((but_prev && (but_prev->rnaprop == but->rnaprop) &&
2050            (but_prev->rnapoin.type == but->rnapoin.type) &&
2051            (but_prev->rnapoin.data == but->rnapoin.data) &&
2052            (but_prev->rnapoin.owner_id == but->rnapoin.owner_id)) == false) {
2053         /* TODO: could make this into utility function. */
2054         WM_msg_subscribe_rna(mbus,
2055                              &but->rnapoin,
2056                              but->rnaprop,
2057                              &(const wmMsgSubscribeValue){
2058                                  .owner = region,
2059                                  .user_data = region,
2060                                  .notify = ED_region_do_msg_notify_tag_redraw,
2061                              },
2062                              __func__);
2063         but_prev = but;
2064       }
2065     }
2066   }
2067 }
2068 
UI_region_message_subscribe(ARegion * region,struct wmMsgBus * mbus)2069 void UI_region_message_subscribe(ARegion *region, struct wmMsgBus *mbus)
2070 {
2071   LISTBASE_FOREACH (uiBlock *, block, &region->uiblocks) {
2072     ui_block_message_subscribe(region, mbus, block);
2073   }
2074 }
2075 
2076 /* ************* EVENTS ************* */
2077 
2078 /**
2079  * Check if the button is pushed, this is only meaningful for some button types.
2080  *
2081  * \return (0 == UNSELECT), (1 == SELECT), (-1 == DO-NOTHING)
2082  */
ui_but_is_pushed_ex(uiBut * but,double * value)2083 int ui_but_is_pushed_ex(uiBut *but, double *value)
2084 {
2085   int is_push = 0;
2086 
2087   if (but->bit) {
2088     const bool state = !ELEM(
2089         but->type, UI_BTYPE_TOGGLE_N, UI_BTYPE_ICON_TOGGLE_N, UI_BTYPE_CHECKBOX_N);
2090     int lvalue;
2091     UI_GET_BUT_VALUE_INIT(but, *value);
2092     lvalue = (int)*value;
2093     if (UI_BITBUT_TEST(lvalue, (but->bitnr))) {
2094       is_push = state;
2095     }
2096     else {
2097       is_push = !state;
2098     }
2099   }
2100   else {
2101     switch (but->type) {
2102       case UI_BTYPE_BUT:
2103       case UI_BTYPE_HOTKEY_EVENT:
2104       case UI_BTYPE_KEY_EVENT:
2105       case UI_BTYPE_COLOR:
2106       case UI_BTYPE_DECORATOR:
2107         is_push = -1;
2108         break;
2109       case UI_BTYPE_BUT_TOGGLE:
2110       case UI_BTYPE_TOGGLE:
2111       case UI_BTYPE_ICON_TOGGLE:
2112       case UI_BTYPE_CHECKBOX:
2113         UI_GET_BUT_VALUE_INIT(but, *value);
2114         if (*value != (double)but->hardmin) {
2115           is_push = true;
2116         }
2117         break;
2118       case UI_BTYPE_ICON_TOGGLE_N:
2119       case UI_BTYPE_TOGGLE_N:
2120       case UI_BTYPE_CHECKBOX_N:
2121         UI_GET_BUT_VALUE_INIT(but, *value);
2122         if (*value == 0.0) {
2123           is_push = true;
2124         }
2125         break;
2126       case UI_BTYPE_ROW:
2127       case UI_BTYPE_LISTROW:
2128       case UI_BTYPE_TAB:
2129         if ((but->type == UI_BTYPE_TAB) && but->rnaprop && but->custom_data) {
2130           /* uiBut.custom_data points to data this tab represents (e.g. workspace).
2131            * uiBut.rnapoin/prop store an active value (e.g. active workspace). */
2132           if (RNA_property_type(but->rnaprop) == PROP_POINTER) {
2133             const PointerRNA active_ptr = RNA_property_pointer_get(&but->rnapoin, but->rnaprop);
2134             if (active_ptr.data == but->custom_data) {
2135               is_push = true;
2136             }
2137           }
2138           break;
2139         }
2140         else if (but->optype) {
2141           break;
2142         }
2143 
2144         UI_GET_BUT_VALUE_INIT(but, *value);
2145         /* support for rna enum buts */
2146         if (but->rnaprop && (RNA_property_flag(but->rnaprop) & PROP_ENUM_FLAG)) {
2147           if ((int)*value & (int)but->hardmax) {
2148             is_push = true;
2149           }
2150         }
2151         else {
2152           if (*value == (double)but->hardmax) {
2153             is_push = true;
2154           }
2155         }
2156         break;
2157       default:
2158         is_push = -1;
2159         break;
2160     }
2161   }
2162 
2163   if ((but->drawflag & UI_BUT_CHECKBOX_INVERT) && (is_push != -1)) {
2164     is_push = !((bool)is_push);
2165   }
2166   return is_push;
2167 }
ui_but_is_pushed(uiBut * but)2168 int ui_but_is_pushed(uiBut *but)
2169 {
2170   double value = UI_BUT_VALUE_UNSET;
2171   return ui_but_is_pushed_ex(but, &value);
2172 }
2173 
ui_but_update_select_flag(uiBut * but,double * value)2174 static void ui_but_update_select_flag(uiBut *but, double *value)
2175 {
2176   switch (ui_but_is_pushed_ex(but, value)) {
2177     case true:
2178       but->flag |= UI_SELECT;
2179       break;
2180     case false:
2181       but->flag &= ~UI_SELECT;
2182       break;
2183   }
2184 }
2185 
2186 /* ************************************************ */
2187 
UI_block_lock_set(uiBlock * block,bool val,const char * lockstr)2188 void UI_block_lock_set(uiBlock *block, bool val, const char *lockstr)
2189 {
2190   if (val) {
2191     block->lock = val;
2192     block->lockstr = lockstr;
2193   }
2194 }
2195 
UI_block_lock_clear(uiBlock * block)2196 void UI_block_lock_clear(uiBlock *block)
2197 {
2198   block->lock = false;
2199   block->lockstr = NULL;
2200 }
2201 
2202 /* *********************** data get/set ***********************
2203  * this either works with the pointed to data, or can work with
2204  * an edit override pointer while dragging for example */
2205 
2206 /* for buttons pointing to color for example */
ui_but_v3_get(uiBut * but,float vec[3])2207 void ui_but_v3_get(uiBut *but, float vec[3])
2208 {
2209   if (but->editvec) {
2210     copy_v3_v3(vec, but->editvec);
2211   }
2212 
2213   if (but->rnaprop) {
2214     PropertyRNA *prop = but->rnaprop;
2215 
2216     zero_v3(vec);
2217 
2218     if (RNA_property_type(prop) == PROP_FLOAT) {
2219       int tot = RNA_property_array_length(&but->rnapoin, prop);
2220       BLI_assert(tot > 0);
2221       if (tot == 3) {
2222         RNA_property_float_get_array(&but->rnapoin, prop, vec);
2223       }
2224       else {
2225         tot = min_ii(tot, 3);
2226         for (int a = 0; a < tot; a++) {
2227           vec[a] = RNA_property_float_get_index(&but->rnapoin, prop, a);
2228         }
2229       }
2230     }
2231   }
2232   else if (but->pointype == UI_BUT_POIN_CHAR) {
2233     const char *cp = (char *)but->poin;
2234 
2235     vec[0] = ((float)cp[0]) / 255.0f;
2236     vec[1] = ((float)cp[1]) / 255.0f;
2237     vec[2] = ((float)cp[2]) / 255.0f;
2238   }
2239   else if (but->pointype == UI_BUT_POIN_FLOAT) {
2240     const float *fp = (float *)but->poin;
2241     copy_v3_v3(vec, fp);
2242   }
2243   else {
2244     if (but->editvec == NULL) {
2245       fprintf(stderr, "%s: can't get color, should never happen\n", __func__);
2246       zero_v3(vec);
2247     }
2248   }
2249 
2250   if (but->type == UI_BTYPE_UNITVEC) {
2251     normalize_v3(vec);
2252   }
2253 }
2254 
2255 /* for buttons pointing to color for example */
ui_but_v3_set(uiBut * but,const float vec[3])2256 void ui_but_v3_set(uiBut *but, const float vec[3])
2257 {
2258   if (but->editvec) {
2259     copy_v3_v3(but->editvec, vec);
2260   }
2261 
2262   if (but->rnaprop) {
2263     PropertyRNA *prop = but->rnaprop;
2264 
2265     if (RNA_property_type(prop) == PROP_FLOAT) {
2266       int tot;
2267       int a;
2268 
2269       tot = RNA_property_array_length(&but->rnapoin, prop);
2270       BLI_assert(tot > 0);
2271       if (tot == 3) {
2272         RNA_property_float_set_array(&but->rnapoin, prop, vec);
2273       }
2274       else {
2275         tot = min_ii(tot, 3);
2276         for (a = 0; a < tot; a++) {
2277           RNA_property_float_set_index(&but->rnapoin, prop, a, vec[a]);
2278         }
2279       }
2280     }
2281   }
2282   else if (but->pointype == UI_BUT_POIN_CHAR) {
2283     char *cp = (char *)but->poin;
2284     cp[0] = (char)(0.5f + vec[0] * 255.0f);
2285     cp[1] = (char)(0.5f + vec[1] * 255.0f);
2286     cp[2] = (char)(0.5f + vec[2] * 255.0f);
2287   }
2288   else if (but->pointype == UI_BUT_POIN_FLOAT) {
2289     float *fp = (float *)but->poin;
2290     copy_v3_v3(fp, vec);
2291   }
2292 }
2293 
ui_but_is_float(const uiBut * but)2294 bool ui_but_is_float(const uiBut *but)
2295 {
2296   if (but->pointype == UI_BUT_POIN_FLOAT && but->poin) {
2297     return true;
2298   }
2299 
2300   if (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_FLOAT) {
2301     return true;
2302   }
2303 
2304   return false;
2305 }
2306 
ui_but_is_bool(const uiBut * but)2307 bool ui_but_is_bool(const uiBut *but)
2308 {
2309   if (ELEM(but->type,
2310            UI_BTYPE_TOGGLE,
2311            UI_BTYPE_TOGGLE_N,
2312            UI_BTYPE_ICON_TOGGLE,
2313            UI_BTYPE_ICON_TOGGLE_N,
2314            UI_BTYPE_TAB)) {
2315     return true;
2316   }
2317 
2318   if (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_BOOLEAN) {
2319     return true;
2320   }
2321 
2322   if ((but->rnaprop && RNA_property_type(but->rnaprop) == PROP_ENUM) &&
2323       (but->type == UI_BTYPE_ROW)) {
2324     return true;
2325   }
2326 
2327   return false;
2328 }
2329 
ui_but_is_unit(const uiBut * but)2330 bool ui_but_is_unit(const uiBut *but)
2331 {
2332   UnitSettings *unit = but->block->unit;
2333   const int unit_type = UI_but_unit_type_get(but);
2334 
2335   if (unit_type == PROP_UNIT_NONE) {
2336     return false;
2337   }
2338 
2339 #if 1 /* removed so angle buttons get correct snapping */
2340   if (ui_but_is_unit_radians_ex(unit, unit_type)) {
2341     return false;
2342   }
2343 #endif
2344 
2345   /* for now disable time unit conversion */
2346   if (unit_type == PROP_UNIT_TIME) {
2347     return false;
2348   }
2349 
2350   if (unit->system == USER_UNIT_NONE) {
2351     if (unit_type != PROP_UNIT_ROTATION) {
2352       return false;
2353     }
2354   }
2355 
2356   return true;
2357 }
2358 
2359 /**
2360  * Check if this button is similar enough to be grouped with another.
2361  */
ui_but_is_compatible(const uiBut * but_a,const uiBut * but_b)2362 bool ui_but_is_compatible(const uiBut *but_a, const uiBut *but_b)
2363 {
2364   if (but_a->type != but_b->type) {
2365     return false;
2366   }
2367   if (but_a->pointype != but_b->pointype) {
2368     return false;
2369   }
2370 
2371   if (but_a->rnaprop) {
2372     /* skip 'rnapoin.data', 'rnapoin.owner_id'
2373      * allow different data to have the same props edited at once */
2374     if (but_a->rnapoin.type != but_b->rnapoin.type) {
2375       return false;
2376     }
2377     if (RNA_property_type(but_a->rnaprop) != RNA_property_type(but_b->rnaprop)) {
2378       return false;
2379     }
2380     if (RNA_property_subtype(but_a->rnaprop) != RNA_property_subtype(but_b->rnaprop)) {
2381       return false;
2382     }
2383   }
2384 
2385   return true;
2386 }
2387 
ui_but_is_rna_valid(uiBut * but)2388 bool ui_but_is_rna_valid(uiBut *but)
2389 {
2390   if (but->rnaprop == NULL || RNA_struct_contains_property(&but->rnapoin, but->rnaprop)) {
2391     return true;
2392   }
2393   printf("property removed %s: %p\n", but->drawstr, but->rnaprop);
2394   return false;
2395 }
2396 
2397 /**
2398  * Checks if the button supports cycling next/previous menu items (ctrl+mouse-wheel).
2399  */
ui_but_supports_cycling(const uiBut * but)2400 bool ui_but_supports_cycling(const uiBut *but)
2401 {
2402   return ((ELEM(but->type, UI_BTYPE_ROW, UI_BTYPE_NUM, UI_BTYPE_NUM_SLIDER, UI_BTYPE_LISTBOX)) ||
2403           (but->type == UI_BTYPE_MENU && ui_but_menu_step_poll(but)) ||
2404           (but->type == UI_BTYPE_COLOR && ((uiButColor *)but)->is_pallete_color) ||
2405           (but->menu_step_func != NULL));
2406 }
2407 
ui_but_value_get(uiBut * but)2408 double ui_but_value_get(uiBut *but)
2409 {
2410   double value = 0.0;
2411 
2412   if (but->editval) {
2413     return *(but->editval);
2414   }
2415   if (but->poin == NULL && but->rnapoin.data == NULL) {
2416     return 0.0;
2417   }
2418 
2419   if (but->rnaprop) {
2420     PropertyRNA *prop = but->rnaprop;
2421 
2422     BLI_assert(but->rnaindex != -1);
2423 
2424     switch (RNA_property_type(prop)) {
2425       case PROP_BOOLEAN:
2426         if (RNA_property_array_check(prop)) {
2427           value = RNA_property_boolean_get_index(&but->rnapoin, prop, but->rnaindex);
2428         }
2429         else {
2430           value = RNA_property_boolean_get(&but->rnapoin, prop);
2431         }
2432         break;
2433       case PROP_INT:
2434         if (RNA_property_array_check(prop)) {
2435           value = RNA_property_int_get_index(&but->rnapoin, prop, but->rnaindex);
2436         }
2437         else {
2438           value = RNA_property_int_get(&but->rnapoin, prop);
2439         }
2440         break;
2441       case PROP_FLOAT:
2442         if (RNA_property_array_check(prop)) {
2443           value = RNA_property_float_get_index(&but->rnapoin, prop, but->rnaindex);
2444         }
2445         else {
2446           value = RNA_property_float_get(&but->rnapoin, prop);
2447         }
2448         break;
2449       case PROP_ENUM:
2450         value = RNA_property_enum_get(&but->rnapoin, prop);
2451         break;
2452       default:
2453         value = 0.0;
2454         break;
2455     }
2456   }
2457   else if (but->pointype == UI_BUT_POIN_CHAR) {
2458     value = *(char *)but->poin;
2459   }
2460   else if (but->pointype == UI_BUT_POIN_SHORT) {
2461     value = *(short *)but->poin;
2462   }
2463   else if (but->pointype == UI_BUT_POIN_INT) {
2464     value = *(int *)but->poin;
2465   }
2466   else if (but->pointype == UI_BUT_POIN_FLOAT) {
2467     value = *(float *)but->poin;
2468   }
2469 
2470   return value;
2471 }
2472 
ui_but_value_set(uiBut * but,double value)2473 void ui_but_value_set(uiBut *but, double value)
2474 {
2475   /* value is a hsv value: convert to rgb */
2476   if (but->rnaprop) {
2477     PropertyRNA *prop = but->rnaprop;
2478 
2479     if (RNA_property_editable(&but->rnapoin, prop)) {
2480       switch (RNA_property_type(prop)) {
2481         case PROP_BOOLEAN:
2482           if (RNA_property_array_check(prop)) {
2483             RNA_property_boolean_set_index(&but->rnapoin, prop, but->rnaindex, value);
2484           }
2485           else {
2486             RNA_property_boolean_set(&but->rnapoin, prop, value);
2487           }
2488           break;
2489         case PROP_INT:
2490           if (RNA_property_array_check(prop)) {
2491             RNA_property_int_set_index(&but->rnapoin, prop, but->rnaindex, (int)value);
2492           }
2493           else {
2494             RNA_property_int_set(&but->rnapoin, prop, (int)value);
2495           }
2496           break;
2497         case PROP_FLOAT:
2498           if (RNA_property_array_check(prop)) {
2499             RNA_property_float_set_index(&but->rnapoin, prop, but->rnaindex, value);
2500           }
2501           else {
2502             RNA_property_float_set(&but->rnapoin, prop, value);
2503           }
2504           break;
2505         case PROP_ENUM:
2506           if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
2507             int ivalue = (int)value;
2508             /* toggle for enum/flag buttons */
2509             ivalue ^= RNA_property_enum_get(&but->rnapoin, prop);
2510             RNA_property_enum_set(&but->rnapoin, prop, ivalue);
2511           }
2512           else {
2513             RNA_property_enum_set(&but->rnapoin, prop, value);
2514           }
2515           break;
2516         default:
2517           break;
2518       }
2519     }
2520 
2521     /* we can't be sure what RNA set functions actually do,
2522      * so leave this unset */
2523     value = UI_BUT_VALUE_UNSET;
2524   }
2525   else if (but->pointype == 0) {
2526     /* pass */
2527   }
2528   else {
2529     /* first do rounding */
2530     if (but->pointype == UI_BUT_POIN_CHAR) {
2531       value = round_db_to_uchar_clamp(value);
2532     }
2533     else if (but->pointype == UI_BUT_POIN_SHORT) {
2534       value = round_db_to_short_clamp(value);
2535     }
2536     else if (but->pointype == UI_BUT_POIN_INT) {
2537       value = round_db_to_int_clamp(value);
2538     }
2539     else if (but->pointype == UI_BUT_POIN_FLOAT) {
2540       float fval = (float)value;
2541       if (fval >= -0.00001f && fval <= 0.00001f) {
2542         /* prevent negative zero */
2543         fval = 0.0f;
2544       }
2545       value = fval;
2546     }
2547 
2548     /* then set value with possible edit override */
2549     if (but->editval) {
2550       value = *but->editval = value;
2551     }
2552     else if (but->pointype == UI_BUT_POIN_CHAR) {
2553       value = *((char *)but->poin) = (char)value;
2554     }
2555     else if (but->pointype == UI_BUT_POIN_SHORT) {
2556       value = *((short *)but->poin) = (short)value;
2557     }
2558     else if (but->pointype == UI_BUT_POIN_INT) {
2559       value = *((int *)but->poin) = (int)value;
2560     }
2561     else if (but->pointype == UI_BUT_POIN_FLOAT) {
2562       value = *((float *)but->poin) = (float)value;
2563     }
2564   }
2565 
2566   ui_but_update_select_flag(but, &value);
2567 }
2568 
ui_but_string_get_max_length(uiBut * but)2569 int ui_but_string_get_max_length(uiBut *but)
2570 {
2571   if (ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU)) {
2572     return but->hardmax;
2573   }
2574   return UI_MAX_DRAW_STR;
2575 }
2576 
ui_but_drag_multi_edit_get(uiBut * but)2577 uiBut *ui_but_drag_multi_edit_get(uiBut *but)
2578 {
2579   uiBut *return_but = NULL;
2580 
2581   BLI_assert(but->flag & UI_BUT_DRAG_MULTI);
2582 
2583   LISTBASE_FOREACH (uiBut *, but_iter, &but->block->buttons) {
2584     if (but_iter->editstr) {
2585       return_but = but_iter;
2586       break;
2587     }
2588   }
2589 
2590   return return_but;
2591 }
2592 
ui_get_but_scale_unit(uiBut * but,double value)2593 static double ui_get_but_scale_unit(uiBut *but, double value)
2594 {
2595   UnitSettings *unit = but->block->unit;
2596   const int unit_type = UI_but_unit_type_get(but);
2597 
2598   /* Time unit is a bit special, not handled by BKE_scene_unit_scale() for now. */
2599   if (unit_type == PROP_UNIT_TIME) { /* WARNING - using evil_C :| */
2600     Scene *scene = CTX_data_scene(but->block->evil_C);
2601     return FRA2TIME(value);
2602   }
2603   return BKE_scene_unit_scale(unit, RNA_SUBTYPE_UNIT_VALUE(unit_type), value);
2604 }
2605 
2606 /* str will be overwritten */
ui_but_convert_to_unit_alt_name(uiBut * but,char * str,size_t maxlen)2607 void ui_but_convert_to_unit_alt_name(uiBut *but, char *str, size_t maxlen)
2608 {
2609   if (!ui_but_is_unit(but)) {
2610     return;
2611   }
2612 
2613   UnitSettings *unit = but->block->unit;
2614   const int unit_type = UI_but_unit_type_get(but);
2615   char *orig_str;
2616 
2617   orig_str = BLI_strdup(str);
2618 
2619   BKE_unit_name_to_alt(str, maxlen, orig_str, unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type));
2620 
2621   MEM_freeN(orig_str);
2622 }
2623 
2624 /**
2625  * \param float_precision: Override the button precision.
2626  */
ui_get_but_string_unit(uiBut * but,char * str,int len_max,double value,bool pad,int float_precision)2627 static void ui_get_but_string_unit(
2628     uiBut *but, char *str, int len_max, double value, bool pad, int float_precision)
2629 {
2630   UnitSettings *unit = but->block->unit;
2631   const int unit_type = UI_but_unit_type_get(but);
2632   int precision;
2633 
2634   if (unit->scale_length < 0.0001f) {
2635     unit->scale_length = 1.0f; /* XXX do_versions */
2636   }
2637 
2638   /* Use precision override? */
2639   if (float_precision == -1) {
2640     /* Sanity checks */
2641     precision = (int)ui_but_get_float_precision(but);
2642     if (precision > UI_PRECISION_FLOAT_MAX) {
2643       precision = UI_PRECISION_FLOAT_MAX;
2644     }
2645     else if (precision == -1) {
2646       precision = 2;
2647     }
2648   }
2649   else {
2650     precision = float_precision;
2651   }
2652 
2653   BKE_unit_value_as_string(str,
2654                            len_max,
2655                            ui_get_but_scale_unit(but, value),
2656                            precision,
2657                            RNA_SUBTYPE_UNIT_VALUE(unit_type),
2658                            unit,
2659                            pad);
2660 }
2661 
ui_get_but_step_unit(uiBut * but,float step_default)2662 static float ui_get_but_step_unit(uiBut *but, float step_default)
2663 {
2664   const int unit_type = RNA_SUBTYPE_UNIT_VALUE(UI_but_unit_type_get(but));
2665   const double step_orig = step_default * UI_PRECISION_FLOAT_SCALE;
2666   /* Scaling up 'step_origg ' here is a bit arbitrary,
2667    * its just giving better scales from user POV */
2668   const double scale_step = ui_get_but_scale_unit(but, step_orig * 10);
2669   const double step = BKE_unit_closest_scalar(scale_step, but->block->unit->system, unit_type);
2670 
2671   /* -1 is an error value */
2672   if (step == -1.0f) {
2673     return step_default;
2674   }
2675 
2676   const double scale_unit = ui_get_but_scale_unit(but, 1.0);
2677   const double step_unit = BKE_unit_closest_scalar(
2678       scale_unit, but->block->unit->system, unit_type);
2679   double step_final;
2680 
2681   BLI_assert(step > 0.0);
2682 
2683   step_final = (step / scale_unit) / (double)UI_PRECISION_FLOAT_SCALE;
2684 
2685   if (step == step_unit) {
2686     /* Logic here is to scale by the original 'step_orig'
2687      * only when the unit step matches the scaled step.
2688      *
2689      * This is needed for units that don't have a wide range of scales (degrees for eg.).
2690      * Without this we can't select between a single degree, or a 10th of a degree.
2691      */
2692     step_final *= step_orig;
2693   }
2694 
2695   return (float)step_final;
2696 }
2697 
2698 /**
2699  * \param float_precision: For number buttons the precision
2700  * to use or -1 to fallback to the button default.
2701  * \param use_exp_float: Use exponent representation of floats
2702  * when out of reasonable range (outside of 1e3/1e-3).
2703  */
ui_but_string_get_ex(uiBut * but,char * str,const size_t maxlen,const int float_precision,const bool use_exp_float,bool * r_use_exp_float)2704 void ui_but_string_get_ex(uiBut *but,
2705                           char *str,
2706                           const size_t maxlen,
2707                           const int float_precision,
2708                           const bool use_exp_float,
2709                           bool *r_use_exp_float)
2710 {
2711   if (r_use_exp_float) {
2712     *r_use_exp_float = false;
2713   }
2714 
2715   if (but->rnaprop && ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU, UI_BTYPE_TAB)) {
2716     PropertyType type = RNA_property_type(but->rnaprop);
2717 
2718     int buf_len;
2719     const char *buf = NULL;
2720     if ((but->type == UI_BTYPE_TAB) && (but->custom_data)) {
2721       StructRNA *ptr_type = RNA_property_pointer_type(&but->rnapoin, but->rnaprop);
2722       PointerRNA ptr;
2723 
2724       /* uiBut.custom_data points to data this tab represents (e.g. workspace).
2725        * uiBut.rnapoin/prop store an active value (e.g. active workspace). */
2726       RNA_pointer_create(but->rnapoin.owner_id, ptr_type, but->custom_data, &ptr);
2727       buf = RNA_struct_name_get_alloc(&ptr, str, maxlen, &buf_len);
2728     }
2729     else if (type == PROP_STRING) {
2730       /* RNA string */
2731       buf = RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, str, maxlen, &buf_len);
2732     }
2733     else if (type == PROP_ENUM) {
2734       /* RNA enum */
2735       const int value = RNA_property_enum_get(&but->rnapoin, but->rnaprop);
2736       if (RNA_property_enum_name(but->block->evil_C, &but->rnapoin, but->rnaprop, value, &buf)) {
2737         BLI_strncpy(str, buf, maxlen);
2738         buf = str;
2739       }
2740     }
2741     else if (type == PROP_POINTER) {
2742       /* RNA pointer */
2743       PointerRNA ptr = RNA_property_pointer_get(&but->rnapoin, but->rnaprop);
2744       buf = RNA_struct_name_get_alloc(&ptr, str, maxlen, &buf_len);
2745     }
2746     else {
2747       BLI_assert(0);
2748     }
2749 
2750     if (buf == NULL) {
2751       str[0] = '\0';
2752     }
2753     else if (buf != str) {
2754       BLI_assert(maxlen <= buf_len + 1);
2755       /* string was too long, we have to truncate */
2756       if (UI_but_is_utf8(but)) {
2757         BLI_strncpy_utf8(str, buf, maxlen);
2758       }
2759       else {
2760         BLI_strncpy(str, buf, maxlen);
2761       }
2762       MEM_freeN((void *)buf);
2763     }
2764   }
2765   else if (ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU)) {
2766     /* string */
2767     BLI_strncpy(str, but->poin, maxlen);
2768     return;
2769   }
2770   else if (ui_but_anim_expression_get(but, str, maxlen)) {
2771     /* driver expression */
2772   }
2773   else {
2774     /* number editing */
2775     double value = ui_but_value_get(but);
2776 
2777     PropertySubType subtype = PROP_NONE;
2778     if (but->rnaprop) {
2779       subtype = RNA_property_subtype(but->rnaprop);
2780     }
2781 
2782     if (ui_but_is_float(but)) {
2783       int prec = (float_precision == -1) ? ui_but_calc_float_precision(but, value) :
2784                                            float_precision;
2785 
2786       if (ui_but_is_unit(but)) {
2787         ui_get_but_string_unit(but, str, maxlen, value, false, prec);
2788       }
2789       else if (subtype == PROP_FACTOR) {
2790         if (U.factor_display_type == USER_FACTOR_AS_FACTOR) {
2791           BLI_snprintf(str, maxlen, "%.*f", prec, value);
2792         }
2793         else {
2794           BLI_snprintf(str, maxlen, "%.*f", MAX2(0, prec - 2), value * 100);
2795         }
2796       }
2797       else {
2798         const int int_digits_num = integer_digits_f(value);
2799         if (use_exp_float) {
2800           if (int_digits_num < -6 || int_digits_num > 12) {
2801             BLI_snprintf(str, maxlen, "%.*g", prec, value);
2802             if (r_use_exp_float) {
2803               *r_use_exp_float = true;
2804             }
2805           }
2806           else {
2807             prec -= int_digits_num;
2808             CLAMP(prec, 0, UI_PRECISION_FLOAT_MAX);
2809             BLI_snprintf(str, maxlen, "%.*f", prec, value);
2810           }
2811         }
2812         else {
2813           prec -= int_digits_num;
2814           CLAMP(prec, 0, UI_PRECISION_FLOAT_MAX);
2815           BLI_snprintf(str, maxlen, "%.*f", prec, value);
2816         }
2817       }
2818     }
2819     else {
2820       BLI_snprintf(str, maxlen, "%d", (int)value);
2821     }
2822   }
2823 }
ui_but_string_get(uiBut * but,char * str,const size_t maxlen)2824 void ui_but_string_get(uiBut *but, char *str, const size_t maxlen)
2825 {
2826   ui_but_string_get_ex(but, str, maxlen, -1, false, NULL);
2827 }
2828 
2829 /**
2830  * A version of #ui_but_string_get_ex for dynamic buffer sizes
2831  * (where #ui_but_string_get_max_length returns 0).
2832  *
2833  * \param r_str_size: size of the returned string (including terminator).
2834  */
ui_but_string_get_dynamic(uiBut * but,int * r_str_size)2835 char *ui_but_string_get_dynamic(uiBut *but, int *r_str_size)
2836 {
2837   char *str = NULL;
2838   *r_str_size = 1;
2839 
2840   if (but->rnaprop && ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU)) {
2841     PropertyType type = RNA_property_type(but->rnaprop);
2842 
2843     if (type == PROP_STRING) {
2844       /* RNA string */
2845       str = RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, NULL, 0, r_str_size);
2846       (*r_str_size) += 1;
2847     }
2848     else if (type == PROP_ENUM) {
2849       /* RNA enum */
2850       const int value = RNA_property_enum_get(&but->rnapoin, but->rnaprop);
2851       const char *value_id;
2852       if (!RNA_property_enum_name(
2853               but->block->evil_C, &but->rnapoin, but->rnaprop, value, &value_id)) {
2854         value_id = "";
2855       }
2856 
2857       *r_str_size = strlen(value_id) + 1;
2858       str = BLI_strdupn(value_id, *r_str_size);
2859     }
2860     else if (type == PROP_POINTER) {
2861       /* RNA pointer */
2862       PointerRNA ptr = RNA_property_pointer_get(&but->rnapoin, but->rnaprop);
2863       str = RNA_struct_name_get_alloc(&ptr, NULL, 0, r_str_size);
2864       (*r_str_size) += 1;
2865     }
2866     else {
2867       BLI_assert(0);
2868     }
2869   }
2870   else {
2871     BLI_assert(0);
2872   }
2873 
2874   if (UNLIKELY(str == NULL)) {
2875     /* should never happen, paranoid check */
2876     *r_str_size = 1;
2877     str = BLI_strdup("");
2878     BLI_assert(0);
2879   }
2880 
2881   return str;
2882 }
2883 
2884 /**
2885  * Report a generic error prefix when evaluating a string with #BPY_run_string_as_number
2886  * as the Python error on its own doesn't provide enough context.
2887  */
2888 #define UI_NUMBER_EVAL_ERROR_PREFIX IFACE_("Error evaluating number, see Info editor for details")
2889 
ui_number_from_string_units(bContext * C,const char * str,const int unit_type,const UnitSettings * unit,double * r_value)2890 static bool ui_number_from_string_units(
2891     bContext *C, const char *str, const int unit_type, const UnitSettings *unit, double *r_value)
2892 {
2893   return user_string_to_number(C, str, unit, unit_type, UI_NUMBER_EVAL_ERROR_PREFIX, r_value);
2894 }
2895 
ui_number_from_string_units_with_but(bContext * C,const char * str,const uiBut * but,double * r_value)2896 static bool ui_number_from_string_units_with_but(bContext *C,
2897                                                  const char *str,
2898                                                  const uiBut *but,
2899                                                  double *r_value)
2900 {
2901   const int unit_type = RNA_SUBTYPE_UNIT_VALUE(UI_but_unit_type_get(but));
2902   const UnitSettings *unit = but->block->unit;
2903   return ui_number_from_string_units(C, str, unit_type, unit, r_value);
2904 }
2905 
ui_number_from_string(bContext * C,const char * str,double * r_value)2906 static bool ui_number_from_string(bContext *C, const char *str, double *r_value)
2907 {
2908   bool ok;
2909 #ifdef WITH_PYTHON
2910   ok = BPY_run_string_as_number(C, NULL, str, UI_NUMBER_EVAL_ERROR_PREFIX, r_value);
2911 #else
2912   UNUSED_VARS(C);
2913   *r_value = atof(str);
2914   ok = true;
2915 #endif
2916   return ok;
2917 }
2918 
ui_number_from_string_factor(bContext * C,const char * str,double * r_value)2919 static bool ui_number_from_string_factor(bContext *C, const char *str, double *r_value)
2920 {
2921   const int len = strlen(str);
2922   if (BLI_strn_endswith(str, "%", len)) {
2923     char *str_new = BLI_strdupn(str, len - 1);
2924     const bool success = ui_number_from_string(C, str_new, r_value);
2925     MEM_freeN(str_new);
2926     *r_value /= 100.0;
2927     return success;
2928   }
2929   if (!ui_number_from_string(C, str, r_value)) {
2930     return false;
2931   }
2932   if (U.factor_display_type == USER_FACTOR_AS_PERCENTAGE) {
2933     *r_value /= 100.0;
2934   }
2935   return true;
2936 }
2937 
ui_number_from_string_percentage(bContext * C,const char * str,double * r_value)2938 static bool ui_number_from_string_percentage(bContext *C, const char *str, double *r_value)
2939 {
2940   const int len = strlen(str);
2941   if (BLI_strn_endswith(str, "%", len)) {
2942     char *str_new = BLI_strdupn(str, len - 1);
2943     const bool success = ui_number_from_string(C, str_new, r_value);
2944     MEM_freeN(str_new);
2945     return success;
2946   }
2947   return ui_number_from_string(C, str, r_value);
2948 }
2949 
ui_but_string_eval_number(bContext * C,const uiBut * but,const char * str,double * r_value)2950 bool ui_but_string_eval_number(bContext *C, const uiBut *but, const char *str, double *r_value)
2951 {
2952   if (str[0] == '\0') {
2953     *r_value = 0.0;
2954     return true;
2955   }
2956 
2957   PropertySubType subtype = PROP_NONE;
2958   if (but->rnaprop) {
2959     subtype = RNA_property_subtype(but->rnaprop);
2960   }
2961 
2962   if (ui_but_is_float(but)) {
2963     if (ui_but_is_unit(but)) {
2964       return ui_number_from_string_units_with_but(C, str, but, r_value);
2965     }
2966     if (subtype == PROP_FACTOR) {
2967       return ui_number_from_string_factor(C, str, r_value);
2968     }
2969     if (subtype == PROP_PERCENTAGE) {
2970       return ui_number_from_string_percentage(C, str, r_value);
2971     }
2972     return ui_number_from_string(C, str, r_value);
2973   }
2974   return ui_number_from_string(C, str, r_value);
2975 }
2976 
2977 /* just the assignment/free part */
ui_but_string_set_internal(uiBut * but,const char * str,size_t str_len)2978 static void ui_but_string_set_internal(uiBut *but, const char *str, size_t str_len)
2979 {
2980   BLI_assert(str_len == strlen(str));
2981   BLI_assert(but->str == NULL);
2982   str_len += 1;
2983 
2984   if (str_len > UI_MAX_NAME_STR) {
2985     but->str = MEM_mallocN(str_len, "ui_def_but str");
2986   }
2987   else {
2988     but->str = but->strdata;
2989   }
2990   memcpy(but->str, str, str_len);
2991 }
2992 
ui_but_string_free_internal(uiBut * but)2993 static void ui_but_string_free_internal(uiBut *but)
2994 {
2995   if (but->str) {
2996     if (but->str != but->strdata) {
2997       MEM_freeN(but->str);
2998     }
2999     /* must call 'ui_but_string_set_internal' after */
3000     but->str = NULL;
3001   }
3002 }
3003 
ui_but_string_set(bContext * C,uiBut * but,const char * str)3004 bool ui_but_string_set(bContext *C, uiBut *but, const char *str)
3005 {
3006   if (but->rnaprop && but->rnapoin.data && ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU)) {
3007     if (RNA_property_editable(&but->rnapoin, but->rnaprop)) {
3008       PropertyType type = RNA_property_type(but->rnaprop);
3009 
3010       if (type == PROP_STRING) {
3011         /* RNA string */
3012         RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
3013         return true;
3014       }
3015 
3016       if (type == PROP_POINTER) {
3017         if (str[0] == '\0') {
3018           RNA_property_pointer_set(&but->rnapoin, but->rnaprop, PointerRNA_NULL, NULL);
3019           return true;
3020         }
3021 
3022         uiButSearch *search_but = (but->type == UI_BTYPE_SEARCH_MENU) ? (uiButSearch *)but : NULL;
3023         /* RNA pointer */
3024         PointerRNA rptr;
3025 
3026         /* This is kind of hackish, in theory think we could only ever use the second member of
3027          * this if/else, since ui_searchbox_apply() is supposed to always set that pointer when
3028          * we are storing pointers... But keeping str search first for now,
3029          * to try to break as little as possible existing code. All this is band-aids anyway.
3030          * Fact remains, using editstr as main 'reference' over whole search button thingy
3031          * is utterly weak and should be redesigned imho, but that's not a simple task. */
3032         if (search_but && search_but->rnasearchprop &&
3033             RNA_property_collection_lookup_string(
3034                 &search_but->rnasearchpoin, search_but->rnasearchprop, str, &rptr)) {
3035           RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr, NULL);
3036         }
3037         else if (search_but->item_active != NULL) {
3038           RNA_pointer_create(NULL,
3039                              RNA_property_pointer_type(&but->rnapoin, but->rnaprop),
3040                              search_but->item_active,
3041                              &rptr);
3042           RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr, NULL);
3043         }
3044 
3045         return true;
3046       }
3047 
3048       if (type == PROP_ENUM) {
3049         int value;
3050         if (RNA_property_enum_value(
3051                 but->block->evil_C, &but->rnapoin, but->rnaprop, str, &value)) {
3052           RNA_property_enum_set(&but->rnapoin, but->rnaprop, value);
3053           return true;
3054         }
3055         return false;
3056       }
3057       BLI_assert(0);
3058     }
3059   }
3060   else if (but->type == UI_BTYPE_TAB) {
3061     if (but->rnaprop && but->custom_data) {
3062       StructRNA *ptr_type = RNA_property_pointer_type(&but->rnapoin, but->rnaprop);
3063       PointerRNA ptr;
3064       PropertyRNA *prop;
3065 
3066       /* uiBut.custom_data points to data this tab represents (e.g. workspace).
3067        * uiBut.rnapoin/prop store an active value (e.g. active workspace). */
3068       RNA_pointer_create(but->rnapoin.owner_id, ptr_type, but->custom_data, &ptr);
3069       prop = RNA_struct_name_property(ptr_type);
3070       if (RNA_property_editable(&ptr, prop)) {
3071         RNA_property_string_set(&ptr, prop, str);
3072       }
3073     }
3074   }
3075   else if (but->type == UI_BTYPE_TEXT) {
3076     /* string */
3077     if (!but->poin) {
3078       str = "";
3079     }
3080     else if (UI_but_is_utf8(but)) {
3081       BLI_strncpy_utf8(but->poin, str, but->hardmax);
3082     }
3083     else {
3084       BLI_strncpy(but->poin, str, but->hardmax);
3085     }
3086 
3087     return true;
3088   }
3089   else if (but->type == UI_BTYPE_SEARCH_MENU) {
3090     /* string */
3091     BLI_strncpy(but->poin, str, but->hardmax);
3092     return true;
3093   }
3094   else if (ui_but_anim_expression_set(but, str)) {
3095     /* driver expression */
3096     return true;
3097   }
3098   else if (str[0] == '#') {
3099     /* shortcut to create new driver expression (versus immediate Py-execution) */
3100     return ui_but_anim_expression_create(but, str + 1);
3101   }
3102   else {
3103     /* number editing */
3104     double value;
3105 
3106     if (ui_but_string_eval_number(C, but, str, &value) == false) {
3107       WM_report_banner_show();
3108       return false;
3109     }
3110 
3111     if (!ui_but_is_float(but)) {
3112       value = floor(value + 0.5);
3113     }
3114 
3115     /* not that we use hard limits here */
3116     if (value < (double)but->hardmin) {
3117       value = but->hardmin;
3118     }
3119     if (value > (double)but->hardmax) {
3120       value = but->hardmax;
3121     }
3122 
3123     ui_but_value_set(but, value);
3124     return true;
3125   }
3126 
3127   return false;
3128 }
3129 
soft_range_round_up(double value,double max)3130 static double soft_range_round_up(double value, double max)
3131 {
3132   /* round up to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, ..
3133    * checking for 0.0 prevents floating point exceptions */
3134   const double newmax = (value != 0.0) ? pow(10.0, ceil(log(value) / M_LN10)) : 0.0;
3135 
3136   if (newmax * 0.2 >= max && newmax * 0.2 >= value) {
3137     return newmax * 0.2;
3138   }
3139   if (newmax * 0.5 >= max && newmax * 0.5 >= value) {
3140     return newmax * 0.5;
3141   }
3142   return newmax;
3143 }
3144 
soft_range_round_down(double value,double max)3145 static double soft_range_round_down(double value, double max)
3146 {
3147   /* round down to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, ..
3148    * checking for 0.0 prevents floating point exceptions */
3149   const double newmax = (value != 0.0) ? pow(10.0, floor(log(value) / M_LN10)) : 0.0;
3150 
3151   if (newmax * 5.0 <= max && newmax * 5.0 <= value) {
3152     return newmax * 5.0;
3153   }
3154   if (newmax * 2.0 <= max && newmax * 2.0 <= value) {
3155     return newmax * 2.0;
3156   }
3157   return newmax;
3158 }
3159 
ui_but_range_set_hard(uiBut * but)3160 void ui_but_range_set_hard(uiBut *but)
3161 {
3162   if (but->rnaprop == NULL) {
3163     return;
3164   }
3165 
3166   const PropertyType type = RNA_property_type(but->rnaprop);
3167 
3168   /* clamp button range to something reasonable in case
3169    * we get -inf/inf from RNA properties */
3170   if (type == PROP_INT) {
3171     int imin, imax;
3172     RNA_property_int_range(&but->rnapoin, but->rnaprop, &imin, &imax);
3173     but->hardmin = (imin == INT_MIN) ? -1e4 : imin;
3174     but->hardmax = (imin == INT_MAX) ? 1e4 : imax;
3175   }
3176   else if (type == PROP_FLOAT) {
3177     float fmin, fmax;
3178     RNA_property_float_range(&but->rnapoin, but->rnaprop, &fmin, &fmax);
3179     but->hardmin = (fmin == -FLT_MAX) ? (float)-1e4 : fmin;
3180     but->hardmax = (fmax == FLT_MAX) ? (float)1e4 : fmax;
3181   }
3182 }
3183 
3184 /* note: this could be split up into functions which handle arrays and not */
ui_but_range_set_soft(uiBut * but)3185 void ui_but_range_set_soft(uiBut *but)
3186 {
3187   /* ideally we would not limit this but practically, its more than
3188    * enough worst case is very long vectors wont use a smart soft-range
3189    * which isn't so bad. */
3190 
3191   if (but->rnaprop) {
3192     const PropertyType type = RNA_property_type(but->rnaprop);
3193     double softmin, softmax /*, step, precision*/;
3194     double value_min;
3195     double value_max;
3196 
3197     /* clamp button range to something reasonable in case
3198      * we get -inf/inf from RNA properties */
3199     if (type == PROP_INT) {
3200       const bool is_array = RNA_property_array_check(but->rnaprop);
3201       int imin, imax, istep;
3202 
3203       RNA_property_int_ui_range(&but->rnapoin, but->rnaprop, &imin, &imax, &istep);
3204       softmin = (imin == INT_MIN) ? -1e4 : imin;
3205       softmax = (imin == INT_MAX) ? 1e4 : imax;
3206       /*step = istep;*/  /*UNUSED*/
3207       /*precision = 1;*/ /*UNUSED*/
3208 
3209       if (is_array) {
3210         int value_range[2];
3211         RNA_property_int_get_array_range(&but->rnapoin, but->rnaprop, value_range);
3212         value_min = (double)value_range[0];
3213         value_max = (double)value_range[1];
3214       }
3215       else {
3216         value_min = value_max = (double)RNA_property_int_get(&but->rnapoin, but->rnaprop);
3217       }
3218     }
3219     else if (type == PROP_FLOAT) {
3220       const bool is_array = RNA_property_array_check(but->rnaprop);
3221       float fmin, fmax, fstep, fprecision;
3222 
3223       RNA_property_float_ui_range(&but->rnapoin, but->rnaprop, &fmin, &fmax, &fstep, &fprecision);
3224       softmin = (fmin == -FLT_MAX) ? (float)-1e4 : fmin;
3225       softmax = (fmax == FLT_MAX) ? (float)1e4 : fmax;
3226       /*step = fstep;*/           /*UNUSED*/
3227       /*precision = fprecision;*/ /*UNUSED*/
3228 
3229       if (is_array) {
3230         float value_range[2];
3231         RNA_property_float_get_array_range(&but->rnapoin, but->rnaprop, value_range);
3232         value_min = (double)value_range[0];
3233         value_max = (double)value_range[1];
3234       }
3235       else {
3236         value_min = value_max = (double)RNA_property_float_get(&but->rnapoin, but->rnaprop);
3237       }
3238     }
3239     else {
3240       return;
3241     }
3242 
3243     /* if the value goes out of the soft/max range, adapt the range */
3244     if (value_min + 1e-10 < softmin) {
3245       if (value_min < 0.0) {
3246         softmin = -soft_range_round_up(-value_min, -softmin);
3247       }
3248       else {
3249         softmin = soft_range_round_down(value_min, softmin);
3250       }
3251 
3252       if (softmin < (double)but->hardmin) {
3253         softmin = (double)but->hardmin;
3254       }
3255     }
3256     if (value_max - 1e-10 > softmax) {
3257       if (value_max < 0.0) {
3258         softmax = -soft_range_round_down(-value_max, -softmax);
3259       }
3260       else {
3261         softmax = soft_range_round_up(value_max, softmax);
3262       }
3263 
3264       if (softmax > (double)but->hardmax) {
3265         softmax = but->hardmax;
3266       }
3267     }
3268 
3269     but->softmin = softmin;
3270     but->softmax = softmax;
3271   }
3272   else if (but->poin && (but->pointype & UI_BUT_POIN_TYPES)) {
3273     float value = ui_but_value_get(but);
3274     if (isfinite(value)) {
3275       CLAMP(value, but->hardmin, but->hardmax);
3276       but->softmin = min_ff(but->softmin, value);
3277       but->softmax = max_ff(but->softmax, value);
3278     }
3279   }
3280 }
3281 
3282 /* ******************* Free ********************/
3283 
3284 /**
3285  * Free data specific to a certain button type.
3286  * For now just do in a switch-case, we could instead have a callback stored in #uiBut and set that
3287  * in #ui_but_alloc_info().
3288  */
ui_but_free_type_specific(uiBut * but)3289 static void ui_but_free_type_specific(uiBut *but)
3290 {
3291   switch (but->type) {
3292     case UI_BTYPE_SEARCH_MENU: {
3293       uiButSearch *search_but = (uiButSearch *)but;
3294 
3295       if (search_but->arg_free_fn) {
3296         search_but->arg_free_fn(search_but->arg);
3297         search_but->arg = NULL;
3298       }
3299       break;
3300     }
3301     default:
3302       break;
3303   }
3304 }
3305 
3306 /* can be called with C==NULL */
ui_but_free(const bContext * C,uiBut * but)3307 static void ui_but_free(const bContext *C, uiBut *but)
3308 {
3309   if (but->opptr) {
3310     WM_operator_properties_free(but->opptr);
3311     MEM_freeN(but->opptr);
3312   }
3313 
3314   if (but->func_argN) {
3315     MEM_freeN(but->func_argN);
3316   }
3317 
3318   if (but->tip_argN) {
3319     MEM_freeN(but->tip_argN);
3320   }
3321 
3322   if (but->hold_argN) {
3323     MEM_freeN(but->hold_argN);
3324   }
3325 
3326   ui_but_free_type_specific(but);
3327 
3328   if (but->active) {
3329     /* XXX solve later, buttons should be free-able without context ideally,
3330      * however they may have open tooltips or popup windows, which need to
3331      * be closed using a context pointer */
3332     if (C) {
3333       ui_but_active_free(C, but);
3334     }
3335     else {
3336       if (but->active) {
3337         MEM_freeN(but->active);
3338       }
3339     }
3340   }
3341   if (but->str && but->str != but->strdata) {
3342     MEM_freeN(but->str);
3343   }
3344 
3345   if ((but->type == UI_BTYPE_IMAGE) && but->poin) {
3346     IMB_freeImBuf((struct ImBuf *)but->poin);
3347   }
3348 
3349   if (but->dragpoin && (but->dragflag & UI_BUT_DRAGPOIN_FREE)) {
3350     MEM_freeN(but->dragpoin);
3351   }
3352   ui_but_extra_operator_icons_free(but);
3353 
3354   BLI_assert(UI_butstore_is_registered(but->block, but) == false);
3355 
3356   MEM_freeN(but);
3357 }
3358 
3359 /* can be called with C==NULL */
UI_block_free(const bContext * C,uiBlock * block)3360 void UI_block_free(const bContext *C, uiBlock *block)
3361 {
3362   UI_butstore_clear(block);
3363 
3364   uiBut *but;
3365   while ((but = BLI_pophead(&block->buttons))) {
3366     ui_but_free(C, but);
3367   }
3368 
3369   if (block->unit) {
3370     MEM_freeN(block->unit);
3371   }
3372 
3373   if (block->func_argN) {
3374     MEM_freeN(block->func_argN);
3375   }
3376 
3377   CTX_store_free_list(&block->contexts);
3378 
3379   BLI_freelistN(&block->saferct);
3380   BLI_freelistN(&block->color_pickers.list);
3381 
3382   ui_block_free_button_groups(block);
3383 
3384   MEM_freeN(block);
3385 }
3386 
UI_blocklist_update_window_matrix(const bContext * C,const ListBase * lb)3387 void UI_blocklist_update_window_matrix(const bContext *C, const ListBase *lb)
3388 {
3389   ARegion *region = CTX_wm_region(C);
3390   wmWindow *window = CTX_wm_window(C);
3391 
3392   LISTBASE_FOREACH (uiBlock *, block, lb) {
3393     if (block->active) {
3394       ui_update_window_matrix(window, region, block);
3395     }
3396   }
3397 }
3398 
UI_blocklist_draw(const bContext * C,const ListBase * lb)3399 void UI_blocklist_draw(const bContext *C, const ListBase *lb)
3400 {
3401   LISTBASE_FOREACH (uiBlock *, block, lb) {
3402     if (block->active) {
3403       UI_block_draw(C, block);
3404     }
3405   }
3406 }
3407 
3408 /* can be called with C==NULL */
UI_blocklist_free(const bContext * C,ListBase * lb)3409 void UI_blocklist_free(const bContext *C, ListBase *lb)
3410 {
3411   uiBlock *block;
3412   while ((block = BLI_pophead(lb))) {
3413     UI_block_free(C, block);
3414   }
3415 }
3416 
UI_blocklist_free_inactive(const bContext * C,ListBase * lb)3417 void UI_blocklist_free_inactive(const bContext *C, ListBase *lb)
3418 {
3419   LISTBASE_FOREACH_MUTABLE (uiBlock *, block, lb) {
3420     if (!block->handle) {
3421       if (!block->active) {
3422         BLI_remlink(lb, block);
3423         UI_block_free(C, block);
3424       }
3425       else {
3426         block->active = 0;
3427       }
3428     }
3429   }
3430 }
3431 
UI_block_region_set(uiBlock * block,ARegion * region)3432 void UI_block_region_set(uiBlock *block, ARegion *region)
3433 {
3434   ListBase *lb = &region->uiblocks;
3435   uiBlock *oldblock = NULL;
3436 
3437   /* each listbase only has one block with this name, free block
3438    * if is already there so it can be rebuilt from scratch */
3439   if (lb) {
3440     oldblock = BLI_findstring(lb, block->name, offsetof(uiBlock, name));
3441 
3442     if (oldblock) {
3443       oldblock->active = 0;
3444       oldblock->panel = NULL;
3445       oldblock->handle = NULL;
3446     }
3447 
3448     /* at the beginning of the list! for dynamical menus/blocks */
3449     BLI_addhead(lb, block);
3450   }
3451 
3452   block->oldblock = oldblock;
3453 }
3454 
UI_block_begin(const bContext * C,ARegion * region,const char * name,char emboss)3455 uiBlock *UI_block_begin(const bContext *C, ARegion *region, const char *name, char emboss)
3456 {
3457   wmWindow *window = CTX_wm_window(C);
3458   Scene *scene = CTX_data_scene(C);
3459 
3460   uiBlock *block = MEM_callocN(sizeof(uiBlock), "uiBlock");
3461   block->active = 1;
3462   block->emboss = emboss;
3463   block->evil_C = (void *)C; /* XXX */
3464 
3465   BLI_listbase_clear(&block->button_groups);
3466 
3467   if (scene) {
3468     /* store display device name, don't lookup for transformations yet
3469      * block could be used for non-color displays where looking up for transformation
3470      * would slow down redraw, so only lookup for actual transform when it's indeed
3471      * needed
3472      */
3473     STRNCPY(block->display_device, scene->display_settings.display_device);
3474 
3475     /* copy to avoid crash when scene gets deleted with ui still open */
3476     block->unit = MEM_mallocN(sizeof(scene->unit), "UI UnitSettings");
3477     memcpy(block->unit, &scene->unit, sizeof(scene->unit));
3478   }
3479   else {
3480     STRNCPY(block->display_device, IMB_colormanagement_display_get_default_name());
3481   }
3482 
3483   BLI_strncpy(block->name, name, sizeof(block->name));
3484 
3485   if (region) {
3486     UI_block_region_set(block, region);
3487   }
3488 
3489   /* Set window matrix and aspect for region and OpenGL state. */
3490   ui_update_window_matrix(window, region, block);
3491 
3492   /* Tag as popup menu if not created within a region. */
3493   if (!(region && region->visible)) {
3494     block->auto_open = true;
3495     block->flag |= UI_BLOCK_LOOP;
3496   }
3497 
3498   return block;
3499 }
3500 
UI_block_emboss_get(uiBlock * block)3501 char UI_block_emboss_get(uiBlock *block)
3502 {
3503   return block->emboss;
3504 }
3505 
UI_block_emboss_set(uiBlock * block,char emboss)3506 void UI_block_emboss_set(uiBlock *block, char emboss)
3507 {
3508   block->emboss = emboss;
3509 }
3510 
UI_block_theme_style_set(uiBlock * block,char theme_style)3511 void UI_block_theme_style_set(uiBlock *block, char theme_style)
3512 {
3513   block->theme_style = theme_style;
3514 }
3515 
UI_block_is_search_only(const uiBlock * block)3516 bool UI_block_is_search_only(const uiBlock *block)
3517 {
3518   return block->flag & UI_BLOCK_SEARCH_ONLY;
3519 }
3520 
3521 /**
3522  * Use when a block must be searched to give accurate results
3523  * for the whole region but shouldn't be displayed.
3524  */
UI_block_set_search_only(uiBlock * block,bool search_only)3525 void UI_block_set_search_only(uiBlock *block, bool search_only)
3526 {
3527   SET_FLAG_FROM_TEST(block->flag, search_only, UI_BLOCK_SEARCH_ONLY);
3528 }
3529 
ui_but_build_drawstr_float(uiBut * but,double value)3530 static void ui_but_build_drawstr_float(uiBut *but, double value)
3531 {
3532   size_t slen = 0;
3533   STR_CONCAT(but->drawstr, slen, but->str);
3534 
3535   PropertySubType subtype = PROP_NONE;
3536   if (but->rnaprop) {
3537     subtype = RNA_property_subtype(but->rnaprop);
3538   }
3539 
3540   /* Change negative zero to regular zero, without altering anything else.  */
3541   value += +0.0f;
3542 
3543   if (value == (double)FLT_MAX) {
3544     STR_CONCAT(but->drawstr, slen, "inf");
3545   }
3546   else if (value == (double)-FLT_MAX) {
3547     STR_CONCAT(but->drawstr, slen, "-inf");
3548   }
3549   else if (subtype == PROP_PERCENTAGE) {
3550     const int prec = ui_but_calc_float_precision(but, value);
3551     STR_CONCATF(but->drawstr, slen, "%.*f%%", prec, value);
3552   }
3553   else if (subtype == PROP_PIXEL) {
3554     const int prec = ui_but_calc_float_precision(but, value);
3555     STR_CONCATF(but->drawstr, slen, "%.*f px", prec, value);
3556   }
3557   else if (subtype == PROP_FACTOR) {
3558     const int precision = ui_but_calc_float_precision(but, value);
3559 
3560     if (U.factor_display_type == USER_FACTOR_AS_FACTOR) {
3561       STR_CONCATF(but->drawstr, slen, "%.*f", precision, value);
3562     }
3563     else {
3564       STR_CONCATF(but->drawstr, slen, "%.*f%%", MAX2(0, precision - 2), value * 100);
3565     }
3566   }
3567   else if (ui_but_is_unit(but)) {
3568     char new_str[sizeof(but->drawstr)];
3569     ui_get_but_string_unit(but, new_str, sizeof(new_str), value, true, -1);
3570     STR_CONCAT(but->drawstr, slen, new_str);
3571   }
3572   else {
3573     const int prec = ui_but_calc_float_precision(but, value);
3574     STR_CONCATF(but->drawstr, slen, "%.*f", prec, value);
3575   }
3576 }
3577 
ui_but_build_drawstr_int(uiBut * but,int value)3578 static void ui_but_build_drawstr_int(uiBut *but, int value)
3579 {
3580   size_t slen = 0;
3581   STR_CONCAT(but->drawstr, slen, but->str);
3582 
3583   PropertySubType subtype = PROP_NONE;
3584   if (but->rnaprop) {
3585     subtype = RNA_property_subtype(but->rnaprop);
3586   }
3587 
3588   STR_CONCATF(but->drawstr, slen, "%d", value);
3589 
3590   if (subtype == PROP_PERCENTAGE) {
3591     STR_CONCAT(but->drawstr, slen, "%");
3592   }
3593   else if (subtype == PROP_PIXEL) {
3594     STR_CONCAT(but->drawstr, slen, " px");
3595   }
3596 }
3597 
3598 /**
3599  * \param but: Button to update.
3600  * \param validate: When set, this function may change the button value.
3601  * Otherwise treat the button value as read-only.
3602  */
ui_but_update_ex(uiBut * but,const bool validate)3603 static void ui_but_update_ex(uiBut *but, const bool validate)
3604 {
3605   /* if something changed in the button */
3606   double value = UI_BUT_VALUE_UNSET;
3607 
3608   ui_but_update_select_flag(but, &value);
3609 
3610   /* only update soft range while not editing */
3611   if (!ui_but_is_editing(but)) {
3612     if ((but->rnaprop != NULL) || (but->poin && (but->pointype & UI_BUT_POIN_TYPES))) {
3613       ui_but_range_set_soft(but);
3614     }
3615   }
3616 
3617   /* test for min and max, icon sliders, etc */
3618   switch (but->type) {
3619     case UI_BTYPE_NUM:
3620     case UI_BTYPE_SCROLL:
3621     case UI_BTYPE_NUM_SLIDER:
3622       if (validate) {
3623         UI_GET_BUT_VALUE_INIT(but, value);
3624         if (value < (double)but->hardmin) {
3625           ui_but_value_set(but, but->hardmin);
3626         }
3627         else if (value > (double)but->hardmax) {
3628           ui_but_value_set(but, but->hardmax);
3629         }
3630 
3631         /* max must never be smaller than min! Both being equal is allowed though */
3632         BLI_assert(but->softmin <= but->softmax && but->hardmin <= but->hardmax);
3633       }
3634       break;
3635 
3636     case UI_BTYPE_ICON_TOGGLE:
3637     case UI_BTYPE_ICON_TOGGLE_N:
3638       if ((but->rnaprop == NULL) || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
3639         if (but->rnaprop && RNA_property_flag(but->rnaprop) & PROP_ICONS_REVERSE) {
3640           but->drawflag |= UI_BUT_ICON_REVERSE;
3641         }
3642 
3643         but->iconadd = (but->flag & UI_SELECT) ? 1 : 0;
3644       }
3645       break;
3646 
3647       /* quiet warnings for unhandled types */
3648     default:
3649       break;
3650   }
3651 
3652   /* safety is 4 to enable small number buttons (like 'users') */
3653   // okwidth = -4 + (BLI_rcti_size_x(&but->rect)); /* UNUSED */
3654 
3655   /* name: */
3656   switch (but->type) {
3657 
3658     case UI_BTYPE_MENU:
3659       if (BLI_rctf_size_x(&but->rect) >= (UI_UNIT_X * 2)) {
3660         /* only needed for menus in popup blocks that don't recreate buttons on redraw */
3661         if (but->block->flag & UI_BLOCK_LOOP) {
3662           if (but->rnaprop && (RNA_property_type(but->rnaprop) == PROP_ENUM)) {
3663             const int value_enum = RNA_property_enum_get(&but->rnapoin, but->rnaprop);
3664 
3665             EnumPropertyItem item;
3666             if (RNA_property_enum_item_from_value_gettexted(
3667                     but->block->evil_C, &but->rnapoin, but->rnaprop, value_enum, &item)) {
3668               const size_t slen = strlen(item.name);
3669               ui_but_string_free_internal(but);
3670               ui_but_string_set_internal(but, item.name, slen);
3671               but->icon = item.icon;
3672             }
3673           }
3674         }
3675         BLI_strncpy(but->drawstr, but->str, sizeof(but->drawstr));
3676       }
3677       break;
3678 
3679     case UI_BTYPE_NUM:
3680     case UI_BTYPE_NUM_SLIDER:
3681       if (but->editstr) {
3682         break;
3683       }
3684       UI_GET_BUT_VALUE_INIT(but, value);
3685       if (ui_but_is_float(but)) {
3686         ui_but_build_drawstr_float(but, value);
3687       }
3688       else {
3689         ui_but_build_drawstr_int(but, (int)value);
3690       }
3691       break;
3692 
3693     case UI_BTYPE_LABEL:
3694       if (ui_but_is_float(but)) {
3695         UI_GET_BUT_VALUE_INIT(but, value);
3696         int prec = ui_but_calc_float_precision(but, value);
3697         BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%.*f", but->str, prec, value);
3698       }
3699       else {
3700         BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
3701       }
3702 
3703       break;
3704 
3705     case UI_BTYPE_TEXT:
3706     case UI_BTYPE_SEARCH_MENU:
3707       if (!but->editstr) {
3708         char str[UI_MAX_DRAW_STR];
3709 
3710         ui_but_string_get(but, str, UI_MAX_DRAW_STR);
3711         BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%s", but->str, str);
3712       }
3713       break;
3714 
3715     case UI_BTYPE_KEY_EVENT: {
3716       const char *str;
3717       if (but->flag & UI_SELECT) {
3718         str = "Press a key";
3719       }
3720       else {
3721         UI_GET_BUT_VALUE_INIT(but, value);
3722         str = WM_key_event_string((short)value, false);
3723       }
3724       BLI_snprintf(but->drawstr, UI_MAX_DRAW_STR, "%s%s", but->str, str);
3725       break;
3726     }
3727     case UI_BTYPE_HOTKEY_EVENT:
3728       if (but->flag & UI_SELECT) {
3729 
3730         if (but->modifier_key) {
3731           char *str = but->drawstr;
3732           but->drawstr[0] = '\0';
3733 
3734           if (but->modifier_key & KM_SHIFT) {
3735             str += BLI_strcpy_rlen(str, "Shift ");
3736           }
3737           if (but->modifier_key & KM_CTRL) {
3738             str += BLI_strcpy_rlen(str, "Ctrl ");
3739           }
3740           if (but->modifier_key & KM_ALT) {
3741             str += BLI_strcpy_rlen(str, "Alt ");
3742           }
3743           if (but->modifier_key & KM_OSKEY) {
3744             str += BLI_strcpy_rlen(str, "Cmd ");
3745           }
3746 
3747           (void)str; /* UNUSED */
3748         }
3749         else {
3750           BLI_strncpy(but->drawstr, "Press a key", UI_MAX_DRAW_STR);
3751         }
3752       }
3753       else {
3754         BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
3755       }
3756 
3757       break;
3758 
3759     case UI_BTYPE_HSVCUBE:
3760     case UI_BTYPE_HSVCIRCLE:
3761       break;
3762     default:
3763       BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
3764       break;
3765   }
3766 
3767   /* if we are doing text editing, this will override the drawstr */
3768   if (but->editstr) {
3769     but->drawstr[0] = '\0';
3770   }
3771 
3772   /* text clipping moved to widget drawing code itself */
3773 }
3774 
ui_but_update(uiBut * but)3775 void ui_but_update(uiBut *but)
3776 {
3777   ui_but_update_ex(but, false);
3778 }
3779 
ui_but_update_edited(uiBut * but)3780 void ui_but_update_edited(uiBut *but)
3781 {
3782   ui_but_update_ex(but, true);
3783 }
3784 
UI_block_align_begin(uiBlock * block)3785 void UI_block_align_begin(uiBlock *block)
3786 {
3787   /* if other align was active, end it */
3788   if (block->flag & UI_BUT_ALIGN) {
3789     UI_block_align_end(block);
3790   }
3791 
3792   block->flag |= UI_BUT_ALIGN_DOWN;
3793   block->alignnr++;
3794 
3795   /* buttons declared after this call will get this align nr */ /* XXX flag? */
3796 }
3797 
UI_block_align_end(uiBlock * block)3798 void UI_block_align_end(uiBlock *block)
3799 {
3800   block->flag &= ~UI_BUT_ALIGN; /* all 4 flags */
3801 }
3802 
ui_block_cm_display_get(uiBlock * block)3803 struct ColorManagedDisplay *ui_block_cm_display_get(uiBlock *block)
3804 {
3805   return IMB_colormanagement_display_get_named(block->display_device);
3806 }
3807 
ui_block_cm_to_display_space_v3(uiBlock * block,float pixel[3])3808 void ui_block_cm_to_display_space_v3(uiBlock *block, float pixel[3])
3809 {
3810   struct ColorManagedDisplay *display = ui_block_cm_display_get(block);
3811 
3812   IMB_colormanagement_scene_linear_to_display_v3(pixel, display);
3813 }
3814 
ui_but_alloc_info(const eButType type,size_t * r_alloc_size,const char ** r_alloc_str,bool * r_has_custom_type)3815 static void ui_but_alloc_info(const eButType type,
3816                               size_t *r_alloc_size,
3817                               const char **r_alloc_str,
3818                               bool *r_has_custom_type)
3819 {
3820   size_t alloc_size;
3821   const char *alloc_str;
3822   bool has_custom_type = true;
3823 
3824   switch (type) {
3825     case UI_BTYPE_NUM:
3826       alloc_size = sizeof(uiButNumber);
3827       alloc_str = "uiButNumber";
3828       break;
3829     case UI_BTYPE_COLOR:
3830       alloc_size = sizeof(uiButColor);
3831       alloc_str = "uiButColor";
3832       break;
3833     case UI_BTYPE_DECORATOR:
3834       alloc_size = sizeof(uiButDecorator);
3835       alloc_str = "uiButDecorator";
3836       break;
3837     case UI_BTYPE_TAB:
3838       alloc_size = sizeof(uiButTab);
3839       alloc_str = "uiButTab";
3840       break;
3841     case UI_BTYPE_SEARCH_MENU:
3842       alloc_size = sizeof(uiButSearch);
3843       alloc_str = "uiButSearch";
3844       break;
3845     case UI_BTYPE_PROGRESS_BAR:
3846       alloc_size = sizeof(uiButProgressbar);
3847       alloc_str = "uiButProgressbar";
3848       break;
3849     case UI_BTYPE_HSVCUBE:
3850       alloc_size = sizeof(uiButHSVCube);
3851       alloc_str = "uiButHSVCube";
3852       break;
3853     case UI_BTYPE_COLORBAND:
3854       alloc_size = sizeof(uiButColorBand);
3855       alloc_str = "uiButColorBand";
3856       break;
3857     case UI_BTYPE_CURVE:
3858       alloc_size = sizeof(uiButCurveMapping);
3859       alloc_str = "uiButCurveMapping";
3860       break;
3861     case UI_BTYPE_CURVEPROFILE:
3862       alloc_size = sizeof(uiButCurveProfile);
3863       alloc_str = "uiButCurveProfile";
3864       break;
3865     default:
3866       alloc_size = sizeof(uiBut);
3867       alloc_str = "uiBut";
3868       has_custom_type = false;
3869       break;
3870   }
3871 
3872   if (r_alloc_size) {
3873     *r_alloc_size = alloc_size;
3874   }
3875   if (r_alloc_str) {
3876     *r_alloc_str = alloc_str;
3877   }
3878   if (r_has_custom_type) {
3879     *r_has_custom_type = has_custom_type;
3880   }
3881 }
3882 
ui_but_alloc(const eButType type)3883 static uiBut *ui_but_alloc(const eButType type)
3884 {
3885   size_t alloc_size;
3886   const char *alloc_str;
3887   ui_but_alloc_info(type, &alloc_size, &alloc_str, NULL);
3888 
3889   return MEM_callocN(alloc_size, alloc_str);
3890 }
3891 
3892 /**
3893  * Reallocate the button (new address is returned) for a new button type.
3894  * This should generally be avoided and instead the correct type be created right away.
3895  *
3896  * \note Only the #uiBut data can be kept. If the old button used a derived type (e.g. #uiButTab),
3897  *       the data that is not inside #uiBut will be lost.
3898  */
ui_but_change_type(uiBut * but,eButType new_type)3899 uiBut *ui_but_change_type(uiBut *but, eButType new_type)
3900 {
3901   if (but->type == new_type) {
3902     /* Nothing to do. */
3903     return but;
3904   }
3905 
3906   size_t alloc_size;
3907   const char *alloc_str;
3908   uiBut *insert_after_but = but->prev;
3909   bool new_has_custom_type, old_has_custom_type;
3910 
3911   /* Remove old button address */
3912   BLI_remlink(&but->block->buttons, but);
3913 
3914   ui_but_alloc_info(but->type, NULL, NULL, &old_has_custom_type);
3915   ui_but_alloc_info(new_type, &alloc_size, &alloc_str, &new_has_custom_type);
3916 
3917   if (new_has_custom_type || old_has_custom_type) {
3918     const void *old_but_ptr = but;
3919     /* Button may have pointer to a member within itself, this will have to be updated. */
3920     const bool has_str_ptr_to_self = but->str == but->strdata;
3921     const bool has_poin_ptr_to_self = but->poin == (char *)but;
3922 
3923     but = MEM_recallocN_id(but, alloc_size, alloc_str);
3924     but->type = new_type;
3925     if (has_str_ptr_to_self) {
3926       but->str = but->strdata;
3927     }
3928     if (has_poin_ptr_to_self) {
3929       but->poin = (char *)but;
3930     }
3931 
3932     BLI_insertlinkafter(&but->block->buttons, insert_after_but, but);
3933 
3934     if (but->layout) {
3935       const bool found_layout = ui_layout_replace_but_ptr(but->layout, old_but_ptr, but);
3936       BLI_assert(found_layout);
3937       UNUSED_VARS_NDEBUG(found_layout);
3938       ui_button_group_replace_but_ptr(uiLayoutGetBlock(but->layout), old_but_ptr, but);
3939     }
3940   }
3941 
3942   return but;
3943 }
3944 
3945 /**
3946  * \brief ui_def_but is the function that draws many button types
3947  *
3948  * \param x, y: The lower left hand corner of the button (X axis)
3949  * \param width, height: The size of the button.
3950  *
3951  * for float buttons:
3952  * \param a1: Click Step (how much to change the value each click)
3953  * \param a2: Number of decimal point values to display. 0 defaults to 3 (0.000)
3954  * 1,2,3, and a maximum of 4, all greater values will be clamped to 4.
3955  */
ui_def_but(uiBlock * block,int type,int retval,const char * str,int x,int y,short width,short height,void * poin,float min,float max,float a1,float a2,const char * tip)3956 static uiBut *ui_def_but(uiBlock *block,
3957                          int type,
3958                          int retval,
3959                          const char *str,
3960                          int x,
3961                          int y,
3962                          short width,
3963                          short height,
3964                          void *poin,
3965                          float min,
3966                          float max,
3967                          float a1,
3968                          float a2,
3969                          const char *tip)
3970 {
3971   BLI_assert(width >= 0 && height >= 0);
3972 
3973   /* we could do some more error checks here */
3974   if ((type & BUTTYPE) == UI_BTYPE_LABEL) {
3975     BLI_assert((poin != NULL || min != 0.0f || max != 0.0f || (a1 == 0.0f && a2 != 0.0f) ||
3976                 (a1 != 0.0f && a1 != 1.0f)) == false);
3977   }
3978 
3979   if (type & UI_BUT_POIN_TYPES) { /* a pointer is required */
3980     if (poin == NULL) {
3981       BLI_assert(0);
3982       return NULL;
3983     }
3984   }
3985 
3986   uiBut *but = ui_but_alloc(type & BUTTYPE);
3987 
3988   but->type = type & BUTTYPE;
3989   but->pointype = type & UI_BUT_POIN_TYPES;
3990   but->bit = type & UI_BUT_POIN_BIT;
3991   but->bitnr = type & 31;
3992   but->icon = ICON_NONE;
3993   but->iconadd = 0;
3994 
3995   but->retval = retval;
3996 
3997   int slen = strlen(str);
3998   ui_but_string_set_internal(but, str, slen);
3999 
4000   but->rect.xmin = x;
4001   but->rect.ymin = y;
4002   but->rect.xmax = but->rect.xmin + width;
4003   but->rect.ymax = but->rect.ymin + height;
4004 
4005   but->poin = poin;
4006   but->hardmin = but->softmin = min;
4007   but->hardmax = but->softmax = max;
4008   but->a1 = a1;
4009   but->a2 = a2;
4010   but->tip = tip;
4011 
4012   but->disabled_info = block->lockstr;
4013   but->emboss = block->emboss;
4014   but->pie_dir = UI_RADIAL_NONE;
4015 
4016   but->block = block; /* pointer back, used for frontbuffer status, and picker */
4017 
4018   if ((block->flag & UI_BUT_ALIGN) && ui_but_can_align(but)) {
4019     but->alignnr = block->alignnr;
4020   }
4021 
4022   but->func = block->func;
4023   but->func_arg1 = block->func_arg1;
4024   but->func_arg2 = block->func_arg2;
4025 
4026   but->funcN = block->funcN;
4027   if (block->func_argN) {
4028     but->func_argN = MEM_dupallocN(block->func_argN);
4029   }
4030 
4031   but->pos = -1; /* cursor invisible */
4032 
4033   if (ELEM(but->type, UI_BTYPE_NUM, UI_BTYPE_NUM_SLIDER)) { /* add a space to name */
4034     /* slen remains unchanged from previous assignment, ensure this stays true */
4035     if (slen > 0 && slen < UI_MAX_NAME_STR - 2) {
4036       if (but->str[slen - 1] != ' ') {
4037         but->str[slen] = ' ';
4038         but->str[slen + 1] = 0;
4039       }
4040     }
4041   }
4042 
4043   if (block->flag & UI_BLOCK_RADIAL) {
4044     but->drawflag |= UI_BUT_TEXT_LEFT;
4045     if (but->str && but->str[0]) {
4046       but->drawflag |= UI_BUT_ICON_LEFT;
4047     }
4048   }
4049   else if (((block->flag & UI_BLOCK_LOOP) && !ui_block_is_popover(block)) ||
4050            ELEM(but->type,
4051                 UI_BTYPE_MENU,
4052                 UI_BTYPE_TEXT,
4053                 UI_BTYPE_LABEL,
4054                 UI_BTYPE_BLOCK,
4055                 UI_BTYPE_BUT_MENU,
4056                 UI_BTYPE_SEARCH_MENU,
4057                 UI_BTYPE_PROGRESS_BAR,
4058                 UI_BTYPE_POPOVER)) {
4059     but->drawflag |= (UI_BUT_TEXT_LEFT | UI_BUT_ICON_LEFT);
4060   }
4061 #ifdef USE_NUMBUTS_LR_ALIGN
4062   else if (ELEM(but->type, UI_BTYPE_NUM, UI_BTYPE_NUM_SLIDER)) {
4063     if (slen != 0) {
4064       but->drawflag |= UI_BUT_TEXT_LEFT;
4065     }
4066   }
4067 #endif
4068 
4069   but->drawflag |= (block->flag & UI_BUT_ALIGN);
4070 
4071   if (block->lock == true) {
4072     but->flag |= UI_BUT_DISABLED;
4073   }
4074 
4075   /* keep track of UI_interface.h */
4076   if (ELEM(but->type,
4077            UI_BTYPE_BLOCK,
4078            UI_BTYPE_BUT,
4079            UI_BTYPE_DECORATOR,
4080            UI_BTYPE_LABEL,
4081            UI_BTYPE_PULLDOWN,
4082            UI_BTYPE_ROUNDBOX,
4083            UI_BTYPE_LISTBOX,
4084            UI_BTYPE_BUT_MENU,
4085            UI_BTYPE_SCROLL,
4086            UI_BTYPE_GRIP,
4087            UI_BTYPE_SEPR,
4088            UI_BTYPE_SEPR_LINE,
4089            UI_BTYPE_SEPR_SPACER) ||
4090       (but->type >= UI_BTYPE_SEARCH_MENU)) {
4091     /* pass */
4092   }
4093   else {
4094     but->flag |= UI_BUT_UNDO;
4095   }
4096 
4097   BLI_addtail(&block->buttons, but);
4098 
4099   if (block->curlayout) {
4100     ui_layout_add_but(block->curlayout, but);
4101   }
4102 
4103 #ifdef WITH_PYTHON
4104   /* if the 'UI_OT_editsource' is running, extract the source info from the button  */
4105   if (UI_editsource_enable_check()) {
4106     UI_editsource_active_but_test(but);
4107   }
4108 #endif
4109 
4110   return but;
4111 }
4112 
ui_def_but_icon(uiBut * but,const int icon,const int flag)4113 void ui_def_but_icon(uiBut *but, const int icon, const int flag)
4114 {
4115   if (icon) {
4116     ui_icon_ensure_deferred(but->block->evil_C, icon, (flag & UI_BUT_ICON_PREVIEW) != 0);
4117   }
4118   but->icon = (BIFIconID)icon;
4119   but->flag |= flag;
4120 
4121   if (but->str && but->str[0]) {
4122     but->drawflag |= UI_BUT_ICON_LEFT;
4123   }
4124 }
4125 
4126 /**
4127  * Avoid using this where possible since it's better not to ask for an icon in the first place.
4128  */
ui_def_but_icon_clear(uiBut * but)4129 void ui_def_but_icon_clear(uiBut *but)
4130 {
4131   but->icon = ICON_NONE;
4132   but->flag &= ~UI_HAS_ICON;
4133   but->drawflag &= ~UI_BUT_ICON_LEFT;
4134 }
4135 
ui_def_but_rna__menu(bContext * UNUSED (C),uiLayout * layout,void * but_p)4136 static void ui_def_but_rna__menu(bContext *UNUSED(C), uiLayout *layout, void *but_p)
4137 {
4138   uiBlock *block = uiLayoutGetBlock(layout);
4139   uiPopupBlockHandle *handle = block->handle;
4140   uiBut *but = (uiBut *)but_p;
4141 
4142   /* see comment in ui_item_enum_expand, re: uiname  */
4143   const EnumPropertyItem *item_array;
4144 
4145   UI_block_flag_enable(block, UI_BLOCK_MOVEMOUSE_QUIT);
4146 
4147   bool free;
4148   RNA_property_enum_items_gettexted(
4149       block->evil_C, &but->rnapoin, but->rnaprop, &item_array, NULL, &free);
4150 
4151   /* we dont want nested rows, cols in menus */
4152   UI_block_layout_set_current(block, layout);
4153 
4154   int totitems = 0;
4155   int categories = 0;
4156   int nbr_entries_nosepr = 0;
4157   for (const EnumPropertyItem *item = item_array; item->identifier; item++, totitems++) {
4158     if (!item->identifier[0]) {
4159       /* inconsistent, but menus with categories do not look good flipped */
4160       if (item->name) {
4161         block->flag |= UI_BLOCK_NO_FLIP;
4162         categories++;
4163         nbr_entries_nosepr++;
4164       }
4165       /* We do not want simple separators in nbr_entries_nosepr count */
4166       continue;
4167     }
4168     nbr_entries_nosepr++;
4169   }
4170 
4171   /* Columns and row estimation. Ignore simple separators here. */
4172   int columns = (nbr_entries_nosepr + 20) / 20;
4173   if (columns < 1) {
4174     columns = 1;
4175   }
4176   if (columns > 8) {
4177     columns = (nbr_entries_nosepr + 25) / 25;
4178   }
4179 
4180   int rows = totitems / columns;
4181   if (rows < 1) {
4182     rows = 1;
4183   }
4184   while (rows * columns < totitems) {
4185     rows++;
4186   }
4187 
4188   const char *title = RNA_property_ui_name(but->rnaprop);
4189 
4190   if (title[0] && (categories == 0) && (block->flag & UI_BLOCK_NO_FLIP)) {
4191     /* Title at the top for menus with categories. */
4192     uiDefBut(
4193         block, UI_BTYPE_LABEL, 0, title, 0, 0, UI_UNIT_X * 5, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
4194     uiItemS(layout);
4195   }
4196 
4197   /* note, item_array[...] is reversed on access */
4198 
4199   /* create items */
4200   uiLayout *split = uiLayoutSplit(layout, 0.0f, false);
4201 
4202   bool new_column;
4203 
4204   int column_end = 0;
4205   uiLayout *column = NULL;
4206   for (int a = 0; a < totitems; a++) {
4207     new_column = (a == column_end);
4208     if (new_column) {
4209       /* start new column, and find out where it ends in advance, so we
4210        * can flip the order of items properly per column */
4211       column_end = totitems;
4212 
4213       for (int b = a + 1; b < totitems; b++) {
4214         const EnumPropertyItem *item = &item_array[b];
4215 
4216         /* new column on N rows or on separation label */
4217         if (((b - a) % rows == 0) || (!item->identifier[0] && item->name)) {
4218           column_end = b;
4219           break;
4220         }
4221       }
4222 
4223       column = uiLayoutColumn(split, false);
4224     }
4225 
4226     const EnumPropertyItem *item = &item_array[a];
4227 
4228     if (new_column && (categories > 0) && item->identifier[0]) {
4229       uiItemL(column, "", ICON_NONE);
4230       uiItemS(column);
4231     }
4232 
4233     if (!item->identifier[0]) {
4234       if (item->name) {
4235         if (item->icon) {
4236           uiItemL(column, item->name, item->icon);
4237         }
4238         else {
4239           /* Do not use uiItemL here, as our root layout is a menu one,
4240            * it will add a fake blank icon! */
4241           uiDefBut(block,
4242                    UI_BTYPE_LABEL,
4243                    0,
4244                    item->name,
4245                    0,
4246                    0,
4247                    UI_UNIT_X * 5,
4248                    UI_UNIT_Y,
4249                    NULL,
4250                    0.0,
4251                    0.0,
4252                    0,
4253                    0,
4254                    "");
4255         }
4256         uiItemS(column);
4257       }
4258     }
4259     else {
4260       if (item->icon) {
4261         uiDefIconTextButI(block,
4262                           UI_BTYPE_BUT_MENU,
4263                           B_NOP,
4264                           item->icon,
4265                           item->name,
4266                           0,
4267                           0,
4268                           UI_UNIT_X * 5,
4269                           UI_UNIT_Y,
4270                           &handle->retvalue,
4271                           item->value,
4272                           0.0,
4273                           0,
4274                           -1,
4275                           item->description);
4276       }
4277       else {
4278         uiDefButI(block,
4279                   UI_BTYPE_BUT_MENU,
4280                   B_NOP,
4281                   item->name,
4282                   0,
4283                   0,
4284                   UI_UNIT_X * 5,
4285                   UI_UNIT_X,
4286                   &handle->retvalue,
4287                   item->value,
4288                   0.0,
4289                   0,
4290                   -1,
4291                   item->description);
4292       }
4293     }
4294   }
4295 
4296   if (title[0] && (categories == 0) && !(block->flag & UI_BLOCK_NO_FLIP)) {
4297     /* Title at the bottom for menus without categories. */
4298     uiItemS(layout);
4299     uiDefBut(
4300         block, UI_BTYPE_LABEL, 0, title, 0, 0, UI_UNIT_X * 5, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
4301   }
4302 
4303   UI_block_layout_set_current(block, layout);
4304 
4305   if (free) {
4306     MEM_freeN((void *)item_array);
4307   }
4308   BLI_assert((block->flag & UI_BLOCK_IS_FLIP) == 0);
4309   block->flag |= UI_BLOCK_IS_FLIP;
4310 }
4311 
ui_def_but_rna__panel_type(bContext * C,uiLayout * layout,void * but_p)4312 static void ui_def_but_rna__panel_type(bContext *C, uiLayout *layout, void *but_p)
4313 {
4314   uiBut *but = but_p;
4315   const char *panel_type = but->func_argN;
4316   PanelType *pt = WM_paneltype_find(panel_type, true);
4317   if (pt) {
4318     ui_item_paneltype_func(C, layout, pt);
4319   }
4320   else {
4321     char msg[256];
4322     SNPRINTF(msg, "Missing Panel: %s", panel_type);
4323     uiItemL(layout, msg, ICON_NONE);
4324   }
4325 }
4326 
ui_but_rna_menu_convert_to_panel_type(uiBut * but,const char * panel_type)4327 void ui_but_rna_menu_convert_to_panel_type(uiBut *but, const char *panel_type)
4328 {
4329   BLI_assert(ELEM(but->type, UI_BTYPE_MENU, UI_BTYPE_COLOR));
4330   //  BLI_assert(but->menu_create_func == ui_def_but_rna__menu);
4331   //  BLI_assert((void *)but->poin == but);
4332   but->menu_create_func = ui_def_but_rna__panel_type;
4333   but->func_argN = BLI_strdup(panel_type);
4334 }
4335 
ui_but_menu_draw_as_popover(const uiBut * but)4336 bool ui_but_menu_draw_as_popover(const uiBut *but)
4337 {
4338   return (but->menu_create_func == ui_def_but_rna__panel_type);
4339 }
4340 
ui_def_but_rna__menu_type(bContext * C,uiLayout * layout,void * but_p)4341 static void ui_def_but_rna__menu_type(bContext *C, uiLayout *layout, void *but_p)
4342 {
4343   uiBut *but = but_p;
4344   const char *menu_type = but->func_argN;
4345   MenuType *mt = WM_menutype_find(menu_type, true);
4346   if (mt) {
4347     ui_item_menutype_func(C, layout, mt);
4348   }
4349   else {
4350     char msg[256];
4351     SNPRINTF(msg, "Missing Menu: %s", menu_type);
4352     uiItemL(layout, msg, ICON_NONE);
4353   }
4354 }
4355 
ui_but_rna_menu_convert_to_menu_type(uiBut * but,const char * menu_type)4356 void ui_but_rna_menu_convert_to_menu_type(uiBut *but, const char *menu_type)
4357 {
4358   BLI_assert(but->type == UI_BTYPE_MENU);
4359   BLI_assert(but->menu_create_func == ui_def_but_rna__menu);
4360   BLI_assert((void *)but->poin == but);
4361   but->menu_create_func = ui_def_but_rna__menu_type;
4362   but->func_argN = BLI_strdup(menu_type);
4363 }
4364 
ui_but_submenu_enable(uiBlock * block,uiBut * but)4365 static void ui_but_submenu_enable(uiBlock *block, uiBut *but)
4366 {
4367   but->flag |= UI_BUT_ICON_SUBMENU;
4368   block->content_hints |= UI_BLOCK_CONTAINS_SUBMENU_BUT;
4369 }
4370 
4371 /**
4372  * ui_def_but_rna_propname and ui_def_but_rna
4373  * both take the same args except for propname vs prop, this is done so we can
4374  * avoid an extra lookup on 'prop' when its already available.
4375  *
4376  * When this kind of change won't disrupt branches, best look into making more
4377  * of our UI functions take prop rather than propname.
4378  */
ui_def_but_rna(uiBlock * block,int type,int retval,const char * str,int x,int y,short width,short height,PointerRNA * ptr,PropertyRNA * prop,int index,float min,float max,float a1,float a2,const char * tip)4379 static uiBut *ui_def_but_rna(uiBlock *block,
4380                              int type,
4381                              int retval,
4382                              const char *str,
4383                              int x,
4384                              int y,
4385                              short width,
4386                              short height,
4387                              PointerRNA *ptr,
4388                              PropertyRNA *prop,
4389                              int index,
4390                              float min,
4391                              float max,
4392                              float a1,
4393                              float a2,
4394                              const char *tip)
4395 {
4396   const PropertyType proptype = RNA_property_type(prop);
4397   int icon = 0;
4398   uiMenuCreateFunc func = NULL;
4399   const bool always_set_a1_a2 = ELEM(type, UI_BTYPE_NUM);
4400 
4401   if (ELEM(type, UI_BTYPE_COLOR, UI_BTYPE_HSVCIRCLE, UI_BTYPE_HSVCUBE)) {
4402     BLI_assert(index == -1);
4403   }
4404 
4405   /* use rna values if parameters are not specified */
4406   if ((proptype == PROP_ENUM) && ELEM(type, UI_BTYPE_MENU, UI_BTYPE_ROW, UI_BTYPE_LISTROW)) {
4407     bool free;
4408     const EnumPropertyItem *item;
4409     RNA_property_enum_items(block->evil_C, ptr, prop, &item, NULL, &free);
4410 
4411     int value;
4412     /* UI_BTYPE_MENU is handled a little differently here */
4413     if (type == UI_BTYPE_MENU) {
4414       value = RNA_property_enum_get(ptr, prop);
4415     }
4416     else {
4417       value = (int)max;
4418     }
4419 
4420     int i = RNA_enum_from_value(item, value);
4421     if (i != -1) {
4422 
4423       if (!str) {
4424         str = item[i].name;
4425 #ifdef WITH_INTERNATIONAL
4426         str = CTX_IFACE_(RNA_property_translation_context(prop), str);
4427 #endif
4428       }
4429 
4430       icon = item[i].icon;
4431     }
4432     else {
4433       if (!str) {
4434         if (type == UI_BTYPE_MENU) {
4435           str = "";
4436         }
4437         else {
4438           str = RNA_property_ui_name(prop);
4439         }
4440       }
4441     }
4442 
4443     if (type == UI_BTYPE_MENU) {
4444       func = ui_def_but_rna__menu;
4445     }
4446 
4447     if (free) {
4448       MEM_freeN((void *)item);
4449     }
4450   }
4451   else {
4452     if (!str) {
4453       str = RNA_property_ui_name(prop);
4454     }
4455     icon = RNA_property_ui_icon(prop);
4456   }
4457 
4458   if (!tip && proptype != PROP_ENUM) {
4459     tip = RNA_property_ui_description(prop);
4460   }
4461 
4462   if (min == max || a1 == -1 || a2 == -1 || always_set_a1_a2) {
4463     if (proptype == PROP_INT) {
4464       int hardmin, hardmax, softmin, softmax, step;
4465 
4466       RNA_property_int_range(ptr, prop, &hardmin, &hardmax);
4467       RNA_property_int_ui_range(ptr, prop, &softmin, &softmax, &step);
4468 
4469       if (!ELEM(type, UI_BTYPE_ROW, UI_BTYPE_LISTROW) && min == max) {
4470         min = hardmin;
4471         max = hardmax;
4472       }
4473       if (a1 == -1 || always_set_a1_a2) {
4474         a1 = step;
4475       }
4476       if (a2 == -1 || always_set_a1_a2) {
4477         a2 = 0;
4478       }
4479     }
4480     else if (proptype == PROP_FLOAT) {
4481       float hardmin, hardmax, softmin, softmax, step, precision;
4482 
4483       RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
4484       RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
4485 
4486       if (!ELEM(type, UI_BTYPE_ROW, UI_BTYPE_LISTROW) && min == max) {
4487         min = hardmin;
4488         max = hardmax;
4489       }
4490       if (a1 == -1 || always_set_a1_a2) {
4491         a1 = step;
4492       }
4493       if (a2 == -1 || always_set_a1_a2) {
4494         a2 = precision;
4495       }
4496     }
4497     else if (proptype == PROP_STRING) {
4498       min = 0;
4499       max = RNA_property_string_maxlength(prop);
4500       /* note, 'max' may be zero (code for dynamically resized array) */
4501     }
4502   }
4503 
4504   /* now create button */
4505   uiBut *but = ui_def_but(
4506       block, type, retval, str, x, y, width, height, NULL, min, max, a1, a2, tip);
4507 
4508   if (but->type == UI_BTYPE_NUM) {
4509     /* Set default values, can be overriden later. */
4510     UI_but_number_step_size_set(but, a1);
4511     UI_but_number_precision_set(but, a2);
4512   }
4513 
4514   but->rnapoin = *ptr;
4515   but->rnaprop = prop;
4516 
4517   if (RNA_property_array_check(but->rnaprop)) {
4518     but->rnaindex = index;
4519   }
4520   else {
4521     but->rnaindex = 0;
4522   }
4523 
4524   if (icon) {
4525     ui_def_but_icon(but, icon, UI_HAS_ICON);
4526   }
4527 
4528   if (type == UI_BTYPE_MENU) {
4529     if (but->emboss == UI_EMBOSS_PULLDOWN) {
4530       ui_but_submenu_enable(block, but);
4531     }
4532   }
4533   else if (type == UI_BTYPE_SEARCH_MENU) {
4534     if (proptype == PROP_POINTER) {
4535       /* Search buttons normally don't get undo, see: T54580. */
4536       but->flag |= UI_BUT_UNDO;
4537     }
4538   }
4539 
4540   const char *info;
4541   if (but->rnapoin.data && !RNA_property_editable_info(&but->rnapoin, prop, &info)) {
4542     UI_but_disable(but, info);
4543   }
4544 
4545   if (but->flag & UI_BUT_UNDO && (ui_but_is_rna_undo(but) == false)) {
4546     but->flag &= ~UI_BUT_UNDO;
4547   }
4548 
4549   /* If this button uses units, calculate the step from this */
4550   if ((proptype == PROP_FLOAT) && ui_but_is_unit(but)) {
4551     if (type == UI_BTYPE_NUM) {
4552       uiButNumber *number_but = (uiButNumber *)but;
4553       number_but->step_size = ui_get_but_step_unit(but, number_but->step_size);
4554     }
4555     else {
4556       but->a1 = ui_get_but_step_unit(but, but->a1);
4557     }
4558   }
4559 
4560   if (func) {
4561     but->menu_create_func = func;
4562     but->poin = (char *)but;
4563   }
4564 
4565   return but;
4566 }
4567 
ui_def_but_rna_propname(uiBlock * block,int type,int retval,const char * str,int x,int y,short width,short height,PointerRNA * ptr,const char * propname,int index,float min,float max,float a1,float a2,const char * tip)4568 static uiBut *ui_def_but_rna_propname(uiBlock *block,
4569                                       int type,
4570                                       int retval,
4571                                       const char *str,
4572                                       int x,
4573                                       int y,
4574                                       short width,
4575                                       short height,
4576                                       PointerRNA *ptr,
4577                                       const char *propname,
4578                                       int index,
4579                                       float min,
4580                                       float max,
4581                                       float a1,
4582                                       float a2,
4583                                       const char *tip)
4584 {
4585   PropertyRNA *prop = RNA_struct_find_property(ptr, propname);
4586 
4587   uiBut *but;
4588   if (prop) {
4589     but = ui_def_but_rna(
4590         block, type, retval, str, x, y, width, height, ptr, prop, index, min, max, a1, a2, tip);
4591   }
4592   else {
4593     but = ui_def_but(
4594         block, type, retval, propname, x, y, width, height, NULL, min, max, a1, a2, tip);
4595 
4596     UI_but_disable(but, "Unknown Property.");
4597   }
4598 
4599   return but;
4600 }
4601 
ui_def_but_operator_ptr(uiBlock * block,int type,wmOperatorType * ot,int opcontext,const char * str,int x,int y,short width,short height,const char * tip)4602 static uiBut *ui_def_but_operator_ptr(uiBlock *block,
4603                                       int type,
4604                                       wmOperatorType *ot,
4605                                       int opcontext,
4606                                       const char *str,
4607                                       int x,
4608                                       int y,
4609                                       short width,
4610                                       short height,
4611                                       const char *tip)
4612 {
4613   if (!str) {
4614     if (ot && ot->srna) {
4615       str = WM_operatortype_name(ot, NULL);
4616     }
4617     else {
4618       str = "";
4619     }
4620   }
4621 
4622   if ((!tip || tip[0] == '\0') && ot && ot->srna && !ot->get_description) {
4623     tip = RNA_struct_ui_description(ot->srna);
4624   }
4625 
4626   uiBut *but = ui_def_but(block, type, -1, str, x, y, width, height, NULL, 0, 0, 0, 0, tip);
4627   but->optype = ot;
4628   but->opcontext = opcontext;
4629   but->flag &= ~UI_BUT_UNDO; /* no need for ui_but_is_rna_undo(), we never need undo here */
4630 
4631   if (!ot) {
4632     UI_but_disable(but, "");
4633   }
4634 
4635   return but;
4636 }
4637 
uiDefBut(uiBlock * block,int type,int retval,const char * str,int x,int y,short width,short height,void * poin,float min,float max,float a1,float a2,const char * tip)4638 uiBut *uiDefBut(uiBlock *block,
4639                 int type,
4640                 int retval,
4641                 const char *str,
4642                 int x,
4643                 int y,
4644                 short width,
4645                 short height,
4646                 void *poin,
4647                 float min,
4648                 float max,
4649                 float a1,
4650                 float a2,
4651                 const char *tip)
4652 {
4653   uiBut *but = ui_def_but(
4654       block, type, retval, str, x, y, width, height, poin, min, max, a1, a2, tip);
4655 
4656   ui_but_update(but);
4657 
4658   return but;
4659 }
4660 
uiDefButImage(uiBlock * block,void * imbuf,int x,int y,short width,short height,const uchar color[4])4661 uiBut *uiDefButImage(
4662     uiBlock *block, void *imbuf, int x, int y, short width, short height, const uchar color[4])
4663 {
4664   uiBut *but = ui_def_but(
4665       block, UI_BTYPE_IMAGE, 0, "", x, y, width, height, imbuf, 0, 0, 0, 0, "");
4666   if (color) {
4667     copy_v4_v4_uchar(but->col, color);
4668   }
4669   else {
4670     but->col[0] = 255;
4671     but->col[1] = 255;
4672     but->col[2] = 255;
4673     but->col[3] = 255;
4674   }
4675   ui_but_update(but);
4676   return but;
4677 }
4678 
uiDefButAlert(uiBlock * block,int icon,int x,int y,short width,short height)4679 uiBut *uiDefButAlert(uiBlock *block, int icon, int x, int y, short width, short height)
4680 {
4681   struct ImBuf *ibuf = UI_icon_alert_imbuf_get(icon);
4682   bTheme *btheme = UI_GetTheme();
4683   return uiDefButImage(block, ibuf, x, y, width, height, btheme->tui.wcol_menu_back.text);
4684 }
4685 
4686 /**
4687  * if \a _x_ is a power of two (only one bit) return the power,
4688  * otherwise return -1.
4689  *
4690  * for powers of two:
4691  * \code{.c}
4692  *     ((1 << findBitIndex(x)) == x);
4693  * \endcode
4694  */
findBitIndex(uint x)4695 static int findBitIndex(uint x)
4696 {
4697   if (!x || !is_power_of_2_i(x)) { /* is_power_of_2_i(x) strips lowest bit */
4698     return -1;
4699   }
4700   int idx = 0;
4701 
4702   if (x & 0xFFFF0000) {
4703     idx += 16;
4704     x >>= 16;
4705   }
4706   if (x & 0xFF00) {
4707     idx += 8;
4708     x >>= 8;
4709   }
4710   if (x & 0xF0) {
4711     idx += 4;
4712     x >>= 4;
4713   }
4714   if (x & 0xC) {
4715     idx += 2;
4716     x >>= 2;
4717   }
4718   if (x & 0x2) {
4719     idx += 1;
4720   }
4721 
4722   return idx;
4723 }
4724 
4725 /* autocomplete helper functions */
4726 struct AutoComplete {
4727   size_t maxlen;
4728   int matches;
4729   char *truncate;
4730   const char *startname;
4731 };
4732 
UI_autocomplete_begin(const char * startname,size_t maxlen)4733 AutoComplete *UI_autocomplete_begin(const char *startname, size_t maxlen)
4734 {
4735   AutoComplete *autocpl;
4736 
4737   autocpl = MEM_callocN(sizeof(AutoComplete), "AutoComplete");
4738   autocpl->maxlen = maxlen;
4739   autocpl->matches = 0;
4740   autocpl->truncate = MEM_callocN(sizeof(char) * maxlen, "AutoCompleteTruncate");
4741   autocpl->startname = startname;
4742 
4743   return autocpl;
4744 }
4745 
UI_autocomplete_update_name(AutoComplete * autocpl,const char * name)4746 void UI_autocomplete_update_name(AutoComplete *autocpl, const char *name)
4747 {
4748   char *truncate = autocpl->truncate;
4749   const char *startname = autocpl->startname;
4750   int match_index = 0;
4751   for (int a = 0; a < autocpl->maxlen - 1; a++) {
4752     if (startname[a] == 0 || startname[a] != name[a]) {
4753       match_index = a;
4754       break;
4755     }
4756   }
4757 
4758   /* found a match */
4759   if (startname[match_index] == 0) {
4760     autocpl->matches++;
4761     /* first match */
4762     if (truncate[0] == 0) {
4763       BLI_strncpy(truncate, name, autocpl->maxlen);
4764     }
4765     else {
4766       /* remove from truncate what is not in bone->name */
4767       for (int a = 0; a < autocpl->maxlen - 1; a++) {
4768         if (name[a] == 0) {
4769           truncate[a] = 0;
4770           break;
4771         }
4772         if (truncate[a] != name[a]) {
4773           truncate[a] = 0;
4774         }
4775       }
4776     }
4777   }
4778 }
4779 
UI_autocomplete_end(AutoComplete * autocpl,char * autoname)4780 int UI_autocomplete_end(AutoComplete *autocpl, char *autoname)
4781 {
4782   int match = AUTOCOMPLETE_NO_MATCH;
4783   if (autocpl->truncate[0]) {
4784     if (autocpl->matches == 1) {
4785       match = AUTOCOMPLETE_FULL_MATCH;
4786     }
4787     else {
4788       match = AUTOCOMPLETE_PARTIAL_MATCH;
4789     }
4790     BLI_strncpy(autoname, autocpl->truncate, autocpl->maxlen);
4791   }
4792   else {
4793     if (autoname != autocpl->startname) { /* don't copy a string over its self */
4794       BLI_strncpy(autoname, autocpl->startname, autocpl->maxlen);
4795     }
4796   }
4797 
4798   MEM_freeN(autocpl->truncate);
4799   MEM_freeN(autocpl);
4800   return match;
4801 }
4802 
ui_but_update_and_icon_set(uiBut * but,int icon)4803 static void ui_but_update_and_icon_set(uiBut *but, int icon)
4804 {
4805   if (icon) {
4806     ui_def_but_icon(but, icon, UI_HAS_ICON);
4807   }
4808 
4809   ui_but_update(but);
4810 }
4811 
uiDefButBit(uiBlock * block,int type,int bit,int retval,const char * str,int x,int y,short width,short height,void * poin,float min,float max,float a1,float a2,const char * tip)4812 static uiBut *uiDefButBit(uiBlock *block,
4813                           int type,
4814                           int bit,
4815                           int retval,
4816                           const char *str,
4817                           int x,
4818                           int y,
4819                           short width,
4820                           short height,
4821                           void *poin,
4822                           float min,
4823                           float max,
4824                           float a1,
4825                           float a2,
4826                           const char *tip)
4827 {
4828   const int bitIdx = findBitIndex(bit);
4829   if (bitIdx == -1) {
4830     return NULL;
4831   }
4832   return uiDefBut(block,
4833                   type | UI_BUT_POIN_BIT | bitIdx,
4834                   retval,
4835                   str,
4836                   x,
4837                   y,
4838                   width,
4839                   height,
4840                   poin,
4841                   min,
4842                   max,
4843                   a1,
4844                   a2,
4845                   tip);
4846 }
uiDefButF(uiBlock * block,int type,int retval,const char * str,int x,int y,short width,short height,float * poin,float min,float max,float a1,float a2,const char * tip)4847 uiBut *uiDefButF(uiBlock *block,
4848                  int type,
4849                  int retval,
4850                  const char *str,
4851                  int x,
4852                  int y,
4853                  short width,
4854                  short height,
4855                  float *poin,
4856                  float min,
4857                  float max,
4858                  float a1,
4859                  float a2,
4860                  const char *tip)
4861 {
4862   return uiDefBut(block,
4863                   type | UI_BUT_POIN_FLOAT,
4864                   retval,
4865                   str,
4866                   x,
4867                   y,
4868                   width,
4869                   height,
4870                   (void *)poin,
4871                   min,
4872                   max,
4873                   a1,
4874                   a2,
4875                   tip);
4876 }
uiDefButBitF(uiBlock * block,int type,int bit,int retval,const char * str,int x,int y,short width,short height,float * poin,float min,float max,float a1,float a2,const char * tip)4877 uiBut *uiDefButBitF(uiBlock *block,
4878                     int type,
4879                     int bit,
4880                     int retval,
4881                     const char *str,
4882                     int x,
4883                     int y,
4884                     short width,
4885                     short height,
4886                     float *poin,
4887                     float min,
4888                     float max,
4889                     float a1,
4890                     float a2,
4891                     const char *tip)
4892 {
4893   return uiDefButBit(block,
4894                      type | UI_BUT_POIN_FLOAT,
4895                      bit,
4896                      retval,
4897                      str,
4898                      x,
4899                      y,
4900                      width,
4901                      height,
4902                      (void *)poin,
4903                      min,
4904                      max,
4905                      a1,
4906                      a2,
4907                      tip);
4908 }
uiDefButI(uiBlock * block,int type,int retval,const char * str,int x,int y,short width,short height,int * poin,float min,float max,float a1,float a2,const char * tip)4909 uiBut *uiDefButI(uiBlock *block,
4910                  int type,
4911                  int retval,
4912                  const char *str,
4913                  int x,
4914                  int y,
4915                  short width,
4916                  short height,
4917                  int *poin,
4918                  float min,
4919                  float max,
4920                  float a1,
4921                  float a2,
4922                  const char *tip)
4923 {
4924   return uiDefBut(block,
4925                   type | UI_BUT_POIN_INT,
4926                   retval,
4927                   str,
4928                   x,
4929                   y,
4930                   width,
4931                   height,
4932                   (void *)poin,
4933                   min,
4934                   max,
4935                   a1,
4936                   a2,
4937                   tip);
4938 }
uiDefButBitI(uiBlock * block,int type,int bit,int retval,const char * str,int x,int y,short width,short height,int * poin,float min,float max,float a1,float a2,const char * tip)4939 uiBut *uiDefButBitI(uiBlock *block,
4940                     int type,
4941                     int bit,
4942                     int retval,
4943                     const char *str,
4944                     int x,
4945                     int y,
4946                     short width,
4947                     short height,
4948                     int *poin,
4949                     float min,
4950                     float max,
4951                     float a1,
4952                     float a2,
4953                     const char *tip)
4954 {
4955   return uiDefButBit(block,
4956                      type | UI_BUT_POIN_INT,
4957                      bit,
4958                      retval,
4959                      str,
4960                      x,
4961                      y,
4962                      width,
4963                      height,
4964                      (void *)poin,
4965                      min,
4966                      max,
4967                      a1,
4968                      a2,
4969                      tip);
4970 }
uiDefButS(uiBlock * block,int type,int retval,const char * str,int x,int y,short width,short height,short * poin,float min,float max,float a1,float a2,const char * tip)4971 uiBut *uiDefButS(uiBlock *block,
4972                  int type,
4973                  int retval,
4974                  const char *str,
4975                  int x,
4976                  int y,
4977                  short width,
4978                  short height,
4979                  short *poin,
4980                  float min,
4981                  float max,
4982                  float a1,
4983                  float a2,
4984                  const char *tip)
4985 {
4986   return uiDefBut(block,
4987                   type | UI_BUT_POIN_SHORT,
4988                   retval,
4989                   str,
4990                   x,
4991                   y,
4992                   width,
4993                   height,
4994                   (void *)poin,
4995                   min,
4996                   max,
4997                   a1,
4998                   a2,
4999                   tip);
5000 }
uiDefButBitS(uiBlock * block,int type,int bit,int retval,const char * str,int x,int y,short width,short height,short * poin,float min,float max,float a1,float a2,const char * tip)5001 uiBut *uiDefButBitS(uiBlock *block,
5002                     int type,
5003                     int bit,
5004                     int retval,
5005                     const char *str,
5006                     int x,
5007                     int y,
5008                     short width,
5009                     short height,
5010                     short *poin,
5011                     float min,
5012                     float max,
5013                     float a1,
5014                     float a2,
5015                     const char *tip)
5016 {
5017   return uiDefButBit(block,
5018                      type | UI_BUT_POIN_SHORT,
5019                      bit,
5020                      retval,
5021                      str,
5022                      x,
5023                      y,
5024                      width,
5025                      height,
5026                      (void *)poin,
5027                      min,
5028                      max,
5029                      a1,
5030                      a2,
5031                      tip);
5032 }
uiDefButC(uiBlock * block,int type,int retval,const char * str,int x,int y,short width,short height,char * poin,float min,float max,float a1,float a2,const char * tip)5033 uiBut *uiDefButC(uiBlock *block,
5034                  int type,
5035                  int retval,
5036                  const char *str,
5037                  int x,
5038                  int y,
5039                  short width,
5040                  short height,
5041                  char *poin,
5042                  float min,
5043                  float max,
5044                  float a1,
5045                  float a2,
5046                  const char *tip)
5047 {
5048   return uiDefBut(block,
5049                   type | UI_BUT_POIN_CHAR,
5050                   retval,
5051                   str,
5052                   x,
5053                   y,
5054                   width,
5055                   height,
5056                   (void *)poin,
5057                   min,
5058                   max,
5059                   a1,
5060                   a2,
5061                   tip);
5062 }
uiDefButBitC(uiBlock * block,int type,int bit,int retval,const char * str,int x,int y,short width,short height,char * poin,float min,float max,float a1,float a2,const char * tip)5063 uiBut *uiDefButBitC(uiBlock *block,
5064                     int type,
5065                     int bit,
5066                     int retval,
5067                     const char *str,
5068                     int x,
5069                     int y,
5070                     short width,
5071                     short height,
5072                     char *poin,
5073                     float min,
5074                     float max,
5075                     float a1,
5076                     float a2,
5077                     const char *tip)
5078 {
5079   return uiDefButBit(block,
5080                      type | UI_BUT_POIN_CHAR,
5081                      bit,
5082                      retval,
5083                      str,
5084                      x,
5085                      y,
5086                      width,
5087                      height,
5088                      (void *)poin,
5089                      min,
5090                      max,
5091                      a1,
5092                      a2,
5093                      tip);
5094 }
uiDefButR(uiBlock * block,int type,int retval,const char * str,int x,int y,short width,short height,PointerRNA * ptr,const char * propname,int index,float min,float max,float a1,float a2,const char * tip)5095 uiBut *uiDefButR(uiBlock *block,
5096                  int type,
5097                  int retval,
5098                  const char *str,
5099                  int x,
5100                  int y,
5101                  short width,
5102                  short height,
5103                  PointerRNA *ptr,
5104                  const char *propname,
5105                  int index,
5106                  float min,
5107                  float max,
5108                  float a1,
5109                  float a2,
5110                  const char *tip)
5111 {
5112   uiBut *but = ui_def_but_rna_propname(
5113       block, type, retval, str, x, y, width, height, ptr, propname, index, min, max, a1, a2, tip);
5114   ui_but_update(but);
5115   return but;
5116 }
uiDefButR_prop(uiBlock * block,int type,int retval,const char * str,int x,int y,short width,short height,PointerRNA * ptr,PropertyRNA * prop,int index,float min,float max,float a1,float a2,const char * tip)5117 uiBut *uiDefButR_prop(uiBlock *block,
5118                       int type,
5119                       int retval,
5120                       const char *str,
5121                       int x,
5122                       int y,
5123                       short width,
5124                       short height,
5125                       PointerRNA *ptr,
5126                       PropertyRNA *prop,
5127                       int index,
5128                       float min,
5129                       float max,
5130                       float a1,
5131                       float a2,
5132                       const char *tip)
5133 {
5134   uiBut *but = ui_def_but_rna(
5135       block, type, retval, str, x, y, width, height, ptr, prop, index, min, max, a1, a2, tip);
5136   ui_but_update(but);
5137   return but;
5138 }
5139 
uiDefButO_ptr(uiBlock * block,int type,wmOperatorType * ot,int opcontext,const char * str,int x,int y,short width,short height,const char * tip)5140 uiBut *uiDefButO_ptr(uiBlock *block,
5141                      int type,
5142                      wmOperatorType *ot,
5143                      int opcontext,
5144                      const char *str,
5145                      int x,
5146                      int y,
5147                      short width,
5148                      short height,
5149                      const char *tip)
5150 {
5151   uiBut *but = ui_def_but_operator_ptr(block, type, ot, opcontext, str, x, y, width, height, tip);
5152   ui_but_update(but);
5153   return but;
5154 }
uiDefButO(uiBlock * block,int type,const char * opname,int opcontext,const char * str,int x,int y,short width,short height,const char * tip)5155 uiBut *uiDefButO(uiBlock *block,
5156                  int type,
5157                  const char *opname,
5158                  int opcontext,
5159                  const char *str,
5160                  int x,
5161                  int y,
5162                  short width,
5163                  short height,
5164                  const char *tip)
5165 {
5166   wmOperatorType *ot = WM_operatortype_find(opname, 0);
5167   if (str == NULL && ot == NULL) {
5168     str = opname;
5169   }
5170   return uiDefButO_ptr(block, type, ot, opcontext, str, x, y, width, height, tip);
5171 }
5172 
5173 /* if a1==1.0 then a2 is an extra icon blending factor (alpha 0.0 - 1.0) */
uiDefIconBut(uiBlock * block,int type,int retval,int icon,int x,int y,short width,short height,void * poin,float min,float max,float a1,float a2,const char * tip)5174 uiBut *uiDefIconBut(uiBlock *block,
5175                     int type,
5176                     int retval,
5177                     int icon,
5178                     int x,
5179                     int y,
5180                     short width,
5181                     short height,
5182                     void *poin,
5183                     float min,
5184                     float max,
5185                     float a1,
5186                     float a2,
5187                     const char *tip)
5188 {
5189   uiBut *but = ui_def_but(
5190       block, type, retval, "", x, y, width, height, poin, min, max, a1, a2, tip);
5191   ui_but_update_and_icon_set(but, icon);
5192   return but;
5193 }
uiDefIconButBit(uiBlock * block,int type,int bit,int retval,int icon,int x,int y,short width,short height,void * poin,float min,float max,float a1,float a2,const char * tip)5194 static uiBut *uiDefIconButBit(uiBlock *block,
5195                               int type,
5196                               int bit,
5197                               int retval,
5198                               int icon,
5199                               int x,
5200                               int y,
5201                               short width,
5202                               short height,
5203                               void *poin,
5204                               float min,
5205                               float max,
5206                               float a1,
5207                               float a2,
5208                               const char *tip)
5209 {
5210   const int bitIdx = findBitIndex(bit);
5211   if (bitIdx == -1) {
5212     return NULL;
5213   }
5214   return uiDefIconBut(block,
5215                       type | UI_BUT_POIN_BIT | bitIdx,
5216                       retval,
5217                       icon,
5218                       x,
5219                       y,
5220                       width,
5221                       height,
5222                       poin,
5223                       min,
5224                       max,
5225                       a1,
5226                       a2,
5227                       tip);
5228 }
5229 
uiDefIconButF(uiBlock * block,int type,int retval,int icon,int x,int y,short width,short height,float * poin,float min,float max,float a1,float a2,const char * tip)5230 uiBut *uiDefIconButF(uiBlock *block,
5231                      int type,
5232                      int retval,
5233                      int icon,
5234                      int x,
5235                      int y,
5236                      short width,
5237                      short height,
5238                      float *poin,
5239                      float min,
5240                      float max,
5241                      float a1,
5242                      float a2,
5243                      const char *tip)
5244 {
5245   return uiDefIconBut(block,
5246                       type | UI_BUT_POIN_FLOAT,
5247                       retval,
5248                       icon,
5249                       x,
5250                       y,
5251                       width,
5252                       height,
5253                       (void *)poin,
5254                       min,
5255                       max,
5256                       a1,
5257                       a2,
5258                       tip);
5259 }
uiDefIconButBitF(uiBlock * block,int type,int bit,int retval,int icon,int x,int y,short width,short height,float * poin,float min,float max,float a1,float a2,const char * tip)5260 uiBut *uiDefIconButBitF(uiBlock *block,
5261                         int type,
5262                         int bit,
5263                         int retval,
5264                         int icon,
5265                         int x,
5266                         int y,
5267                         short width,
5268                         short height,
5269                         float *poin,
5270                         float min,
5271                         float max,
5272                         float a1,
5273                         float a2,
5274                         const char *tip)
5275 {
5276   return uiDefIconButBit(block,
5277                          type | UI_BUT_POIN_FLOAT,
5278                          bit,
5279                          retval,
5280                          icon,
5281                          x,
5282                          y,
5283                          width,
5284                          height,
5285                          (void *)poin,
5286                          min,
5287                          max,
5288                          a1,
5289                          a2,
5290                          tip);
5291 }
uiDefIconButI(uiBlock * block,int type,int retval,int icon,int x,int y,short width,short height,int * poin,float min,float max,float a1,float a2,const char * tip)5292 uiBut *uiDefIconButI(uiBlock *block,
5293                      int type,
5294                      int retval,
5295                      int icon,
5296                      int x,
5297                      int y,
5298                      short width,
5299                      short height,
5300                      int *poin,
5301                      float min,
5302                      float max,
5303                      float a1,
5304                      float a2,
5305                      const char *tip)
5306 {
5307   return uiDefIconBut(block,
5308                       type | UI_BUT_POIN_INT,
5309                       retval,
5310                       icon,
5311                       x,
5312                       y,
5313                       width,
5314                       height,
5315                       (void *)poin,
5316                       min,
5317                       max,
5318                       a1,
5319                       a2,
5320                       tip);
5321 }
uiDefIconButBitI(uiBlock * block,int type,int bit,int retval,int icon,int x,int y,short width,short height,int * poin,float min,float max,float a1,float a2,const char * tip)5322 uiBut *uiDefIconButBitI(uiBlock *block,
5323                         int type,
5324                         int bit,
5325                         int retval,
5326                         int icon,
5327                         int x,
5328                         int y,
5329                         short width,
5330                         short height,
5331                         int *poin,
5332                         float min,
5333                         float max,
5334                         float a1,
5335                         float a2,
5336                         const char *tip)
5337 {
5338   return uiDefIconButBit(block,
5339                          type | UI_BUT_POIN_INT,
5340                          bit,
5341                          retval,
5342                          icon,
5343                          x,
5344                          y,
5345                          width,
5346                          height,
5347                          (void *)poin,
5348                          min,
5349                          max,
5350                          a1,
5351                          a2,
5352                          tip);
5353 }
uiDefIconButS(uiBlock * block,int type,int retval,int icon,int x,int y,short width,short height,short * poin,float min,float max,float a1,float a2,const char * tip)5354 uiBut *uiDefIconButS(uiBlock *block,
5355                      int type,
5356                      int retval,
5357                      int icon,
5358                      int x,
5359                      int y,
5360                      short width,
5361                      short height,
5362                      short *poin,
5363                      float min,
5364                      float max,
5365                      float a1,
5366                      float a2,
5367                      const char *tip)
5368 {
5369   return uiDefIconBut(block,
5370                       type | UI_BUT_POIN_SHORT,
5371                       retval,
5372                       icon,
5373                       x,
5374                       y,
5375                       width,
5376                       height,
5377                       (void *)poin,
5378                       min,
5379                       max,
5380                       a1,
5381                       a2,
5382                       tip);
5383 }
uiDefIconButBitS(uiBlock * block,int type,int bit,int retval,int icon,int x,int y,short width,short height,short * poin,float min,float max,float a1,float a2,const char * tip)5384 uiBut *uiDefIconButBitS(uiBlock *block,
5385                         int type,
5386                         int bit,
5387                         int retval,
5388                         int icon,
5389                         int x,
5390                         int y,
5391                         short width,
5392                         short height,
5393                         short *poin,
5394                         float min,
5395                         float max,
5396                         float a1,
5397                         float a2,
5398                         const char *tip)
5399 {
5400   return uiDefIconButBit(block,
5401                          type | UI_BUT_POIN_SHORT,
5402                          bit,
5403                          retval,
5404                          icon,
5405                          x,
5406                          y,
5407                          width,
5408                          height,
5409                          (void *)poin,
5410                          min,
5411                          max,
5412                          a1,
5413                          a2,
5414                          tip);
5415 }
uiDefIconButC(uiBlock * block,int type,int retval,int icon,int x,int y,short width,short height,char * poin,float min,float max,float a1,float a2,const char * tip)5416 uiBut *uiDefIconButC(uiBlock *block,
5417                      int type,
5418                      int retval,
5419                      int icon,
5420                      int x,
5421                      int y,
5422                      short width,
5423                      short height,
5424                      char *poin,
5425                      float min,
5426                      float max,
5427                      float a1,
5428                      float a2,
5429                      const char *tip)
5430 {
5431   return uiDefIconBut(block,
5432                       type | UI_BUT_POIN_CHAR,
5433                       retval,
5434                       icon,
5435                       x,
5436                       y,
5437                       width,
5438                       height,
5439                       (void *)poin,
5440                       min,
5441                       max,
5442                       a1,
5443                       a2,
5444                       tip);
5445 }
uiDefIconButBitC(uiBlock * block,int type,int bit,int retval,int icon,int x,int y,short width,short height,char * poin,float min,float max,float a1,float a2,const char * tip)5446 uiBut *uiDefIconButBitC(uiBlock *block,
5447                         int type,
5448                         int bit,
5449                         int retval,
5450                         int icon,
5451                         int x,
5452                         int y,
5453                         short width,
5454                         short height,
5455                         char *poin,
5456                         float min,
5457                         float max,
5458                         float a1,
5459                         float a2,
5460                         const char *tip)
5461 {
5462   return uiDefIconButBit(block,
5463                          type | UI_BUT_POIN_CHAR,
5464                          bit,
5465                          retval,
5466                          icon,
5467                          x,
5468                          y,
5469                          width,
5470                          height,
5471                          (void *)poin,
5472                          min,
5473                          max,
5474                          a1,
5475                          a2,
5476                          tip);
5477 }
uiDefIconButR(uiBlock * block,int type,int retval,int icon,int x,int y,short width,short height,PointerRNA * ptr,const char * propname,int index,float min,float max,float a1,float a2,const char * tip)5478 uiBut *uiDefIconButR(uiBlock *block,
5479                      int type,
5480                      int retval,
5481                      int icon,
5482                      int x,
5483                      int y,
5484                      short width,
5485                      short height,
5486                      PointerRNA *ptr,
5487                      const char *propname,
5488                      int index,
5489                      float min,
5490                      float max,
5491                      float a1,
5492                      float a2,
5493                      const char *tip)
5494 {
5495   uiBut *but = ui_def_but_rna_propname(
5496       block, type, retval, "", x, y, width, height, ptr, propname, index, min, max, a1, a2, tip);
5497   ui_but_update_and_icon_set(but, icon);
5498   return but;
5499 }
uiDefIconButR_prop(uiBlock * block,int type,int retval,int icon,int x,int y,short width,short height,PointerRNA * ptr,PropertyRNA * prop,int index,float min,float max,float a1,float a2,const char * tip)5500 uiBut *uiDefIconButR_prop(uiBlock *block,
5501                           int type,
5502                           int retval,
5503                           int icon,
5504                           int x,
5505                           int y,
5506                           short width,
5507                           short height,
5508                           PointerRNA *ptr,
5509                           PropertyRNA *prop,
5510                           int index,
5511                           float min,
5512                           float max,
5513                           float a1,
5514                           float a2,
5515                           const char *tip)
5516 {
5517   uiBut *but = ui_def_but_rna(
5518       block, type, retval, "", x, y, width, height, ptr, prop, index, min, max, a1, a2, tip);
5519   ui_but_update_and_icon_set(but, icon);
5520   return but;
5521 }
5522 
uiDefIconButO_ptr(uiBlock * block,int type,wmOperatorType * ot,int opcontext,int icon,int x,int y,short width,short height,const char * tip)5523 uiBut *uiDefIconButO_ptr(uiBlock *block,
5524                          int type,
5525                          wmOperatorType *ot,
5526                          int opcontext,
5527                          int icon,
5528                          int x,
5529                          int y,
5530                          short width,
5531                          short height,
5532                          const char *tip)
5533 {
5534   uiBut *but = ui_def_but_operator_ptr(block, type, ot, opcontext, "", x, y, width, height, tip);
5535   ui_but_update_and_icon_set(but, icon);
5536   return but;
5537 }
uiDefIconButO(uiBlock * block,int type,const char * opname,int opcontext,int icon,int x,int y,short width,short height,const char * tip)5538 uiBut *uiDefIconButO(uiBlock *block,
5539                      int type,
5540                      const char *opname,
5541                      int opcontext,
5542                      int icon,
5543                      int x,
5544                      int y,
5545                      short width,
5546                      short height,
5547                      const char *tip)
5548 {
5549   wmOperatorType *ot = WM_operatortype_find(opname, 0);
5550   return uiDefIconButO_ptr(block, type, ot, opcontext, icon, x, y, width, height, tip);
5551 }
5552 
5553 /* Button containing both string label and icon */
uiDefIconTextBut(uiBlock * block,int type,int retval,int icon,const char * str,int x,int y,short width,short height,void * poin,float min,float max,float a1,float a2,const char * tip)5554 uiBut *uiDefIconTextBut(uiBlock *block,
5555                         int type,
5556                         int retval,
5557                         int icon,
5558                         const char *str,
5559                         int x,
5560                         int y,
5561                         short width,
5562                         short height,
5563                         void *poin,
5564                         float min,
5565                         float max,
5566                         float a1,
5567                         float a2,
5568                         const char *tip)
5569 {
5570   uiBut *but = ui_def_but(
5571       block, type, retval, str, x, y, width, height, poin, min, max, a1, a2, tip);
5572   ui_but_update_and_icon_set(but, icon);
5573   but->drawflag |= UI_BUT_ICON_LEFT;
5574   return but;
5575 }
uiDefIconTextButBit(uiBlock * block,int type,int bit,int retval,int icon,const char * str,int x,int y,short width,short height,void * poin,float min,float max,float a1,float a2,const char * tip)5576 static uiBut *uiDefIconTextButBit(uiBlock *block,
5577                                   int type,
5578                                   int bit,
5579                                   int retval,
5580                                   int icon,
5581                                   const char *str,
5582                                   int x,
5583                                   int y,
5584                                   short width,
5585                                   short height,
5586                                   void *poin,
5587                                   float min,
5588                                   float max,
5589                                   float a1,
5590                                   float a2,
5591                                   const char *tip)
5592 {
5593   const int bitIdx = findBitIndex(bit);
5594   if (bitIdx == -1) {
5595     return NULL;
5596   }
5597   return uiDefIconTextBut(block,
5598                           type | UI_BUT_POIN_BIT | bitIdx,
5599                           retval,
5600                           icon,
5601                           str,
5602                           x,
5603                           y,
5604                           width,
5605                           height,
5606                           poin,
5607                           min,
5608                           max,
5609                           a1,
5610                           a2,
5611                           tip);
5612 }
5613 
uiDefIconTextButF(uiBlock * block,int type,int retval,int icon,const char * str,int x,int y,short width,short height,float * poin,float min,float max,float a1,float a2,const char * tip)5614 uiBut *uiDefIconTextButF(uiBlock *block,
5615                          int type,
5616                          int retval,
5617                          int icon,
5618                          const char *str,
5619                          int x,
5620                          int y,
5621                          short width,
5622                          short height,
5623                          float *poin,
5624                          float min,
5625                          float max,
5626                          float a1,
5627                          float a2,
5628                          const char *tip)
5629 {
5630   return uiDefIconTextBut(block,
5631                           type | UI_BUT_POIN_FLOAT,
5632                           retval,
5633                           icon,
5634                           str,
5635                           x,
5636                           y,
5637                           width,
5638                           height,
5639                           (void *)poin,
5640                           min,
5641                           max,
5642                           a1,
5643                           a2,
5644                           tip);
5645 }
uiDefIconTextButBitF(uiBlock * block,int type,int bit,int retval,int icon,const char * str,int x,int y,short width,short height,float * poin,float min,float max,float a1,float a2,const char * tip)5646 uiBut *uiDefIconTextButBitF(uiBlock *block,
5647                             int type,
5648                             int bit,
5649                             int retval,
5650                             int icon,
5651                             const char *str,
5652                             int x,
5653                             int y,
5654                             short width,
5655                             short height,
5656                             float *poin,
5657                             float min,
5658                             float max,
5659                             float a1,
5660                             float a2,
5661                             const char *tip)
5662 {
5663   return uiDefIconTextButBit(block,
5664                              type | UI_BUT_POIN_FLOAT,
5665                              bit,
5666                              retval,
5667                              icon,
5668                              str,
5669                              x,
5670                              y,
5671                              width,
5672                              height,
5673                              (void *)poin,
5674                              min,
5675                              max,
5676                              a1,
5677                              a2,
5678                              tip);
5679 }
uiDefIconTextButI(uiBlock * block,int type,int retval,int icon,const char * str,int x,int y,short width,short height,int * poin,float min,float max,float a1,float a2,const char * tip)5680 uiBut *uiDefIconTextButI(uiBlock *block,
5681                          int type,
5682                          int retval,
5683                          int icon,
5684                          const char *str,
5685                          int x,
5686                          int y,
5687                          short width,
5688                          short height,
5689                          int *poin,
5690                          float min,
5691                          float max,
5692                          float a1,
5693                          float a2,
5694                          const char *tip)
5695 {
5696   return uiDefIconTextBut(block,
5697                           type | UI_BUT_POIN_INT,
5698                           retval,
5699                           icon,
5700                           str,
5701                           x,
5702                           y,
5703                           width,
5704                           height,
5705                           (void *)poin,
5706                           min,
5707                           max,
5708                           a1,
5709                           a2,
5710                           tip);
5711 }
uiDefIconTextButBitI(uiBlock * block,int type,int bit,int retval,int icon,const char * str,int x,int y,short width,short height,int * poin,float min,float max,float a1,float a2,const char * tip)5712 uiBut *uiDefIconTextButBitI(uiBlock *block,
5713                             int type,
5714                             int bit,
5715                             int retval,
5716                             int icon,
5717                             const char *str,
5718                             int x,
5719                             int y,
5720                             short width,
5721                             short height,
5722                             int *poin,
5723                             float min,
5724                             float max,
5725                             float a1,
5726                             float a2,
5727                             const char *tip)
5728 {
5729   return uiDefIconTextButBit(block,
5730                              type | UI_BUT_POIN_INT,
5731                              bit,
5732                              retval,
5733                              icon,
5734                              str,
5735                              x,
5736                              y,
5737                              width,
5738                              height,
5739                              (void *)poin,
5740                              min,
5741                              max,
5742                              a1,
5743                              a2,
5744                              tip);
5745 }
uiDefIconTextButS(uiBlock * block,int type,int retval,int icon,const char * str,int x,int y,short width,short height,short * poin,float min,float max,float a1,float a2,const char * tip)5746 uiBut *uiDefIconTextButS(uiBlock *block,
5747                          int type,
5748                          int retval,
5749                          int icon,
5750                          const char *str,
5751                          int x,
5752                          int y,
5753                          short width,
5754                          short height,
5755                          short *poin,
5756                          float min,
5757                          float max,
5758                          float a1,
5759                          float a2,
5760                          const char *tip)
5761 {
5762   return uiDefIconTextBut(block,
5763                           type | UI_BUT_POIN_SHORT,
5764                           retval,
5765                           icon,
5766                           str,
5767                           x,
5768                           y,
5769                           width,
5770                           height,
5771                           (void *)poin,
5772                           min,
5773                           max,
5774                           a1,
5775                           a2,
5776                           tip);
5777 }
uiDefIconTextButBitS(uiBlock * block,int type,int bit,int retval,int icon,const char * str,int x,int y,short width,short height,short * poin,float min,float max,float a1,float a2,const char * tip)5778 uiBut *uiDefIconTextButBitS(uiBlock *block,
5779                             int type,
5780                             int bit,
5781                             int retval,
5782                             int icon,
5783                             const char *str,
5784                             int x,
5785                             int y,
5786                             short width,
5787                             short height,
5788                             short *poin,
5789                             float min,
5790                             float max,
5791                             float a1,
5792                             float a2,
5793                             const char *tip)
5794 {
5795   return uiDefIconTextButBit(block,
5796                              type | UI_BUT_POIN_SHORT,
5797                              bit,
5798                              retval,
5799                              icon,
5800                              str,
5801                              x,
5802                              y,
5803                              width,
5804                              height,
5805                              (void *)poin,
5806                              min,
5807                              max,
5808                              a1,
5809                              a2,
5810                              tip);
5811 }
uiDefIconTextButC(uiBlock * block,int type,int retval,int icon,const char * str,int x,int y,short width,short height,char * poin,float min,float max,float a1,float a2,const char * tip)5812 uiBut *uiDefIconTextButC(uiBlock *block,
5813                          int type,
5814                          int retval,
5815                          int icon,
5816                          const char *str,
5817                          int x,
5818                          int y,
5819                          short width,
5820                          short height,
5821                          char *poin,
5822                          float min,
5823                          float max,
5824                          float a1,
5825                          float a2,
5826                          const char *tip)
5827 {
5828   return uiDefIconTextBut(block,
5829                           type | UI_BUT_POIN_CHAR,
5830                           retval,
5831                           icon,
5832                           str,
5833                           x,
5834                           y,
5835                           width,
5836                           height,
5837                           (void *)poin,
5838                           min,
5839                           max,
5840                           a1,
5841                           a2,
5842                           tip);
5843 }
uiDefIconTextButBitC(uiBlock * block,int type,int bit,int retval,int icon,const char * str,int x,int y,short width,short height,char * poin,float min,float max,float a1,float a2,const char * tip)5844 uiBut *uiDefIconTextButBitC(uiBlock *block,
5845                             int type,
5846                             int bit,
5847                             int retval,
5848                             int icon,
5849                             const char *str,
5850                             int x,
5851                             int y,
5852                             short width,
5853                             short height,
5854                             char *poin,
5855                             float min,
5856                             float max,
5857                             float a1,
5858                             float a2,
5859                             const char *tip)
5860 {
5861   return uiDefIconTextButBit(block,
5862                              type | UI_BUT_POIN_CHAR,
5863                              bit,
5864                              retval,
5865                              icon,
5866                              str,
5867                              x,
5868                              y,
5869                              width,
5870                              height,
5871                              (void *)poin,
5872                              min,
5873                              max,
5874                              a1,
5875                              a2,
5876                              tip);
5877 }
uiDefIconTextButR(uiBlock * block,int type,int retval,int icon,const char * str,int x,int y,short width,short height,PointerRNA * ptr,const char * propname,int index,float min,float max,float a1,float a2,const char * tip)5878 uiBut *uiDefIconTextButR(uiBlock *block,
5879                          int type,
5880                          int retval,
5881                          int icon,
5882                          const char *str,
5883                          int x,
5884                          int y,
5885                          short width,
5886                          short height,
5887                          PointerRNA *ptr,
5888                          const char *propname,
5889                          int index,
5890                          float min,
5891                          float max,
5892                          float a1,
5893                          float a2,
5894                          const char *tip)
5895 {
5896   uiBut *but = ui_def_but_rna_propname(
5897       block, type, retval, str, x, y, width, height, ptr, propname, index, min, max, a1, a2, tip);
5898   ui_but_update_and_icon_set(but, icon);
5899   but->drawflag |= UI_BUT_ICON_LEFT;
5900   return but;
5901 }
uiDefIconTextButR_prop(uiBlock * block,int type,int retval,int icon,const char * str,int x,int y,short width,short height,PointerRNA * ptr,PropertyRNA * prop,int index,float min,float max,float a1,float a2,const char * tip)5902 uiBut *uiDefIconTextButR_prop(uiBlock *block,
5903                               int type,
5904                               int retval,
5905                               int icon,
5906                               const char *str,
5907                               int x,
5908                               int y,
5909                               short width,
5910                               short height,
5911                               PointerRNA *ptr,
5912                               PropertyRNA *prop,
5913                               int index,
5914                               float min,
5915                               float max,
5916                               float a1,
5917                               float a2,
5918                               const char *tip)
5919 {
5920   uiBut *but = ui_def_but_rna(
5921       block, type, retval, str, x, y, width, height, ptr, prop, index, min, max, a1, a2, tip);
5922   ui_but_update_and_icon_set(but, icon);
5923   but->drawflag |= UI_BUT_ICON_LEFT;
5924   return but;
5925 }
uiDefIconTextButO_ptr(uiBlock * block,int type,wmOperatorType * ot,int opcontext,int icon,const char * str,int x,int y,short width,short height,const char * tip)5926 uiBut *uiDefIconTextButO_ptr(uiBlock *block,
5927                              int type,
5928                              wmOperatorType *ot,
5929                              int opcontext,
5930                              int icon,
5931                              const char *str,
5932                              int x,
5933                              int y,
5934                              short width,
5935                              short height,
5936                              const char *tip)
5937 {
5938   uiBut *but = ui_def_but_operator_ptr(block, type, ot, opcontext, str, x, y, width, height, tip);
5939   ui_but_update_and_icon_set(but, icon);
5940   but->drawflag |= UI_BUT_ICON_LEFT;
5941   return but;
5942 }
uiDefIconTextButO(uiBlock * block,int type,const char * opname,int opcontext,int icon,const char * str,int x,int y,short width,short height,const char * tip)5943 uiBut *uiDefIconTextButO(uiBlock *block,
5944                          int type,
5945                          const char *opname,
5946                          int opcontext,
5947                          int icon,
5948                          const char *str,
5949                          int x,
5950                          int y,
5951                          short width,
5952                          short height,
5953                          const char *tip)
5954 {
5955   wmOperatorType *ot = WM_operatortype_find(opname, 0);
5956   if (str && str[0] == '\0') {
5957     return uiDefIconButO_ptr(block, type, ot, opcontext, icon, x, y, width, height, tip);
5958   }
5959   return uiDefIconTextButO_ptr(block, type, ot, opcontext, icon, str, x, y, width, height, tip);
5960 }
5961 
5962 /* END Button containing both string label and icon */
5963 
5964 /* cruft to make uiBlock and uiBut private */
5965 
UI_blocklist_min_y_get(ListBase * lb)5966 int UI_blocklist_min_y_get(ListBase *lb)
5967 {
5968   int min = 0;
5969 
5970   LISTBASE_FOREACH (uiBlock *, block, lb) {
5971     if (block == lb->first || block->rect.ymin < min) {
5972       min = block->rect.ymin;
5973     }
5974   }
5975 
5976   return min;
5977 }
5978 
UI_block_direction_set(uiBlock * block,char direction)5979 void UI_block_direction_set(uiBlock *block, char direction)
5980 {
5981   block->direction = direction;
5982 }
5983 
5984 /* this call escapes if there's alignment flags */
UI_block_order_flip(uiBlock * block)5985 void UI_block_order_flip(uiBlock *block)
5986 {
5987   float centy, miny = 10000, maxy = -10000;
5988 
5989   if (U.uiflag & USER_MENUFIXEDORDER) {
5990     return;
5991   }
5992   if (block->flag & UI_BLOCK_NO_FLIP) {
5993     return;
5994   }
5995 
5996   LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
5997     if (but->drawflag & UI_BUT_ALIGN) {
5998       return;
5999     }
6000     if (but->rect.ymin < miny) {
6001       miny = but->rect.ymin;
6002     }
6003     if (but->rect.ymax > maxy) {
6004       maxy = but->rect.ymax;
6005     }
6006   }
6007   /* mirror trick */
6008   centy = (miny + maxy) / 2.0f;
6009   LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
6010     but->rect.ymin = centy - (but->rect.ymin - centy);
6011     but->rect.ymax = centy - (but->rect.ymax - centy);
6012     SWAP(float, but->rect.ymin, but->rect.ymax);
6013   }
6014 
6015   block->flag ^= UI_BLOCK_IS_FLIP;
6016 }
6017 
UI_block_flag_enable(uiBlock * block,int flag)6018 void UI_block_flag_enable(uiBlock *block, int flag)
6019 {
6020   block->flag |= flag;
6021 }
6022 
UI_block_flag_disable(uiBlock * block,int flag)6023 void UI_block_flag_disable(uiBlock *block, int flag)
6024 {
6025   block->flag &= ~flag;
6026 }
6027 
UI_but_flag_enable(uiBut * but,int flag)6028 void UI_but_flag_enable(uiBut *but, int flag)
6029 {
6030   but->flag |= flag;
6031 }
6032 
UI_but_flag_disable(uiBut * but,int flag)6033 void UI_but_flag_disable(uiBut *but, int flag)
6034 {
6035   but->flag &= ~flag;
6036 }
6037 
UI_but_flag_is_set(uiBut * but,int flag)6038 bool UI_but_flag_is_set(uiBut *but, int flag)
6039 {
6040   return (but->flag & flag) != 0;
6041 }
6042 
UI_but_drawflag_enable(uiBut * but,int flag)6043 void UI_but_drawflag_enable(uiBut *but, int flag)
6044 {
6045   but->drawflag |= flag;
6046 }
6047 
UI_but_drawflag_disable(uiBut * but,int flag)6048 void UI_but_drawflag_disable(uiBut *but, int flag)
6049 {
6050   but->drawflag &= ~flag;
6051 }
6052 
UI_but_disable(uiBut * but,const char * disabled_hint)6053 void UI_but_disable(uiBut *but, const char *disabled_hint)
6054 {
6055   UI_but_flag_enable(but, UI_BUT_DISABLED);
6056 
6057   /* Only one disabled hint at a time currently. Don't override the previous one here. */
6058   if (but->disabled_info && but->disabled_info[0]) {
6059     return;
6060   }
6061 
6062   but->disabled_info = disabled_hint;
6063 }
6064 
UI_but_type_set_menu_from_pulldown(uiBut * but)6065 void UI_but_type_set_menu_from_pulldown(uiBut *but)
6066 {
6067   BLI_assert(but->type == UI_BTYPE_PULLDOWN);
6068   but->type = UI_BTYPE_MENU;
6069   UI_but_drawflag_disable(but, UI_BUT_TEXT_RIGHT);
6070   UI_but_drawflag_enable(but, UI_BUT_TEXT_LEFT);
6071 }
6072 
UI_but_return_value_get(uiBut * but)6073 int UI_but_return_value_get(uiBut *but)
6074 {
6075   return but->retval;
6076 }
6077 
UI_but_drag_set_id(uiBut * but,ID * id)6078 void UI_but_drag_set_id(uiBut *but, ID *id)
6079 {
6080   but->dragtype = WM_DRAG_ID;
6081   if ((but->dragflag & UI_BUT_DRAGPOIN_FREE)) {
6082     MEM_SAFE_FREE(but->dragpoin);
6083     but->dragflag &= ~UI_BUT_DRAGPOIN_FREE;
6084   }
6085   but->dragpoin = (void *)id;
6086 }
6087 
UI_but_drag_set_rna(uiBut * but,PointerRNA * ptr)6088 void UI_but_drag_set_rna(uiBut *but, PointerRNA *ptr)
6089 {
6090   but->dragtype = WM_DRAG_RNA;
6091   if ((but->dragflag & UI_BUT_DRAGPOIN_FREE)) {
6092     MEM_SAFE_FREE(but->dragpoin);
6093     but->dragflag &= ~UI_BUT_DRAGPOIN_FREE;
6094   }
6095   but->dragpoin = (void *)ptr;
6096 }
6097 
UI_but_drag_set_path(uiBut * but,const char * path,const bool use_free)6098 void UI_but_drag_set_path(uiBut *but, const char *path, const bool use_free)
6099 {
6100   but->dragtype = WM_DRAG_PATH;
6101   if ((but->dragflag & UI_BUT_DRAGPOIN_FREE)) {
6102     MEM_SAFE_FREE(but->dragpoin);
6103     but->dragflag &= ~UI_BUT_DRAGPOIN_FREE;
6104   }
6105   but->dragpoin = (void *)path;
6106   if (use_free) {
6107     but->dragflag |= UI_BUT_DRAGPOIN_FREE;
6108   }
6109 }
6110 
UI_but_drag_set_name(uiBut * but,const char * name)6111 void UI_but_drag_set_name(uiBut *but, const char *name)
6112 {
6113   but->dragtype = WM_DRAG_NAME;
6114   if ((but->dragflag & UI_BUT_DRAGPOIN_FREE)) {
6115     MEM_SAFE_FREE(but->dragpoin);
6116     but->dragflag &= ~UI_BUT_DRAGPOIN_FREE;
6117   }
6118   but->dragpoin = (void *)name;
6119 }
6120 
6121 /* value from button itself */
UI_but_drag_set_value(uiBut * but)6122 void UI_but_drag_set_value(uiBut *but)
6123 {
6124   but->dragtype = WM_DRAG_VALUE;
6125 }
6126 
UI_but_drag_set_image(uiBut * but,const char * path,int icon,struct ImBuf * imb,float scale,const bool use_free)6127 void UI_but_drag_set_image(
6128     uiBut *but, const char *path, int icon, struct ImBuf *imb, float scale, const bool use_free)
6129 {
6130   but->dragtype = WM_DRAG_PATH;
6131   ui_def_but_icon(but, icon, 0); /* no flag UI_HAS_ICON, so icon doesn't draw in button */
6132   if ((but->dragflag & UI_BUT_DRAGPOIN_FREE)) {
6133     MEM_SAFE_FREE(but->dragpoin);
6134     but->dragflag &= ~UI_BUT_DRAGPOIN_FREE;
6135   }
6136   but->dragpoin = (void *)path;
6137   if (use_free) {
6138     but->dragflag |= UI_BUT_DRAGPOIN_FREE;
6139   }
6140   but->imb = imb;
6141   but->imb_scale = scale;
6142 }
6143 
UI_but_operator_ptr_get(uiBut * but)6144 PointerRNA *UI_but_operator_ptr_get(uiBut *but)
6145 {
6146   if (but->optype && !but->opptr) {
6147     but->opptr = MEM_callocN(sizeof(PointerRNA), "uiButOpPtr");
6148     WM_operator_properties_create_ptr(but->opptr, but->optype);
6149   }
6150 
6151   return but->opptr;
6152 }
6153 
UI_but_unit_type_set(uiBut * but,const int unit_type)6154 void UI_but_unit_type_set(uiBut *but, const int unit_type)
6155 {
6156   but->unit_type = (uchar)(RNA_SUBTYPE_UNIT_VALUE(unit_type));
6157 }
6158 
UI_but_unit_type_get(const uiBut * but)6159 int UI_but_unit_type_get(const uiBut *but)
6160 {
6161   const int ownUnit = (int)but->unit_type;
6162 
6163   /* own unit define always takes precedence over RNA provided, allowing for overriding
6164    * default value provided in RNA in a few special cases (i.e. Active Keyframe in Graph Edit)
6165    */
6166   /* XXX: this doesn't allow clearing unit completely, though the same could be said for icons */
6167   if ((ownUnit != 0) || (but->rnaprop == NULL)) {
6168     return ownUnit << 16;
6169   }
6170   return RNA_SUBTYPE_UNIT(RNA_property_subtype(but->rnaprop));
6171 }
6172 
UI_block_func_handle_set(uiBlock * block,uiBlockHandleFunc func,void * arg)6173 void UI_block_func_handle_set(uiBlock *block, uiBlockHandleFunc func, void *arg)
6174 {
6175   block->handle_func = func;
6176   block->handle_func_arg = arg;
6177 }
6178 
UI_block_func_butmenu_set(uiBlock * block,uiMenuHandleFunc func,void * arg)6179 void UI_block_func_butmenu_set(uiBlock *block, uiMenuHandleFunc func, void *arg)
6180 {
6181   block->butm_func = func;
6182   block->butm_func_arg = arg;
6183 }
6184 
UI_block_func_set(uiBlock * block,uiButHandleFunc func,void * arg1,void * arg2)6185 void UI_block_func_set(uiBlock *block, uiButHandleFunc func, void *arg1, void *arg2)
6186 {
6187   block->func = func;
6188   block->func_arg1 = arg1;
6189   block->func_arg2 = arg2;
6190 }
6191 
UI_block_funcN_set(uiBlock * block,uiButHandleNFunc funcN,void * argN,void * arg2)6192 void UI_block_funcN_set(uiBlock *block, uiButHandleNFunc funcN, void *argN, void *arg2)
6193 {
6194   if (block->func_argN) {
6195     MEM_freeN(block->func_argN);
6196   }
6197 
6198   block->funcN = funcN;
6199   block->func_argN = argN;
6200   block->func_arg2 = arg2;
6201 }
6202 
UI_but_func_rename_set(uiBut * but,uiButHandleRenameFunc func,void * arg1)6203 void UI_but_func_rename_set(uiBut *but, uiButHandleRenameFunc func, void *arg1)
6204 {
6205   but->rename_func = func;
6206   but->rename_arg1 = arg1;
6207 }
6208 
UI_but_func_drawextra_set(uiBlock * block,void (* func)(const bContext * C,void * idv,void * arg1,void * arg2,rcti * rect),void * arg1,void * arg2)6209 void UI_but_func_drawextra_set(
6210     uiBlock *block,
6211     void (*func)(const bContext *C, void *idv, void *arg1, void *arg2, rcti *rect),
6212     void *arg1,
6213     void *arg2)
6214 {
6215   block->drawextra = func;
6216   block->drawextra_arg1 = arg1;
6217   block->drawextra_arg2 = arg2;
6218 }
6219 
UI_but_func_set(uiBut * but,uiButHandleFunc func,void * arg1,void * arg2)6220 void UI_but_func_set(uiBut *but, uiButHandleFunc func, void *arg1, void *arg2)
6221 {
6222   but->func = func;
6223   but->func_arg1 = arg1;
6224   but->func_arg2 = arg2;
6225 }
6226 
UI_but_funcN_set(uiBut * but,uiButHandleNFunc funcN,void * argN,void * arg2)6227 void UI_but_funcN_set(uiBut *but, uiButHandleNFunc funcN, void *argN, void *arg2)
6228 {
6229   if (but->func_argN) {
6230     MEM_freeN(but->func_argN);
6231   }
6232 
6233   but->funcN = funcN;
6234   but->func_argN = argN;
6235   but->func_arg2 = arg2;
6236 }
6237 
UI_but_func_complete_set(uiBut * but,uiButCompleteFunc func,void * arg)6238 void UI_but_func_complete_set(uiBut *but, uiButCompleteFunc func, void *arg)
6239 {
6240   but->autocomplete_func = func;
6241   but->autofunc_arg = arg;
6242 }
6243 
UI_but_func_menu_step_set(uiBut * but,uiMenuStepFunc func)6244 void UI_but_func_menu_step_set(uiBut *but, uiMenuStepFunc func)
6245 {
6246   but->menu_step_func = func;
6247 }
6248 
UI_but_func_tooltip_set(uiBut * but,uiButToolTipFunc func,void * argN)6249 void UI_but_func_tooltip_set(uiBut *but, uiButToolTipFunc func, void *argN)
6250 {
6251   but->tip_func = func;
6252   if (but->tip_argN) {
6253     MEM_freeN(but->tip_argN);
6254   }
6255   but->tip_argN = argN;
6256 }
6257 
UI_but_func_pushed_state_set(uiBut * but,uiButPushedStateFunc func,void * arg)6258 void UI_but_func_pushed_state_set(uiBut *but, uiButPushedStateFunc func, void *arg)
6259 {
6260   but->pushed_state_func = func;
6261   but->pushed_state_arg = arg;
6262 }
6263 
uiDefBlockBut(uiBlock * block,uiBlockCreateFunc func,void * arg,const char * str,int x,int y,short width,short height,const char * tip)6264 uiBut *uiDefBlockBut(uiBlock *block,
6265                      uiBlockCreateFunc func,
6266                      void *arg,
6267                      const char *str,
6268                      int x,
6269                      int y,
6270                      short width,
6271                      short height,
6272                      const char *tip)
6273 {
6274   uiBut *but = ui_def_but(
6275       block, UI_BTYPE_BLOCK, 0, str, x, y, width, height, arg, 0.0, 0.0, 0.0, 0.0, tip);
6276   but->block_create_func = func;
6277   ui_but_update(but);
6278   return but;
6279 }
6280 
uiDefBlockButN(uiBlock * block,uiBlockCreateFunc func,void * argN,const char * str,int x,int y,short width,short height,const char * tip)6281 uiBut *uiDefBlockButN(uiBlock *block,
6282                       uiBlockCreateFunc func,
6283                       void *argN,
6284                       const char *str,
6285                       int x,
6286                       int y,
6287                       short width,
6288                       short height,
6289                       const char *tip)
6290 {
6291   uiBut *but = ui_def_but(
6292       block, UI_BTYPE_BLOCK, 0, str, x, y, width, height, NULL, 0.0, 0.0, 0.0, 0.0, tip);
6293   but->block_create_func = func;
6294   if (but->func_argN) {
6295     MEM_freeN(but->func_argN);
6296   }
6297   but->func_argN = argN;
6298   ui_but_update(but);
6299   return but;
6300 }
6301 
uiDefPulldownBut(uiBlock * block,uiBlockCreateFunc func,void * arg,const char * str,int x,int y,short width,short height,const char * tip)6302 uiBut *uiDefPulldownBut(uiBlock *block,
6303                         uiBlockCreateFunc func,
6304                         void *arg,
6305                         const char *str,
6306                         int x,
6307                         int y,
6308                         short width,
6309                         short height,
6310                         const char *tip)
6311 {
6312   uiBut *but = ui_def_but(
6313       block, UI_BTYPE_PULLDOWN, 0, str, x, y, width, height, arg, 0.0, 0.0, 0.0, 0.0, tip);
6314   but->block_create_func = func;
6315   ui_but_update(but);
6316   return but;
6317 }
6318 
uiDefMenuBut(uiBlock * block,uiMenuCreateFunc func,void * arg,const char * str,int x,int y,short width,short height,const char * tip)6319 uiBut *uiDefMenuBut(uiBlock *block,
6320                     uiMenuCreateFunc func,
6321                     void *arg,
6322                     const char *str,
6323                     int x,
6324                     int y,
6325                     short width,
6326                     short height,
6327                     const char *tip)
6328 {
6329   uiBut *but = ui_def_but(
6330       block, UI_BTYPE_PULLDOWN, 0, str, x, y, width, height, arg, 0.0, 0.0, 0.0, 0.0, tip);
6331   but->menu_create_func = func;
6332   ui_but_update(but);
6333   return but;
6334 }
6335 
uiDefIconTextMenuBut(uiBlock * block,uiMenuCreateFunc func,void * arg,int icon,const char * str,int x,int y,short width,short height,const char * tip)6336 uiBut *uiDefIconTextMenuBut(uiBlock *block,
6337                             uiMenuCreateFunc func,
6338                             void *arg,
6339                             int icon,
6340                             const char *str,
6341                             int x,
6342                             int y,
6343                             short width,
6344                             short height,
6345                             const char *tip)
6346 {
6347   uiBut *but = ui_def_but(
6348       block, UI_BTYPE_PULLDOWN, 0, str, x, y, width, height, arg, 0.0, 0.0, 0.0, 0.0, tip);
6349 
6350   ui_def_but_icon(but, icon, UI_HAS_ICON);
6351 
6352   but->drawflag |= UI_BUT_ICON_LEFT;
6353   ui_but_submenu_enable(block, but);
6354 
6355   but->menu_create_func = func;
6356   ui_but_update(but);
6357 
6358   return but;
6359 }
6360 
uiDefIconMenuBut(uiBlock * block,uiMenuCreateFunc func,void * arg,int icon,int x,int y,short width,short height,const char * tip)6361 uiBut *uiDefIconMenuBut(uiBlock *block,
6362                         uiMenuCreateFunc func,
6363                         void *arg,
6364                         int icon,
6365                         int x,
6366                         int y,
6367                         short width,
6368                         short height,
6369                         const char *tip)
6370 {
6371   uiBut *but = ui_def_but(
6372       block, UI_BTYPE_PULLDOWN, 0, "", x, y, width, height, arg, 0.0, 0.0, 0.0, 0.0, tip);
6373 
6374   ui_def_but_icon(but, icon, UI_HAS_ICON);
6375   but->drawflag &= ~UI_BUT_ICON_LEFT;
6376 
6377   but->menu_create_func = func;
6378   ui_but_update(but);
6379 
6380   return but;
6381 }
6382 
6383 /* Block button containing both string label and icon */
uiDefIconTextBlockBut(uiBlock * block,uiBlockCreateFunc func,void * arg,int icon,const char * str,int x,int y,short width,short height,const char * tip)6384 uiBut *uiDefIconTextBlockBut(uiBlock *block,
6385                              uiBlockCreateFunc func,
6386                              void *arg,
6387                              int icon,
6388                              const char *str,
6389                              int x,
6390                              int y,
6391                              short width,
6392                              short height,
6393                              const char *tip)
6394 {
6395   uiBut *but = ui_def_but(
6396       block, UI_BTYPE_BLOCK, 0, str, x, y, width, height, arg, 0.0, 0.0, 0.0, 0.0, tip);
6397 
6398   /* XXX temp, old menu calls pass on icon arrow, which is now UI_BUT_ICON_SUBMENU flag */
6399   if (icon != ICON_RIGHTARROW_THIN) {
6400     ui_def_but_icon(but, icon, 0);
6401     but->drawflag |= UI_BUT_ICON_LEFT;
6402   }
6403   but->flag |= UI_HAS_ICON;
6404   ui_but_submenu_enable(block, but);
6405 
6406   but->block_create_func = func;
6407   ui_but_update(but);
6408 
6409   return but;
6410 }
6411 
6412 /* Block button containing icon */
uiDefIconBlockBut(uiBlock * block,uiBlockCreateFunc func,void * arg,int retval,int icon,int x,int y,short width,short height,const char * tip)6413 uiBut *uiDefIconBlockBut(uiBlock *block,
6414                          uiBlockCreateFunc func,
6415                          void *arg,
6416                          int retval,
6417                          int icon,
6418                          int x,
6419                          int y,
6420                          short width,
6421                          short height,
6422                          const char *tip)
6423 {
6424   uiBut *but = ui_def_but(
6425       block, UI_BTYPE_BLOCK, retval, "", x, y, width, height, arg, 0.0, 0.0, 0.0, 0.0, tip);
6426 
6427   ui_def_but_icon(but, icon, UI_HAS_ICON);
6428 
6429   but->drawflag |= UI_BUT_ICON_LEFT;
6430 
6431   but->block_create_func = func;
6432   ui_but_update(but);
6433 
6434   return but;
6435 }
6436 
uiDefKeyevtButS(uiBlock * block,int retval,const char * str,int x,int y,short width,short height,short * spoin,const char * tip)6437 uiBut *uiDefKeyevtButS(uiBlock *block,
6438                        int retval,
6439                        const char *str,
6440                        int x,
6441                        int y,
6442                        short width,
6443                        short height,
6444                        short *spoin,
6445                        const char *tip)
6446 {
6447   uiBut *but = ui_def_but(block,
6448                           UI_BTYPE_KEY_EVENT | UI_BUT_POIN_SHORT,
6449                           retval,
6450                           str,
6451                           x,
6452                           y,
6453                           width,
6454                           height,
6455                           spoin,
6456                           0.0,
6457                           0.0,
6458                           0.0,
6459                           0.0,
6460                           tip);
6461   ui_but_update(but);
6462   return but;
6463 }
6464 
6465 /* short pointers hardcoded */
6466 /* modkeypoin will be set to KM_SHIFT, KM_ALT, KM_CTRL, KM_OSKEY bits */
uiDefHotKeyevtButS(uiBlock * block,int retval,const char * str,int x,int y,short width,short height,short * keypoin,const short * modkeypoin,const char * tip)6467 uiBut *uiDefHotKeyevtButS(uiBlock *block,
6468                           int retval,
6469                           const char *str,
6470                           int x,
6471                           int y,
6472                           short width,
6473                           short height,
6474                           short *keypoin,
6475                           const short *modkeypoin,
6476                           const char *tip)
6477 {
6478   uiBut *but = ui_def_but(block,
6479                           UI_BTYPE_HOTKEY_EVENT | UI_BUT_POIN_SHORT,
6480                           retval,
6481                           str,
6482                           x,
6483                           y,
6484                           width,
6485                           height,
6486                           keypoin,
6487                           0.0,
6488                           0.0,
6489                           0.0,
6490                           0.0,
6491                           tip);
6492   but->modifier_key = *modkeypoin;
6493   ui_but_update(but);
6494   return but;
6495 }
6496 
6497 /* arg is pointer to string/name, use UI_but_func_search_set() below to make this work */
6498 /* here a1 and a2, if set, control thumbnail preview rows/cols */
uiDefSearchBut(uiBlock * block,void * arg,int retval,int icon,int maxlen,int x,int y,short width,short height,float a1,float a2,const char * tip)6499 uiBut *uiDefSearchBut(uiBlock *block,
6500                       void *arg,
6501                       int retval,
6502                       int icon,
6503                       int maxlen,
6504                       int x,
6505                       int y,
6506                       short width,
6507                       short height,
6508                       float a1,
6509                       float a2,
6510                       const char *tip)
6511 {
6512   uiBut *but = ui_def_but(
6513       block, UI_BTYPE_SEARCH_MENU, retval, "", x, y, width, height, arg, 0.0, maxlen, a1, a2, tip);
6514 
6515   ui_def_but_icon(but, icon, UI_HAS_ICON);
6516 
6517   but->drawflag |= UI_BUT_ICON_LEFT | UI_BUT_TEXT_LEFT;
6518 
6519   ui_but_update(but);
6520 
6521   return but;
6522 }
6523 
6524 /**
6525  * \note The item-pointer (referred to below) is a per search item user pointer
6526  * passed to #UI_search_item_add (stored in  #uiSearchItems.pointers).
6527  *
6528  * \param search_create_fn: Function to create the menu.
6529  * \param search_update_fn: Function to refresh search content after the search text has changed.
6530  * \param arg: user value.
6531  * \param search_arg_free_fn: When non-null, use this function to free \a arg.
6532  * \param search_exec_fn: Function that executes the action, gets \a arg as the first argument.
6533  * The second argument as the active item-pointer
6534  * \param active: When non-null, this item-pointer item will be visible and selected,
6535  * otherwise the first item will be selected.
6536  */
UI_but_func_search_set(uiBut * but,uiButSearchCreateFn search_create_fn,uiButSearchUpdateFn search_update_fn,void * arg,uiButSearchArgFreeFn search_arg_free_fn,uiButHandleFunc search_exec_fn,void * active)6537 void UI_but_func_search_set(uiBut *but,
6538                             uiButSearchCreateFn search_create_fn,
6539                             uiButSearchUpdateFn search_update_fn,
6540                             void *arg,
6541                             uiButSearchArgFreeFn search_arg_free_fn,
6542                             uiButHandleFunc search_exec_fn,
6543                             void *active)
6544 {
6545   uiButSearch *search_but = (uiButSearch *)but;
6546 
6547   BLI_assert(but->type == UI_BTYPE_SEARCH_MENU);
6548 
6549   /* needed since callers don't have access to internal functions
6550    * (as an alternative we could expose it) */
6551   if (search_create_fn == NULL) {
6552     search_create_fn = ui_searchbox_create_generic;
6553   }
6554 
6555   if (search_but->arg_free_fn != NULL) {
6556     search_but->arg_free_fn(search_but->arg);
6557     search_but->arg = NULL;
6558   }
6559 
6560   search_but->popup_create_fn = search_create_fn;
6561   search_but->items_update_fn = search_update_fn;
6562   search_but->item_active = active;
6563 
6564   search_but->arg = arg;
6565   search_but->arg_free_fn = search_arg_free_fn;
6566 
6567   if (search_exec_fn) {
6568 #ifdef DEBUG
6569     if (search_but->but.func) {
6570       /* watch this, can be cause of much confusion, see: T47691 */
6571       printf("%s: warning, overwriting button callback with search function callback!\n",
6572              __func__);
6573     }
6574 #endif
6575     /* Handling will pass the active item as arg2 later, so keep it NULL here. */
6576     UI_but_func_set(but, search_exec_fn, search_but->arg, NULL);
6577   }
6578 
6579   /* search buttons show red-alert if item doesn't exist, not for menus */
6580   if (0 == (but->block->flag & UI_BLOCK_LOOP)) {
6581     /* skip empty buttons, not all buttons need input, we only show invalid */
6582     if (but->drawstr[0]) {
6583       ui_but_search_refresh(search_but);
6584     }
6585   }
6586 }
6587 
UI_but_func_search_set_context_menu(uiBut * but,uiButSearchContextMenuFn context_menu_fn)6588 void UI_but_func_search_set_context_menu(uiBut *but, uiButSearchContextMenuFn context_menu_fn)
6589 {
6590   uiButSearch *but_search = (uiButSearch *)but;
6591   BLI_assert(but->type == UI_BTYPE_SEARCH_MENU);
6592 
6593   but_search->item_context_menu_fn = context_menu_fn;
6594 }
6595 
6596 /**
6597  * \param search_sep_string: when not NULL, this string is used as a separator,
6598  * showing the icon and highlighted text after the last instance of this string.
6599  */
UI_but_func_search_set_sep_string(uiBut * but,const char * search_sep_string)6600 void UI_but_func_search_set_sep_string(uiBut *but, const char *search_sep_string)
6601 {
6602   uiButSearch *but_search = (uiButSearch *)but;
6603   BLI_assert(but->type == UI_BTYPE_SEARCH_MENU);
6604 
6605   but_search->item_sep_string = search_sep_string;
6606 }
6607 
UI_but_func_search_set_tooltip(uiBut * but,uiButSearchTooltipFn tooltip_fn)6608 void UI_but_func_search_set_tooltip(uiBut *but, uiButSearchTooltipFn tooltip_fn)
6609 {
6610   uiButSearch *but_search = (uiButSearch *)but;
6611   BLI_assert(but->type == UI_BTYPE_SEARCH_MENU);
6612 
6613   but_search->item_tooltip_fn = tooltip_fn;
6614 }
6615 
6616 /* Callbacks for operator search button. */
operator_enum_search_update_fn(const struct bContext * C,void * but,const char * str,uiSearchItems * items)6617 static void operator_enum_search_update_fn(const struct bContext *C,
6618                                            void *but,
6619                                            const char *str,
6620                                            uiSearchItems *items)
6621 {
6622   wmOperatorType *ot = ((uiBut *)but)->optype;
6623   PropertyRNA *prop = ot->prop;
6624 
6625   if (prop == NULL) {
6626     printf("%s: %s has no enum property set\n", __func__, ot->idname);
6627   }
6628   else if (RNA_property_type(prop) != PROP_ENUM) {
6629     printf("%s: %s \"%s\" is not an enum property\n",
6630            __func__,
6631            ot->idname,
6632            RNA_property_identifier(prop));
6633   }
6634   else {
6635     PointerRNA *ptr = UI_but_operator_ptr_get(but); /* Will create it if needed! */
6636 
6637     bool do_free;
6638     const EnumPropertyItem *all_items;
6639     RNA_property_enum_items_gettexted((bContext *)C, ptr, prop, &all_items, NULL, &do_free);
6640 
6641     StringSearch *search = BLI_string_search_new();
6642     for (const EnumPropertyItem *item = all_items; item->identifier; item++) {
6643       BLI_string_search_add(search, item->name, (void *)item);
6644     }
6645 
6646     const EnumPropertyItem **filtered_items;
6647     int filtered_amount = BLI_string_search_query(search, str, (void ***)&filtered_items);
6648 
6649     for (int i = 0; i < filtered_amount; i++) {
6650       const EnumPropertyItem *item = filtered_items[i];
6651       /* note: need to give the index rather than the
6652        * identifier because the enum can be freed */
6653       if (!UI_search_item_add(
6654               items, item->name, POINTER_FROM_INT(item->value), item->icon, 0, 0)) {
6655         break;
6656       }
6657     }
6658 
6659     MEM_freeN((void *)filtered_items);
6660     BLI_string_search_free(search);
6661 
6662     if (do_free) {
6663       MEM_freeN((void *)all_items);
6664     }
6665   }
6666 }
6667 
operator_enum_search_exec_fn(struct bContext * UNUSED (C),void * but,void * arg2)6668 static void operator_enum_search_exec_fn(struct bContext *UNUSED(C), void *but, void *arg2)
6669 {
6670   wmOperatorType *ot = ((uiBut *)but)->optype;
6671   PointerRNA *opptr = UI_but_operator_ptr_get(but); /* Will create it if needed! */
6672 
6673   if (ot) {
6674     if (ot->prop) {
6675       RNA_property_enum_set(opptr, ot->prop, POINTER_AS_INT(arg2));
6676       /* We do not call op from here, will be called by button code.
6677        * ui_apply_but_funcs_after() (in interface_handlers.c)
6678        * called this func before checking operators,
6679        * because one of its parameters is the button itself! */
6680     }
6681     else {
6682       printf("%s: op->prop for '%s' is NULL\n", __func__, ot->idname);
6683     }
6684   }
6685 }
6686 
6687 /**
6688  * Same parameters as for uiDefSearchBut, with additional operator type and properties,
6689  * used by callback to call again the right op with the right options (properties values).
6690  */
uiDefSearchButO_ptr(uiBlock * block,wmOperatorType * ot,IDProperty * properties,void * arg,int retval,int icon,int maxlen,int x,int y,short width,short height,float a1,float a2,const char * tip)6691 uiBut *uiDefSearchButO_ptr(uiBlock *block,
6692                            wmOperatorType *ot,
6693                            IDProperty *properties,
6694                            void *arg,
6695                            int retval,
6696                            int icon,
6697                            int maxlen,
6698                            int x,
6699                            int y,
6700                            short width,
6701                            short height,
6702                            float a1,
6703                            float a2,
6704                            const char *tip)
6705 {
6706   uiBut *but = uiDefSearchBut(block, arg, retval, icon, maxlen, x, y, width, height, a1, a2, tip);
6707   UI_but_func_search_set(but,
6708                          ui_searchbox_create_generic,
6709                          operator_enum_search_update_fn,
6710                          but,
6711                          NULL,
6712                          operator_enum_search_exec_fn,
6713                          NULL);
6714 
6715   but->optype = ot;
6716   but->opcontext = WM_OP_EXEC_DEFAULT;
6717 
6718   if (properties) {
6719     PointerRNA *ptr = UI_but_operator_ptr_get(but);
6720     /* Copy idproperties. */
6721     ptr->data = IDP_CopyProperty(properties);
6722   }
6723 
6724   return but;
6725 }
6726 
UI_but_node_link_set(uiBut * but,bNodeSocket * socket,const float draw_color[4])6727 void UI_but_node_link_set(uiBut *but, bNodeSocket *socket, const float draw_color[4])
6728 {
6729   but->flag |= UI_BUT_NODE_LINK;
6730   but->custom_data = socket;
6731   rgba_float_to_uchar(but->col, draw_color);
6732 }
6733 
UI_but_number_step_size_set(uiBut * but,float step_size)6734 void UI_but_number_step_size_set(uiBut *but, float step_size)
6735 {
6736   uiButNumber *but_number = (uiButNumber *)but;
6737   BLI_assert(but->type == UI_BTYPE_NUM);
6738 
6739   but_number->step_size = step_size;
6740   BLI_assert(step_size > 0);
6741 }
6742 
UI_but_number_precision_set(uiBut * but,float precision)6743 void UI_but_number_precision_set(uiBut *but, float precision)
6744 {
6745   uiButNumber *but_number = (uiButNumber *)but;
6746   BLI_assert(but->type == UI_BTYPE_NUM);
6747 
6748   but_number->precision = precision;
6749   /* -1 is a valid value, UI code figures out an appropriate precision then. */
6750   BLI_assert(precision > -2);
6751 }
6752 
6753 /**
6754  * push a new event onto event queue to activate the given button
6755  * (usually a text-field) upon entering a popup
6756  */
UI_but_focus_on_enter_event(wmWindow * win,uiBut * but)6757 void UI_but_focus_on_enter_event(wmWindow *win, uiBut *but)
6758 {
6759   wmEvent event;
6760   wm_event_init_from_window(win, &event);
6761 
6762   event.type = EVT_BUT_OPEN;
6763   event.val = KM_PRESS;
6764   event.is_repeat = false;
6765   event.customdata = but;
6766   event.customdatafree = false;
6767 
6768   wm_event_add(win, &event);
6769 }
6770 
UI_but_func_hold_set(uiBut * but,uiButHandleHoldFunc func,void * argN)6771 void UI_but_func_hold_set(uiBut *but, uiButHandleHoldFunc func, void *argN)
6772 {
6773   but->hold_func = func;
6774   but->hold_argN = argN;
6775 }
6776 
UI_but_string_info_get(bContext * C,uiBut * but,...)6777 void UI_but_string_info_get(bContext *C, uiBut *but, ...)
6778 {
6779   va_list args;
6780   uiStringInfo *si;
6781 
6782   const EnumPropertyItem *items = NULL, *item = NULL;
6783   int totitems;
6784   bool free_items = false;
6785 
6786   va_start(args, but);
6787   while ((si = (uiStringInfo *)va_arg(args, void *))) {
6788     int type = si->type;
6789     char *tmp = NULL;
6790 
6791     if (type == BUT_GET_LABEL) {
6792       if (but->str && but->str[0]) {
6793         const char *str_sep;
6794         size_t str_len;
6795 
6796         if ((but->flag & UI_BUT_HAS_SEP_CHAR) && (str_sep = strrchr(but->str, UI_SEP_CHAR))) {
6797           str_len = (str_sep - but->str);
6798         }
6799         else {
6800           str_len = strlen(but->str);
6801         }
6802 
6803         tmp = BLI_strdupn(but->str, str_len);
6804       }
6805       else {
6806         type = BUT_GET_RNA_LABEL; /* Fail-safe solution... */
6807       }
6808     }
6809     else if (type == BUT_GET_TIP) {
6810       if (but->tip_func) {
6811         tmp = but->tip_func(C, but->tip_argN, but->tip);
6812       }
6813       else if (but->tip && but->tip[0]) {
6814         tmp = BLI_strdup(but->tip);
6815       }
6816       else {
6817         type = BUT_GET_RNA_TIP; /* Fail-safe solution... */
6818       }
6819     }
6820 
6821     if (type == BUT_GET_RNAPROP_IDENTIFIER) {
6822       if (but->rnaprop) {
6823         tmp = BLI_strdup(RNA_property_identifier(but->rnaprop));
6824       }
6825     }
6826     else if (type == BUT_GET_RNASTRUCT_IDENTIFIER) {
6827       if (but->rnaprop && but->rnapoin.data) {
6828         tmp = BLI_strdup(RNA_struct_identifier(but->rnapoin.type));
6829       }
6830       else if (but->optype) {
6831         tmp = BLI_strdup(but->optype->idname);
6832       }
6833       else if (ELEM(but->type, UI_BTYPE_MENU, UI_BTYPE_PULLDOWN)) {
6834         MenuType *mt = UI_but_menutype_get(but);
6835         if (mt) {
6836           tmp = BLI_strdup(mt->idname);
6837         }
6838       }
6839       else if (but->type == UI_BTYPE_POPOVER) {
6840         PanelType *pt = UI_but_paneltype_get(but);
6841         if (pt) {
6842           tmp = BLI_strdup(pt->idname);
6843         }
6844       }
6845     }
6846     else if (ELEM(type, BUT_GET_RNA_LABEL, BUT_GET_RNA_TIP)) {
6847       if (but->rnaprop) {
6848         if (type == BUT_GET_RNA_LABEL) {
6849           tmp = BLI_strdup(RNA_property_ui_name(but->rnaprop));
6850         }
6851         else {
6852           const char *t = RNA_property_ui_description(but->rnaprop);
6853           if (t && t[0]) {
6854             tmp = BLI_strdup(t);
6855           }
6856         }
6857       }
6858       else if (but->optype) {
6859         if (type == BUT_GET_RNA_LABEL) {
6860           tmp = BLI_strdup(WM_operatortype_name(but->optype, but->opptr));
6861         }
6862         else {
6863           tmp = WM_operatortype_description(C, but->optype, but->opptr);
6864         }
6865       }
6866       else if (ELEM(but->type, UI_BTYPE_MENU, UI_BTYPE_PULLDOWN, UI_BTYPE_POPOVER)) {
6867         {
6868           MenuType *mt = UI_but_menutype_get(but);
6869           if (mt) {
6870             if (type == BUT_GET_RNA_LABEL) {
6871               tmp = BLI_strdup(mt->label);
6872             }
6873             else {
6874               /* Not all menus are from Python. */
6875               if (mt->rna_ext.srna) {
6876                 const char *t = RNA_struct_ui_description(mt->rna_ext.srna);
6877                 if (t && t[0]) {
6878                   tmp = BLI_strdup(t);
6879                 }
6880               }
6881             }
6882           }
6883         }
6884 
6885         if (tmp == NULL) {
6886           wmOperatorType *ot = UI_but_operatortype_get_from_enum_menu(but, NULL);
6887           if (ot) {
6888             if (type == BUT_GET_RNA_LABEL) {
6889               tmp = BLI_strdup(WM_operatortype_name(ot, NULL));
6890             }
6891             else {
6892               tmp = WM_operatortype_description(C, ot, NULL);
6893             }
6894           }
6895         }
6896 
6897         if (tmp == NULL) {
6898           PanelType *pt = UI_but_paneltype_get(but);
6899           if (pt) {
6900             if (type == BUT_GET_RNA_LABEL) {
6901               tmp = BLI_strdup(pt->label);
6902             }
6903             else {
6904               /* Not all panels are from Python. */
6905               if (pt->rna_ext.srna) {
6906                 /* Panels don't yet have descriptions, this may be added. */
6907               }
6908             }
6909           }
6910         }
6911       }
6912     }
6913     else if (type == BUT_GET_RNA_LABEL_CONTEXT) {
6914       const char *_tmp = BLT_I18NCONTEXT_DEFAULT;
6915       if (but->rnaprop) {
6916         _tmp = RNA_property_translation_context(but->rnaprop);
6917       }
6918       else if (but->optype) {
6919         _tmp = RNA_struct_translation_context(but->optype->srna);
6920       }
6921       else if (ELEM(but->type, UI_BTYPE_MENU, UI_BTYPE_PULLDOWN)) {
6922         MenuType *mt = UI_but_menutype_get(but);
6923         if (mt) {
6924           _tmp = RNA_struct_translation_context(mt->rna_ext.srna);
6925         }
6926       }
6927       if (BLT_is_default_context(_tmp)) {
6928         _tmp = BLT_I18NCONTEXT_DEFAULT_BPYRNA;
6929       }
6930       tmp = BLI_strdup(_tmp);
6931     }
6932     else if (ELEM(type, BUT_GET_RNAENUM_IDENTIFIER, BUT_GET_RNAENUM_LABEL, BUT_GET_RNAENUM_TIP)) {
6933       PointerRNA *ptr = NULL;
6934       PropertyRNA *prop = NULL;
6935       int value = 0;
6936 
6937       /* get the enum property... */
6938       if (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_ENUM) {
6939         /* enum property */
6940         ptr = &but->rnapoin;
6941         prop = but->rnaprop;
6942         value = (ELEM(but->type, UI_BTYPE_ROW, UI_BTYPE_TAB)) ? (int)but->hardmax :
6943                                                                 (int)ui_but_value_get(but);
6944       }
6945       else if (but->optype) {
6946         PointerRNA *opptr = UI_but_operator_ptr_get(but);
6947         wmOperatorType *ot = but->optype;
6948 
6949         /* so the context is passed to itemf functions */
6950         WM_operator_properties_sanitize(opptr, false);
6951 
6952         /* if the default property of the operator is enum and it is set,
6953          * fetch the tooltip of the selected value so that "Snap" and "Mirror"
6954          * operator menus in the Anim Editors will show tooltips for the different
6955          * operations instead of the meaningless generic operator tooltip
6956          */
6957         if (ot->prop && RNA_property_type(ot->prop) == PROP_ENUM) {
6958           if (RNA_struct_contains_property(opptr, ot->prop)) {
6959             ptr = opptr;
6960             prop = ot->prop;
6961             value = RNA_property_enum_get(opptr, ot->prop);
6962           }
6963         }
6964       }
6965 
6966       /* get strings from matching enum item */
6967       if (ptr && prop) {
6968         if (!item) {
6969           int i;
6970 
6971           RNA_property_enum_items_gettexted(C, ptr, prop, &items, &totitems, &free_items);
6972           for (i = 0, item = items; i < totitems; i++, item++) {
6973             if (item->identifier[0] && item->value == value) {
6974               break;
6975             }
6976           }
6977         }
6978         if (item && item->identifier) {
6979           if (type == BUT_GET_RNAENUM_IDENTIFIER) {
6980             tmp = BLI_strdup(item->identifier);
6981           }
6982           else if (type == BUT_GET_RNAENUM_LABEL) {
6983             tmp = BLI_strdup(item->name);
6984           }
6985           else if (item->description && item->description[0]) {
6986             tmp = BLI_strdup(item->description);
6987           }
6988         }
6989       }
6990     }
6991     else if (type == BUT_GET_OP_KEYMAP) {
6992       if (!ui_block_is_menu(but->block)) {
6993         char buf[128];
6994         if (ui_but_event_operator_string(C, but, buf, sizeof(buf))) {
6995           tmp = BLI_strdup(buf);
6996         }
6997       }
6998     }
6999     else if (type == BUT_GET_PROP_KEYMAP) {
7000       /* for properties that are bound to one of the context cycle, etc. keys... */
7001       char buf[128];
7002       if (ui_but_event_property_operator_string(C, but, buf, sizeof(buf))) {
7003         tmp = BLI_strdup(buf);
7004       }
7005     }
7006 
7007     si->strinfo = tmp;
7008   }
7009   va_end(args);
7010 
7011   if (free_items && items) {
7012     MEM_freeN((void *)items);
7013   }
7014 }
7015 
7016 /* Program Init/Exit */
7017 
UI_init(void)7018 void UI_init(void)
7019 {
7020   ui_resources_init();
7021 }
7022 
7023 /* after reading userdef file */
UI_init_userdef(void)7024 void UI_init_userdef(void)
7025 {
7026   /* Initialize UI variables from values set in the preferences. */
7027   uiStyleInit();
7028 }
7029 
UI_reinit_font(void)7030 void UI_reinit_font(void)
7031 {
7032   uiStyleInit();
7033 }
7034 
UI_exit(void)7035 void UI_exit(void)
7036 {
7037   ui_resources_free();
7038   ui_but_clipboard_free();
7039 }
7040 
UI_interface_tag_script_reload(void)7041 void UI_interface_tag_script_reload(void)
7042 {
7043   ui_interface_tag_script_reload_queries();
7044 }
7045