Fix for RMB Menu title including shortcut
[blender.git] / source / blender / editors / interface / interface.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation 2002-2008, full recode.
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/interface/interface.c
27  *  \ingroup edinterface
28  */
29
30
31 #include <float.h>
32 #include <limits.h>
33 #include <math.h>
34 #include <string.h>
35 #include <ctype.h>
36 #include <stddef.h>  /* offsetof() */
37
38 #include "MEM_guardedalloc.h"
39
40 #include "DNA_object_types.h"
41 #include "DNA_scene_types.h"
42 #include "DNA_screen_types.h"
43 #include "DNA_userdef_types.h"
44
45 #include "BLI_math.h"
46 #include "BLI_listbase.h"
47 #include "BLI_string.h"
48 #include "BLI_string_utf8.h"
49 #include "BLI_rect.h"
50
51 #include "BLI_utildefines.h"
52
53 #include "BKE_context.h"
54 #include "BKE_unit.h"
55 #include "BKE_scene.h"
56 #include "BKE_screen.h"
57 #include "BKE_idprop.h"
58
59 #include "BIF_gl.h"
60
61 #include "BLF_api.h"
62 #include "BLF_translation.h"
63
64 #include "UI_interface.h"
65
66 #include "IMB_imbuf.h"
67
68 #include "WM_api.h"
69 #include "WM_types.h"
70 #include "wm_subwindow.h"
71
72 #include "RNA_access.h"
73
74 #include "BPY_extern.h"
75
76 #include "IMB_colormanagement.h"
77
78 #include "interface_intern.h"
79
80 /* avoid unneeded calls to ui_but_value_get */
81 #define UI_BUT_VALUE_UNSET DBL_MAX
82 #define UI_GET_BUT_VALUE_INIT(_but, _value) if (_value == DBL_MAX) {  (_value) = ui_but_value_get(_but); } (void)0
83
84 #define B_NOP -1
85
86 /**
87  * a full doc with API notes can be found in 'blender/doc/guides/interface_API.txt'
88  * 
89  * `uiBlahBlah()`       external function.
90  * `ui_blah_blah()`     internal function.
91  */
92
93 static void ui_but_free(const bContext *C, uiBut *but);
94
95 bool ui_block_is_menu(const uiBlock *block)
96 {
97         return (((block->flag & UI_BLOCK_LOOP) != 0) &&
98                 /* non-menu popups use keep-open, so check this is off */
99                 ((block->flag & UI_BLOCK_KEEP_OPEN) == 0));
100 }
101
102 bool ui_block_is_pie_menu(const uiBlock *block)
103 {
104         return ((block->flag & UI_BLOCK_RADIAL) != 0);
105 }
106
107 static bool ui_but_is_unit_radians_ex(UnitSettings *unit, const int unit_type)
108 {
109         return (unit->system_rotation == USER_UNIT_ROT_RADIANS && unit_type == PROP_UNIT_ROTATION);
110 }
111
112 static bool ui_but_is_unit_radians(const uiBut *but)
113 {
114         UnitSettings *unit = but->block->unit;
115         const int unit_type = UI_but_unit_type_get(but);
116
117         return ui_but_is_unit_radians_ex(unit, unit_type);
118 }
119
120 /* ************* window matrix ************** */
121
122 void ui_block_to_window_fl(const ARegion *ar, uiBlock *block, float *x, float *y)
123 {
124         float gx, gy;
125         int sx, sy, getsizex, getsizey;
126
127         getsizex = BLI_rcti_size_x(&ar->winrct) + 1;
128         getsizey = BLI_rcti_size_y(&ar->winrct) + 1;
129         sx = ar->winrct.xmin;
130         sy = ar->winrct.ymin;
131
132         gx = *x;
133         gy = *y;
134
135         if (block->panel) {
136                 gx += block->panel->ofsx;
137                 gy += block->panel->ofsy;
138         }
139
140         *x = ((float)sx) + ((float)getsizex) * (0.5f + 0.5f * (gx * block->winmat[0][0] + gy * block->winmat[1][0] + block->winmat[3][0]));
141         *y = ((float)sy) + ((float)getsizey) * (0.5f + 0.5f * (gx * block->winmat[0][1] + gy * block->winmat[1][1] + block->winmat[3][1]));
142 }
143
144 void ui_block_to_window(const ARegion *ar, uiBlock *block, int *x, int *y)
145 {
146         float fx, fy;
147
148         fx = *x;
149         fy = *y;
150
151         ui_block_to_window_fl(ar, block, &fx, &fy);
152
153         *x = (int)(fx + 0.5f);
154         *y = (int)(fy + 0.5f);
155 }
156
157 void ui_block_to_window_rctf(const ARegion *ar, uiBlock *block, rctf *rct_dst, const rctf *rct_src)
158 {
159         *rct_dst = *rct_src;
160         ui_block_to_window_fl(ar, block, &rct_dst->xmin, &rct_dst->ymin);
161         ui_block_to_window_fl(ar, block, &rct_dst->xmax, &rct_dst->ymax);
162 }
163
164 void ui_window_to_block_fl(const ARegion *ar, uiBlock *block, float *x, float *y)   /* for mouse cursor */
165 {
166         float a, b, c, d, e, f, px, py;
167         int sx, sy, getsizex, getsizey;
168
169         getsizex = BLI_rcti_size_x(&ar->winrct) + 1;
170         getsizey = BLI_rcti_size_y(&ar->winrct) + 1;
171         sx = ar->winrct.xmin;
172         sy = ar->winrct.ymin;
173
174         a = 0.5f * ((float)getsizex) * block->winmat[0][0];
175         b = 0.5f * ((float)getsizex) * block->winmat[1][0];
176         c = 0.5f * ((float)getsizex) * (1.0f + block->winmat[3][0]);
177
178         d = 0.5f * ((float)getsizey) * block->winmat[0][1];
179         e = 0.5f * ((float)getsizey) * block->winmat[1][1];
180         f = 0.5f * ((float)getsizey) * (1.0f + block->winmat[3][1]);
181
182         px = *x - sx;
183         py = *y - sy;
184
185         *y =  (a * (py - f) + d * (c - px)) / (a * e - d * b);
186         *x = (px - b * (*y) - c) / a;
187
188         if (block->panel) {
189                 *x -= block->panel->ofsx;
190                 *y -= block->panel->ofsy;
191         }
192 }
193
194 void ui_window_to_block(const ARegion *ar, uiBlock *block, int *x, int *y)
195 {
196         float fx, fy;
197
198         fx = *x;
199         fy = *y;
200
201         ui_window_to_block_fl(ar, block, &fx, &fy);
202
203         *x = (int)(fx + 0.5f);
204         *y = (int)(fy + 0.5f);
205 }
206
207 void ui_window_to_region(const ARegion *ar, int *x, int *y)
208 {
209         *x -= ar->winrct.xmin;
210         *y -= ar->winrct.ymin;
211 }
212
213 void ui_region_to_window(const ARegion *ar, int *x, int *y)
214 {
215         *x += ar->winrct.xmin;
216         *y += ar->winrct.ymin;
217 }
218 /* ******************* block calc ************************* */
219
220 void ui_block_translate(uiBlock *block, int x, int y)
221 {
222         uiBut *but;
223
224         for (but = block->buttons.first; but; but = but->next) {
225                 BLI_rctf_translate(&but->rect, x, y);
226         }
227
228         BLI_rctf_translate(&block->rect, x, y);
229 }
230
231 static void ui_block_bounds_calc_text(uiBlock *block, float offset)
232 {
233         uiStyle *style = UI_style_get();
234         uiBut *bt, *init_col_bt, *col_bt;
235         int i = 0, j, x1addval = offset;
236
237         UI_fontstyle_set(&style->widget);
238
239         for (init_col_bt = bt = block->buttons.first; bt; bt = bt->next) {
240                 if (!ELEM(bt->type, UI_BTYPE_SEPR, UI_BTYPE_SEPR_LINE)) {
241                         j = BLF_width(style->widget.uifont_id, bt->drawstr, sizeof(bt->drawstr));
242
243                         if (j > i)
244                                 i = j;
245                 }
246
247                 if (bt->next && bt->rect.xmin < bt->next->rect.xmin) {
248                         /* End of this column, and it’s not the last one. */
249                         for (col_bt = init_col_bt; col_bt->prev != bt; col_bt = col_bt->next) {
250                                 col_bt->rect.xmin = x1addval;
251                                 col_bt->rect.xmax = x1addval + i + block->bounds;
252
253                                 ui_but_update(col_bt);  /* clips text again */
254                         }
255
256                         /* And we prepare next column. */
257                         x1addval += i + block->bounds;
258                         i = 0;
259                         init_col_bt = col_bt;
260                 }
261         }
262
263         /* Last column. */
264         for (col_bt = init_col_bt; col_bt; col_bt = col_bt->next) {
265                 col_bt->rect.xmin = x1addval;
266                 col_bt->rect.xmax = max_ff(x1addval + i + block->bounds, offset + block->minbounds);
267
268                 ui_but_update(col_bt);  /* clips text again */
269         }
270 }
271
272 void ui_block_bounds_calc(uiBlock *block)
273 {
274         uiBut *bt;
275         int xof;
276         
277         if (BLI_listbase_is_empty(&block->buttons)) {
278                 if (block->panel) {
279                         block->rect.xmin = 0.0; block->rect.xmax = block->panel->sizex;
280                         block->rect.ymin = 0.0; block->rect.ymax = block->panel->sizey;
281                 }
282         }
283         else {
284         
285                 BLI_rctf_init_minmax(&block->rect);
286
287                 for (bt = block->buttons.first; bt; bt = bt->next) {
288                         BLI_rctf_union(&block->rect, &bt->rect);
289                 }
290                 
291                 block->rect.xmin -= block->bounds;
292                 block->rect.ymin -= block->bounds;
293                 block->rect.xmax += block->bounds;
294                 block->rect.ymax += block->bounds;
295         }
296
297         block->rect.xmax = block->rect.xmin + max_ff(BLI_rctf_size_x(&block->rect), block->minbounds);
298
299         /* hardcoded exception... but that one is annoying with larger safety */ 
300         bt = block->buttons.first;
301         if (bt && STREQLEN(bt->str, "ERROR", 5)) xof = 10;
302         else xof = 40;
303
304         block->safety.xmin = block->rect.xmin - xof;
305         block->safety.ymin = block->rect.ymin - xof;
306         block->safety.xmax = block->rect.xmax + xof;
307         block->safety.ymax = block->rect.ymax + xof;
308 }
309
310 static void ui_block_bounds_calc_centered(wmWindow *window, uiBlock *block)
311 {
312         int xmax, ymax;
313         int startx, starty;
314         int width, height;
315         
316         /* note: this is used for the splash where window bounds event has not been
317          * updated by ghost, get the window bounds from ghost directly */
318
319         xmax = WM_window_pixels_x(window);
320         ymax = WM_window_pixels_y(window);
321
322         ui_block_bounds_calc(block);
323         
324         width  = BLI_rctf_size_x(&block->rect);
325         height = BLI_rctf_size_y(&block->rect);
326         
327         startx = (xmax * 0.5f) - (width * 0.5f);
328         starty = (ymax * 0.5f) - (height * 0.5f);
329         
330         ui_block_translate(block, startx - block->rect.xmin, starty - block->rect.ymin);
331         
332         /* now recompute bounds and safety */
333         ui_block_bounds_calc(block);
334         
335 }
336
337 static void ui_block_bounds_calc_centered_pie(uiBlock *block)
338 {
339         const int xy[2] = {
340             block->pie_data.pie_center_spawned[0],
341             block->pie_data.pie_center_spawned[1]
342         };
343
344         ui_block_translate(block, xy[0], xy[1]);
345
346         /* now recompute bounds and safety */
347         ui_block_bounds_calc(block);
348 }
349
350 static void ui_block_bounds_calc_popup(
351         wmWindow *window, uiBlock *block,
352         eBlockBoundsCalc bounds_calc, const int xy[2])
353 {
354         int startx, starty, endx, endy, width, height, oldwidth, oldheight;
355         int oldbounds, xmax, ymax;
356         const int margin = UI_SCREEN_MARGIN;
357
358         oldbounds = block->bounds;
359
360         /* compute mouse position with user defined offset */
361         ui_block_bounds_calc(block);
362         
363         xmax = WM_window_pixels_x(window);
364         ymax = WM_window_pixels_y(window);
365
366         oldwidth  = BLI_rctf_size_x(&block->rect);
367         oldheight = BLI_rctf_size_y(&block->rect);
368
369         /* first we ensure wide enough text bounds */
370         if (bounds_calc == UI_BLOCK_BOUNDS_POPUP_MENU) {
371                 if (block->flag & UI_BLOCK_LOOP) {
372                         block->bounds = 2.5f * UI_UNIT_X;
373                         ui_block_bounds_calc_text(block, block->rect.xmin);
374                 }
375         }
376
377         /* next we recompute bounds */
378         block->bounds = oldbounds;
379         ui_block_bounds_calc(block);
380
381         /* and we adjust the position to fit within window */
382         width  = BLI_rctf_size_x(&block->rect);
383         height = BLI_rctf_size_y(&block->rect);
384
385         /* avoid divide by zero below, caused by calling with no UI, but better not crash */
386         oldwidth = oldwidth > 0 ? oldwidth : MAX2(1, width);
387         oldheight = oldheight > 0 ? oldheight : MAX2(1, height);
388
389         /* offset block based on mouse position, user offset is scaled
390          * along in case we resized the block in ui_block_bounds_calc_text */
391         startx = xy[0] + block->rect.xmin + (block->mx * width) / oldwidth;
392         starty = xy[1] + block->rect.ymin + (block->my * height) / oldheight;
393
394         if (startx < margin)
395                 startx = margin;
396         if (starty < margin)
397                 starty = margin;
398
399         endx = startx + width;
400         endy = starty + height;
401
402         if (endx > xmax) {
403                 endx = xmax - margin;
404                 startx = endx - width;
405         }
406         if (endy > ymax - margin) {
407                 endy = ymax - margin;
408                 starty = endy - height;
409         }
410
411         ui_block_translate(block, startx - block->rect.xmin, starty - block->rect.ymin);
412
413         /* now recompute bounds and safety */
414         ui_block_bounds_calc(block);
415 }
416
417 /* used for various cases */
418 void UI_block_bounds_set_normal(uiBlock *block, int addval)
419 {
420         if (block == NULL)
421                 return;
422         
423         block->bounds = addval;
424         block->bounds_type = UI_BLOCK_BOUNDS;
425 }
426
427 /* used for pulldowns */
428 void UI_block_bounds_set_text(uiBlock *block, int addval)
429 {
430         block->bounds = addval;
431         block->bounds_type = UI_BLOCK_BOUNDS_TEXT;
432 }
433
434 /* used for block popups */
435 void UI_block_bounds_set_popup(uiBlock *block, int addval, int mx, int my)
436 {
437         block->bounds = addval;
438         block->bounds_type = UI_BLOCK_BOUNDS_POPUP_MOUSE;
439         block->mx = mx;
440         block->my = my;
441 }
442
443 /* used for menu popups */
444 void UI_block_bounds_set_menu(uiBlock *block, int addval, int mx, int my)
445 {
446         block->bounds = addval;
447         block->bounds_type = UI_BLOCK_BOUNDS_POPUP_MENU;
448         block->mx = mx;
449         block->my = my;
450 }
451
452 /* used for centered popups, i.e. splash */
453 void UI_block_bounds_set_centered(uiBlock *block, int addval)
454 {
455         block->bounds = addval;
456         block->bounds_type = UI_BLOCK_BOUNDS_POPUP_CENTER;
457 }
458
459 void UI_block_bounds_set_explicit(uiBlock *block, int minx, int miny, int maxx, int maxy)
460 {
461         block->rect.xmin = minx;
462         block->rect.ymin = miny;
463         block->rect.xmax = maxx;
464         block->rect.ymax = maxy;
465         block->bounds_type = UI_BLOCK_BOUNDS_NONE;
466 }
467
468 static int ui_but_calc_float_precision(uiBut *but, double value)
469 {
470         int prec = (int)but->a2;
471
472         /* first check for various special cases:
473          * * If button is radians, we want additional precision (see T39861).
474          * * If prec is not set, we fallback to a simple default */
475         if (ui_but_is_unit_radians(but) && prec < 5) {
476                 prec = 5;
477         }
478         else if (prec == -1) {
479                 prec = (but->hardmax < 10.001f) ? 3 : 2;
480         }
481
482         return UI_calc_float_precision(prec, value);
483 }
484
485 /* ************** LINK LINE DRAWING  ************* */
486
487 /* link line drawing is not part of buttons or theme.. so we stick with it here */
488
489 static void ui_draw_linkline(uiLinkLine *line, int highlightActiveLines, int dashInactiveLines)
490 {
491         rcti rect;
492
493         if (line->from == NULL || line->to == NULL) return;
494         
495         rect.xmin = BLI_rctf_cent_x(&line->from->rect);
496         rect.ymin = BLI_rctf_cent_y(&line->from->rect);
497         rect.xmax = BLI_rctf_cent_x(&line->to->rect);
498         rect.ymax = BLI_rctf_cent_y(&line->to->rect);
499         
500         if (dashInactiveLines)
501                 UI_ThemeColor(TH_GRID);
502         else if (line->flag & UI_SELECT)
503                 glColor3ub(100, 100, 100);
504         else if (highlightActiveLines && ((line->from->flag & UI_ACTIVE) || (line->to->flag & UI_ACTIVE)))
505                 UI_ThemeColor(TH_TEXT_HI);
506         else
507                 glColor3ub(0, 0, 0);
508
509         ui_draw_link_bezier(&rect);
510 }
511
512 static void ui_draw_links(uiBlock *block)
513 {
514         uiBut *but;
515         uiLinkLine *line;
516
517         /* Draw the gray out lines. Do this first so they appear at the
518          * bottom of inactive or active lines.
519          * As we go, remember if we see any active or selected lines. */
520         bool found_selectline = false;
521         bool found_activeline = false;
522
523         for (but = block->buttons.first; but; but = but->next) {
524                 if (but->type == UI_BTYPE_LINK && but->link) {
525                         for (line = but->link->lines.first; line; line = line->next) {
526                                 if (!(line->from->flag & UI_ACTIVE) && !(line->to->flag & UI_ACTIVE)) {
527                                         if (line->deactive)
528                                                 ui_draw_linkline(line, 0, true);
529                                 }
530                                 else
531                                         found_activeline = true;
532
533                                 if ((line->from->flag & UI_SELECT) || (line->to->flag & UI_SELECT))
534                                         found_selectline = true;
535                         }
536                 }
537         }
538
539         /* Draw the inactive lines (lines with neither button being hovered over) */
540         for (but = block->buttons.first; but; but = but->next) {
541                 if (but->type == UI_BTYPE_LINK && but->link) {
542                         for (line = but->link->lines.first; line; line = line->next) {
543                                 if (!(line->from->flag & UI_ACTIVE) && !(line->to->flag & UI_ACTIVE)) {
544                                         if (!line->deactive)
545                                                 ui_draw_linkline(line, 0, false);
546                                 }
547                         }
548                 }
549         }
550
551         /* Draw any active lines (lines with either button being hovered over).
552          * Do this last so they appear on top of inactive and gray out lines. */
553         if (found_activeline) {
554                 for (but = block->buttons.first; but; but = but->next) {
555                         if (but->type == UI_BTYPE_LINK && but->link) {
556                                 for (line = but->link->lines.first; line; line = line->next) {
557                                         if ((line->from->flag & UI_ACTIVE) || (line->to->flag & UI_ACTIVE))
558                                                 ui_draw_linkline(line, !found_selectline, false);
559                                 }
560                         }
561                 }
562         }
563 }
564
565 /* ************** BLOCK ENDING FUNCTION ************* */
566
567 /* NOTE: if but->poin is allocated memory for every defbut, things fail... */
568 static bool ui_but_equals_old(const uiBut *but, const uiBut *oldbut)
569 {
570         /* various properties are being compared here, hopefully sufficient
571          * to catch all cases, but it is simple to add more checks later */
572         if (but->retval != oldbut->retval) return false;
573         if (but->rnapoin.data != oldbut->rnapoin.data) return false;
574         if (but->rnaprop != oldbut->rnaprop || but->rnaindex != oldbut->rnaindex) return false;
575         if (but->func != oldbut->func) return false;
576         if (but->funcN != oldbut->funcN) return false;
577         if (oldbut->func_arg1 != oldbut && but->func_arg1 != oldbut->func_arg1) return false;
578         if (oldbut->func_arg2 != oldbut && but->func_arg2 != oldbut->func_arg2) return false;
579         if (!but->funcN && ((but->poin != oldbut->poin && (uiBut *)oldbut->poin != oldbut) ||
580                             (but->pointype != oldbut->pointype))) return false;
581         if (but->optype != oldbut->optype) return false;
582
583         return true;
584 }
585
586 uiBut *ui_but_find_old(uiBlock *block_old, const uiBut *but_new)
587 {
588         uiBut *but_old;
589         for (but_old = block_old->buttons.first; but_old; but_old = but_old->next) {
590                 if (ui_but_equals_old(but_new, but_old)) {
591                         break;
592                 }
593         }
594         return but_old;
595 }
596 uiBut *ui_but_find_new(uiBlock *block_new, const uiBut *but_old)
597 {
598         uiBut *but_new;
599         for (but_new = block_new->buttons.first; but_new; but_new = but_new->next) {
600                 if (ui_but_equals_old(but_new, but_old)) {
601                         break;
602                 }
603         }
604         return but_new;
605 }
606
607 /* oldbut is being inserted in new block, so we use the lines from new button, and replace button pointers */
608 static void ui_but_update_linklines(uiBlock *block, uiBut *oldbut, uiBut *newbut)
609 {
610         uiLinkLine *line;
611         uiBut *but;
612         
613         /* if active button is UI_BTYPE_LINK */
614         if (newbut->type == UI_BTYPE_LINK && newbut->link) {
615                 
616                 SWAP(uiLink *, oldbut->link, newbut->link);
617                 
618                 for (line = oldbut->link->lines.first; line; line = line->next) {
619                         if (line->to == newbut)
620                                 line->to = oldbut;
621                         if (line->from == newbut)
622                                 line->from = oldbut;
623                 }
624         }
625         
626         /* check all other button links */
627         for (but = block->buttons.first; but; but = but->next) {
628                 if (but != newbut && but->type == UI_BTYPE_LINK && but->link) {
629                         for (line = but->link->lines.first; line; line = line->next) {
630                                 if (line->to == newbut)
631                                         line->to = oldbut;
632                                 if (line->from == newbut)
633                                         line->from = oldbut;
634                         }
635                 }
636         }
637 }
638
639 /**
640  * \return true when \a but_p is set (only done for active buttons).
641  */
642 static bool ui_but_update_from_old_block(const bContext *C, uiBlock *block, uiBut **but_p, uiBut **but_old_p)
643 {
644         const int drawflag_copy = 0;  /* None currently. */
645
646         uiBlock *oldblock = block->oldblock;
647         uiBut *oldbut = NULL, *but = *but_p;
648         bool found_active = false;
649
650
651 #if 0
652         /* simple/stupid - search every time */
653         oldbut = ui_but_find_old(oldblock, but);
654         (void)but_old_p;
655 #else
656         BLI_assert(*but_old_p == NULL || BLI_findindex(&oldblock->buttons, *but_old_p) != -1);
657
658         /* fastpath - avoid loop-in-loop, calling 'ui_but_find_old'
659          * as long as old/new buttons are aligned */
660         if (LIKELY(*but_old_p && ui_but_equals_old(but, *but_old_p))) {
661                 oldbut = *but_old_p;
662         }
663         else {
664                 /* fallback to block search */
665                 oldbut = ui_but_find_old(oldblock, but);
666         }
667         (*but_old_p) = oldbut ? oldbut->next : NULL;
668 #endif
669
670
671         if (!oldbut) {
672                 return found_active;
673         }
674
675         if (oldbut->active) {
676                 /* flags from the buttons we want to refresh, may want to add more here... */
677                 const int flag_copy = UI_BUT_REDALERT;
678
679                 found_active = true;
680
681 #if 0
682                 but->flag = oldbut->flag;
683                 but->active = oldbut->active;
684                 but->pos = oldbut->pos;
685                 but->ofs = oldbut->ofs;
686                 but->editstr = oldbut->editstr;
687                 but->editval = oldbut->editval;
688                 but->editvec = oldbut->editvec;
689                 but->editcoba = oldbut->editcoba;
690                 but->editcumap = oldbut->editcumap;
691                 but->selsta = oldbut->selsta;
692                 but->selend = oldbut->selend;
693                 but->softmin = oldbut->softmin;
694                 but->softmax = oldbut->softmax;
695                 but->linkto[0] = oldbut->linkto[0];
696                 but->linkto[1] = oldbut->linkto[1];
697                 oldbut->active = NULL;
698 #endif
699
700                 /* move button over from oldblock to new block */
701                 BLI_remlink(&oldblock->buttons, oldbut);
702                 BLI_insertlinkafter(&block->buttons, but, oldbut);
703                 oldbut->block = block;
704                 *but_p = oldbut;
705
706                 /* still stuff needs to be copied */
707                 oldbut->rect = but->rect;
708                 oldbut->context = but->context; /* set by Layout */
709
710                 /* drawing */
711                 oldbut->icon = but->icon;
712                 oldbut->iconadd = but->iconadd;
713                 oldbut->alignnr = but->alignnr;
714
715                 /* typically the same pointers, but not on undo/redo */
716                 /* XXX some menu buttons store button itself in but->poin. Ugly */
717                 if (oldbut->poin != (char *)oldbut) {
718                         SWAP(char *, oldbut->poin, but->poin);
719                         SWAP(void *, oldbut->func_argN, but->func_argN);
720                         SWAP(void *, oldbut->tip_argN, but->tip_argN);
721                 }
722
723                 oldbut->flag = (oldbut->flag & ~flag_copy) | (but->flag & flag_copy);
724                 oldbut->drawflag = (oldbut->drawflag & ~drawflag_copy) | (but->drawflag & drawflag_copy);
725
726                 /* copy hardmin for list rows to prevent 'sticking' highlight to mouse position
727                  * when scrolling without moving mouse (see [#28432]) */
728                 if (ELEM(oldbut->type, UI_BTYPE_ROW, UI_BTYPE_LISTROW))
729                         oldbut->hardmax = but->hardmax;
730
731                 ui_but_update_linklines(block, oldbut, but);
732
733                 if (!BLI_listbase_is_empty(&block->butstore)) {
734                         UI_butstore_register_update(block, oldbut, but);
735                 }
736
737                 /* move/copy string from the new button to the old */
738                 /* needed for alt+mouse wheel over enums */
739                 if (but->str != but->strdata) {
740                         if (oldbut->str != oldbut->strdata) {
741                                 SWAP(char *, but->str, oldbut->str);
742                         }
743                         else {
744                                 oldbut->str = but->str;
745                                 but->str = but->strdata;
746                         }
747                 }
748                 else {
749                         if (oldbut->str != oldbut->strdata) {
750                                 MEM_freeN(oldbut->str);
751                                 oldbut->str = oldbut->strdata;
752                         }
753                         BLI_strncpy(oldbut->strdata, but->strdata, sizeof(oldbut->strdata));
754                 }
755
756                 BLI_remlink(&block->buttons, but);
757                 ui_but_free(C, but);
758
759                 /* note: if layout hasn't been applied yet, it uses old button pointers... */
760         }
761         else {
762                 const int flag_copy = UI_BUT_DRAG_MULTI;
763
764                 but->flag = (but->flag & ~flag_copy) | (oldbut->flag & flag_copy);
765
766                 /* ensures one button can get activated, and in case the buttons
767                  * draw are the same this gives O(1) lookup for each button */
768                 BLI_remlink(&oldblock->buttons, oldbut);
769                 ui_but_free(C, oldbut);
770         }
771
772         return found_active;
773 }
774
775 /* needed for temporarily rename buttons, such as in outliner or file-select,
776  * they should keep calling uiDefButs to keep them alive */
777 /* returns 0 when button removed */
778 bool UI_but_active_only(const bContext *C, ARegion *ar, uiBlock *block, uiBut *but)
779 {
780         uiBlock *oldblock;
781         uiBut *oldbut;
782         bool activate = false, found = false, isactive = false;
783         
784         oldblock = block->oldblock;
785         if (!oldblock) {
786                 activate = true;
787         }
788         else {
789                 oldbut = ui_but_find_old(oldblock, but);
790                 if (oldbut) {
791                         found = true;
792
793                         if (oldbut->active) {
794                                 isactive = true;
795                         }
796                 }
797         }
798         if ((activate == true) || (found == false)) {
799                 ui_but_activate_event((bContext *)C, ar, but);
800         }
801         else if ((found == true) && (isactive == false)) {
802                 BLI_remlink(&block->buttons, but);
803                 ui_but_free(C, but);
804                 return false;
805         }
806         
807         return true;
808 }
809
810 /* simulate button click */
811 void UI_but_execute(const bContext *C, uiBut *but)
812 {
813         ARegion *ar = CTX_wm_region(C);
814         void *active_back;
815         ui_but_execute_begin((bContext *)C, ar, but, &active_back);
816         /* Value is applied in begin. No further action required. */
817         ui_but_execute_end((bContext *)C, ar, but, active_back);
818 }
819
820 /* use to check if we need to disable undo, but don't make any changes
821  * returns false if undo needs to be disabled. */
822 static bool ui_but_is_rna_undo(const uiBut *but)
823 {
824         if (but->rnapoin.id.data) {
825                 /* avoid undo push for buttons who's ID are screen or wm level
826                  * we could disable undo for buttons with no ID too but may have
827                  * unforeseen consequences, so best check for ID's we _know_ are not
828                  * handled by undo - campbell */
829                 ID *id = but->rnapoin.id.data;
830                 if (ID_CHECK_UNDO(id) == false) {
831                         return false;
832                 }
833                 else {
834                         return true;
835                 }
836         }
837         else if (but->rnapoin.type && !RNA_struct_undo_check(but->rnapoin.type)) {
838                 return false;
839         }
840
841         return true;
842 }
843
844 /* assigns automatic keybindings to menu items for fast access
845  * (underline key in menu) */
846 static void ui_menu_block_set_keyaccels(uiBlock *block)
847 {
848         uiBut *but;
849
850         unsigned int menu_key_mask = 0;
851         unsigned char menu_key;
852         const char *str_pt;
853         int pass;
854         int tot_missing = 0;
855
856         /* only do it before bounding */
857         if (block->rect.xmin != block->rect.xmax)
858                 return;
859
860         for (pass = 0; pass < 2; pass++) {
861                 /* 2 Passes, on for first letter only, second for any letter if first fails
862                  * fun first pass on all buttons so first word chars always get first priority */
863
864                 for (but = block->buttons.first; but; but = but->next) {
865                         if (!ELEM(but->type,
866                                   UI_BTYPE_BUT,
867                                   UI_BTYPE_BUT_MENU,
868                                   UI_BTYPE_MENU, UI_BTYPE_BLOCK,
869                                   UI_BTYPE_PULLDOWN) ||
870                             (but->flag & UI_HIDDEN))
871                         {
872                                 /* pass */
873                         }
874                         else if (but->menu_key == '\0') {
875                                 if (but->str) {
876                                         for (str_pt = but->str; *str_pt; ) {
877                                                 menu_key = tolower(*str_pt);
878                                                 if ((menu_key >= 'a' && menu_key <= 'z') && !(menu_key_mask & 1 << (menu_key - 'a'))) {
879                                                         menu_key_mask |= 1 << (menu_key - 'a');
880                                                         break;
881                                                 }
882
883                                                 if (pass == 0) {
884                                                         /* Skip to next delimiter on first pass (be picky) */
885                                                         while (isalpha(*str_pt))
886                                                                 str_pt++;
887
888                                                         if (*str_pt)
889                                                                 str_pt++;
890                                                 }
891                                                 else {
892                                                         /* just step over every char second pass and find first usable key */
893                                                         str_pt++;
894                                                 }
895                                         }
896
897                                         if (*str_pt) {
898                                                 but->menu_key = menu_key;
899                                         }
900                                         else {
901                                                 /* run second pass */
902                                                 tot_missing++;
903                                         }
904
905                                         /* if all keys have been used just exit, unlikely */
906                                         if (menu_key_mask == (1 << 26) - 1) {
907                                                 return;
908                                         }
909                                 }
910                         }
911                 }
912
913                 /* check if second pass is needed */
914                 if (!tot_missing) {
915                         break;
916                 }
917         }
918 }
919
920 /* XXX, this code will shorten any allocated string to 'UI_MAX_NAME_STR'
921  * since this is really long its unlikely to be an issue,
922  * but this could be supported */
923 void ui_but_add_shortcut(uiBut *but, const char *shortcut_str, const bool do_strip)
924 {
925
926         if (do_strip && (but->flag & UI_BUT_HAS_SEP_CHAR)) {
927                 char *cpoin = strrchr(but->str, UI_SEP_CHAR);
928                 if (cpoin) {
929                         *cpoin = '\0';
930                 }
931                 but->flag &= ~UI_BUT_HAS_SEP_CHAR;
932         }
933
934         /* without this, just allow stripping of the shortcut */
935         if (shortcut_str) {
936                 char *butstr_orig;
937
938                 if (but->str != but->strdata) {
939                         butstr_orig = but->str; /* free after using as source buffer */
940                 }
941                 else {
942                         butstr_orig = BLI_strdup(but->str);
943                 }
944                 BLI_snprintf(but->strdata,
945                              sizeof(but->strdata),
946                              "%s" UI_SEP_CHAR_S "%s",
947                              butstr_orig, shortcut_str);
948                 MEM_freeN(butstr_orig);
949                 but->str = but->strdata;
950                 but->flag |= UI_BUT_HAS_SEP_CHAR;
951                 ui_but_update(but);
952         }
953 }
954
955 static bool ui_but_event_operator_string(const bContext *C, uiBut *but, char *buf, const size_t buf_len)
956 {
957         MenuType *mt;
958         bool found = false;
959
960         if (but->optype) {
961                 IDProperty *prop = (but->opptr) ? but->opptr->data : NULL;
962
963                 if (WM_key_event_operator_string(C, but->optype->idname, but->opcontext, prop, true,
964                                                  buf, buf_len))
965                 {
966                         found = true;
967                 }
968         }
969         else if ((mt = UI_but_menutype_get(but))) {
970                 IDProperty *prop_menu;
971                 IDProperty *prop_menu_name;
972
973                 /* annoying, create a property */
974                 IDPropertyTemplate val = {0};
975                 prop_menu = IDP_New(IDP_GROUP, &val, __func__); /* dummy, name is unimportant  */
976                 IDP_AddToGroup(prop_menu, (prop_menu_name = IDP_NewString("", "name", sizeof(mt->idname))));
977
978                 IDP_AssignString(prop_menu_name, mt->idname, sizeof(mt->idname));
979
980                 if (WM_key_event_operator_string(C, "WM_OT_call_menu", WM_OP_INVOKE_REGION_WIN, prop_menu, true,
981                                                  buf, buf_len))
982                 {
983                         found = true;
984                 }
985
986                 IDP_FreeProperty(prop_menu);
987                 MEM_freeN(prop_menu);
988         }
989
990         return found;
991 }
992
993 static bool ui_but_event_property_operator_string(const bContext *C, uiBut *but, char *buf, const size_t buf_len)
994 {       
995         /* context toggle operator names to check... */
996         const char *ctx_toggle_opnames[] = {
997                 "WM_OT_context_toggle",
998                 "WM_OT_context_toggle_enum",
999                 "WM_OT_context_cycle_int",
1000                 "WM_OT_context_cycle_enum",
1001                 "WM_OT_context_cycle_array",
1002                 "WM_OT_context_menu_enum",
1003                 NULL
1004         };              
1005         const size_t num_ops = sizeof(ctx_toggle_opnames) / sizeof(const char *);
1006         
1007         bool found = false;
1008         
1009         /* this version is only for finding hotkeys for properties (which get set via context using operators) */
1010         if (but->rnaprop) {
1011                 /* to avoid massive slowdowns on property panels, for now, we only check the 
1012                  * hotkeys for Editor / Scene settings...
1013                  *
1014                  * TODO: userpref settings?
1015                  */
1016                 // TODO: value (for enum stuff)?
1017                 char *data_path = NULL;
1018                 
1019                 if (but->rnapoin.id.data) {
1020                         ID *id = but->rnapoin.id.data;
1021                         
1022                         if (GS(id->name) == ID_SCR) {
1023                                 /* screen/editor property 
1024                                  * NOTE: in most cases, there is actually no info for backwards tracing 
1025                                  * how to get back to ID from the editor data we may be dealing with
1026                                  */
1027                                 if (RNA_struct_is_a(but->rnapoin.type, &RNA_Space)) {
1028                                         /* data should be directly on here... */
1029                                         data_path = BLI_sprintfN("space_data.%s", RNA_property_identifier(but->rnaprop));
1030                                 }
1031                                 else {
1032                                         /* special exceptions for common nested data in editors... */
1033                                         if (RNA_struct_is_a(but->rnapoin.type, &RNA_DopeSheet)) {
1034                                                 /* dopesheet filtering options... */
1035                                                 data_path = BLI_sprintfN("space_data.dopesheet.%s", RNA_property_identifier(but->rnaprop));
1036                                         }
1037                                 }
1038                         }
1039                         else if (GS(id->name) == ID_SCE) {
1040                                 if (RNA_struct_is_a(but->rnapoin.type, &RNA_ToolSettings)) {
1041                                         /* toolsettings property 
1042                                          * NOTE: toolsettings is usually accessed directly (i.e. not through scene)
1043                                          */
1044                                         data_path = RNA_path_from_ID_to_property(&but->rnapoin, but->rnaprop);
1045                                 }
1046                                 else {
1047                                         /* scene property */
1048                                         char *path = RNA_path_from_ID_to_property(&but->rnapoin, but->rnaprop);
1049                                         
1050                                         if (path) {
1051                                                 data_path = BLI_sprintfN("scene.%s", path);
1052                                                 MEM_freeN(path);
1053                                         }
1054 #if 0
1055                                         else {
1056                                                 printf("ERROR in %s(): Couldn't get path for scene property - %s\n",
1057                                                        __func__, RNA_property_identifier(but->rnaprop));
1058                                         }
1059 #endif
1060                                 }
1061                         }
1062                         else {
1063                                 //puts("other id");
1064                         }
1065                         
1066                         //printf("prop shortcut: '%s' (%s)\n", RNA_property_identifier(but->rnaprop), data_path);
1067                 }
1068                 
1069                 /* we have a datapath! */
1070                 if (data_path) {
1071                         size_t i;
1072                         
1073                         /* create a property to host the "datapath" property we're sending to the operators */
1074                         IDProperty *prop_path;
1075                         IDProperty *prop_path_value;
1076                         
1077                         IDPropertyTemplate val = {0};
1078                         prop_path = IDP_New(IDP_GROUP, &val, __func__);
1079                         prop_path_value = IDP_NewString(data_path, "data_path", strlen(data_path) + 1);
1080                         IDP_AddToGroup(prop_path, prop_path_value);
1081                         
1082                         /* check each until one works... */
1083                         for (i = 0; (i < num_ops) && (ctx_toggle_opnames[i]); i++) {
1084                                 //printf("\t%s\n", ctx_toggle_opnames[i]);
1085                                 if (WM_key_event_operator_string(C, ctx_toggle_opnames[i], WM_OP_INVOKE_REGION_WIN, prop_path, false,
1086                                                                  buf, buf_len))
1087                                 {
1088                                         found = true;
1089                                         break;
1090                                 }
1091                         }
1092                         
1093                         /* cleanup */
1094                         IDP_FreeProperty(prop_path);
1095                         MEM_freeN(prop_path);
1096                         MEM_freeN(data_path);
1097                 }
1098         }
1099         
1100         return found;
1101 }
1102
1103 /**
1104  * This goes in a seemingly weird pattern:
1105  *
1106  * <pre>
1107  *     4
1108  *  5     6
1109  * 1       2
1110  *  7     8
1111  *     3
1112  * </pre>
1113  *
1114  * but it's actually quite logical. It's designed to be 'upwards compatible'
1115  * for muscle memory so that the menu item locations are fixed and don't move
1116  * as new items are added to the menu later on. It also optimises efficiency -
1117  * a radial menu is best kept symmetrical, with as large an angle between
1118  * items as possible, so that the gestural mouse movements can be fast and inexact.
1119
1120  * It starts off with two opposite sides for the first two items
1121  * then joined by the one below for the third (this way, even with three items,
1122  * the menu seems to still be 'in order' reading left to right). Then the fourth is
1123  * added to complete the compass directions. From here, it's just a matter of
1124  * subdividing the rest of the angles for the last 4 items.
1125  *
1126  * --Matt 07/2006
1127  */
1128 const char ui_radial_dir_order[8] = {
1129     UI_RADIAL_W,  UI_RADIAL_E,  UI_RADIAL_S,  UI_RADIAL_N,
1130     UI_RADIAL_NW, UI_RADIAL_NE, UI_RADIAL_SW, UI_RADIAL_SE};
1131
1132 const char  ui_radial_dir_to_numpad[8] = {8, 9, 6, 3, 2, 1, 4, 7};
1133 const short ui_radial_dir_to_angle[8] =  {90, 45, 0, 315, 270, 225, 180, 135};
1134
1135 static void ui_but_pie_direction_string(uiBut *but, char *buf, int size)
1136 {
1137         BLI_assert(but->pie_dir < ARRAY_SIZE(ui_radial_dir_to_numpad));
1138         BLI_snprintf(buf, size, "%d", ui_radial_dir_to_numpad[but->pie_dir]);
1139 }
1140
1141 static void ui_menu_block_set_keymaps(const bContext *C, uiBlock *block)
1142 {
1143         uiBut *but;
1144         char buf[128];
1145
1146         /* only do it before bounding */
1147         if (block->rect.xmin != block->rect.xmax)
1148                 return;
1149
1150         if (block->flag & UI_BLOCK_RADIAL) {
1151                 for (but = block->buttons.first; but; but = but->next) {
1152                         if (but->pie_dir != UI_RADIAL_NONE) {
1153                                 ui_but_pie_direction_string(but, buf, sizeof(buf));
1154                                 ui_but_add_shortcut(but, buf, false);
1155                         }
1156                 }
1157         }
1158         else {
1159                 for (but = block->buttons.first; but; but = but->next) {
1160
1161                         if (ui_but_event_operator_string(C, but, buf, sizeof(buf))) {
1162                                 ui_but_add_shortcut(but, buf, false);
1163                         }
1164                         else if (ui_but_event_property_operator_string(C, but, buf, sizeof(buf))) {
1165                                 ui_but_add_shortcut(but, buf, false);
1166                         }
1167                 }
1168         }
1169 }
1170
1171 void UI_block_update_from_old(const bContext *C, uiBlock *block)
1172 {
1173         uiBut *but_old;
1174         uiBut *but;
1175
1176         if (!block->oldblock)
1177                 return;
1178
1179         but_old = block->oldblock->buttons.first;
1180
1181         if (BLI_listbase_is_empty(&block->oldblock->butstore) == false) {
1182                 UI_butstore_update(block);
1183         }
1184
1185         for (but = block->buttons.first; but; but = but->next) {
1186                 if (ui_but_update_from_old_block(C, block, &but, &but_old)) {
1187                         ui_but_update(but);
1188                 }
1189         }
1190
1191         block->auto_open = block->oldblock->auto_open;
1192         block->auto_open_last = block->oldblock->auto_open_last;
1193         block->tooltipdisabled = block->oldblock->tooltipdisabled;
1194         BLI_movelisttolist(&block->color_pickers.list, &block->oldblock->color_pickers.list);
1195
1196         block->oldblock = NULL;
1197 }
1198
1199 void UI_block_end_ex(const bContext *C, uiBlock *block, const int xy[2])
1200 {
1201         wmWindow *window = CTX_wm_window(C);
1202         Scene *scene = CTX_data_scene(C);
1203         uiBut *but;
1204
1205         BLI_assert(block->active);
1206
1207         UI_block_update_from_old(C, block);
1208
1209         /* inherit flags from 'old' buttons that was drawn here previous, based
1210          * on matching buttons, we need this to make button event handling non
1211          * blocking, while still allowing buttons to be remade each redraw as it
1212          * is expected by blender code */
1213         for (but = block->buttons.first; but; but = but->next) {
1214                 /* temp? Proper check for graying out */
1215                 if (but->optype) {
1216                         wmOperatorType *ot = but->optype;
1217
1218                         if (but->context)
1219                                 CTX_store_set((bContext *)C, but->context);
1220
1221                         if (ot == NULL || WM_operator_poll_context((bContext *)C, ot, but->opcontext) == 0) {
1222                                 but->flag |= UI_BUT_DISABLED;
1223                                 but->lock = true;
1224                         }
1225
1226                         if (but->context)
1227                                 CTX_store_set((bContext *)C, NULL);
1228                 }
1229
1230                 ui_but_anim_flag(but, (scene) ? scene->r.cfra : 0.0f);
1231         }
1232
1233
1234
1235         /* handle pending stuff */
1236         if (block->layouts.first) {
1237                 UI_block_layout_resolve(block, NULL, NULL);
1238         }
1239         ui_block_align_calc(block);
1240         if ((block->flag & UI_BLOCK_LOOP) && (block->flag & UI_BLOCK_NUMSELECT)) {
1241                 ui_menu_block_set_keyaccels(block); /* could use a different flag to check */
1242         }
1243
1244         if (block->flag & UI_BLOCK_LOOP) {
1245                 ui_menu_block_set_keymaps(C, block);
1246         }
1247         
1248         /* after keymaps! */
1249         switch (block->bounds_type) {
1250                 case UI_BLOCK_BOUNDS_NONE:
1251                         break;
1252                 case UI_BLOCK_BOUNDS:
1253                         ui_block_bounds_calc(block);
1254                         break;
1255                 case UI_BLOCK_BOUNDS_TEXT:
1256                         ui_block_bounds_calc_text(block, 0.0f);
1257                         break;
1258                 case UI_BLOCK_BOUNDS_POPUP_CENTER:
1259                         ui_block_bounds_calc_centered(window, block);
1260                         break;
1261                 case UI_BLOCK_BOUNDS_PIE_CENTER:
1262                         ui_block_bounds_calc_centered_pie(block);
1263                         break;
1264
1265                         /* fallback */
1266                 case UI_BLOCK_BOUNDS_POPUP_MOUSE:
1267                 case UI_BLOCK_BOUNDS_POPUP_MENU:
1268                         ui_block_bounds_calc_popup(window, block, block->bounds_type, xy);
1269                         break;
1270         }
1271
1272         if (block->rect.xmin == 0.0f && block->rect.xmax == 0.0f) {
1273                 UI_block_bounds_set_normal(block, 0);
1274         }
1275         if (block->flag & UI_BUT_ALIGN) {
1276                 UI_block_align_end(block);
1277         }
1278
1279         block->endblock = 1;
1280 }
1281
1282 void UI_block_end(const bContext *C, uiBlock *block)
1283 {
1284         wmWindow *window = CTX_wm_window(C);
1285
1286         UI_block_end_ex(C, block, &window->eventstate->x);
1287 }
1288
1289 /* ************** BLOCK DRAWING FUNCTION ************* */
1290
1291 void ui_fontscale(short *points, float aspect)
1292 {
1293         if (aspect < 0.9f || aspect > 1.1f) {
1294                 float pointsf = *points;
1295                 
1296                 /* for some reason scaling fonts goes too fast compared to widget size */
1297                 /* XXX not true anymore? (ton) */
1298                 //aspect = sqrt(aspect);
1299                 pointsf /= aspect;
1300                 
1301                 if (aspect > 1.0f)
1302                         *points = ceilf(pointsf);
1303                 else
1304                         *points = floorf(pointsf);
1305         }
1306 }
1307
1308 /* project button or block (but==NULL) to pixels in regionspace */
1309 static void ui_but_to_pixelrect(rcti *rect, const ARegion *ar, uiBlock *block, uiBut *but)
1310 {
1311         rctf rectf;
1312
1313         ui_block_to_window_rctf(ar, block, &rectf, (but) ? &but->rect : &block->rect);
1314
1315         rectf.xmin -= ar->winrct.xmin;
1316         rectf.ymin -= ar->winrct.ymin;
1317         rectf.xmax -= ar->winrct.xmin;
1318         rectf.ymax -= ar->winrct.ymin;
1319
1320         rect->xmin = floorf(rectf.xmin);
1321         rect->ymin = floorf(rectf.ymin);
1322         rect->xmax = floorf(rectf.xmax);
1323         rect->ymax = floorf(rectf.ymax);
1324 }
1325
1326 /* uses local copy of style, to scale things down, and allow widgets to change stuff */
1327 void UI_block_draw(const bContext *C, uiBlock *block)
1328 {
1329         uiStyle style = *UI_style_get_dpi();  /* XXX pass on as arg */
1330         ARegion *ar;
1331         uiBut *but;
1332         rcti rect;
1333         int multisample_enabled;
1334         
1335         /* get menu region or area region */
1336         ar = CTX_wm_menu(C);
1337         if (!ar)
1338                 ar = CTX_wm_region(C);
1339
1340         if (!block->endblock)
1341                 UI_block_end(C, block);
1342
1343         /* disable AA, makes widgets too blurry */
1344         multisample_enabled = glIsEnabled(GL_MULTISAMPLE_ARB);
1345         if (multisample_enabled)
1346                 glDisable(GL_MULTISAMPLE_ARB);
1347
1348         /* we set this only once */
1349         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1350         
1351         /* scale fonts */
1352         ui_fontscale(&style.paneltitle.points, block->aspect);
1353         ui_fontscale(&style.grouplabel.points, block->aspect);
1354         ui_fontscale(&style.widgetlabel.points, block->aspect);
1355         ui_fontscale(&style.widget.points, block->aspect);
1356         
1357         /* scale block min/max to rect */
1358         ui_but_to_pixelrect(&rect, ar, block, NULL);
1359         
1360         /* pixel space for AA widgets */
1361         glMatrixMode(GL_PROJECTION);
1362         glPushMatrix();
1363         glMatrixMode(GL_MODELVIEW);
1364         glPushMatrix();
1365         glLoadIdentity();
1366
1367         wmOrtho2_region_ui(ar);
1368         
1369         /* back */
1370         if (block->flag & UI_BLOCK_RADIAL)
1371                 ui_draw_pie_center(block);
1372         else if (block->flag & UI_BLOCK_LOOP)
1373                 ui_draw_menu_back(&style, block, &rect);
1374         else if (block->panel)
1375                 ui_draw_aligned_panel(&style, block, &rect, UI_panel_category_is_visible(ar));
1376
1377         /* widgets */
1378         for (but = block->buttons.first; but; but = but->next) {
1379                 if (!(but->flag & (UI_HIDDEN | UI_SCROLLED))) {
1380                         ui_but_to_pixelrect(&rect, ar, block, but);
1381                 
1382                         /* XXX: figure out why invalid coordinates happen when closing render window */
1383                         /* and material preview is redrawn in main window (temp fix for bug #23848) */
1384                         if (rect.xmin < rect.xmax && rect.ymin < rect.ymax)
1385                                 ui_draw_but(C, ar, &style, but, &rect);
1386                 }
1387         }
1388         
1389         /* restore matrix */
1390         glMatrixMode(GL_PROJECTION);
1391         glPopMatrix();
1392         glMatrixMode(GL_MODELVIEW);
1393         glPopMatrix();
1394
1395         if (multisample_enabled)
1396                 glEnable(GL_MULTISAMPLE_ARB);
1397         
1398         ui_draw_links(block);
1399 }
1400
1401 /* ************* EVENTS ************* */
1402
1403 /**
1404  * Check if the button is pushed, this is only meaningful for some button types.
1405  *
1406  * \return (0 == UNSELECT), (1 == SELECT), (-1 == DO-NOTHING)
1407  */
1408 int ui_but_is_pushed_ex(uiBut *but, double *value)
1409 {
1410         int is_push = 0;
1411
1412         if (but->bit) {
1413                 const bool state = ELEM(but->type, UI_BTYPE_TOGGLE_N, UI_BTYPE_ICON_TOGGLE_N, UI_BTYPE_CHECKBOX_N) ? false : true;
1414                 int lvalue;
1415                 UI_GET_BUT_VALUE_INIT(but, *value);
1416                 lvalue = (int)*value;
1417                 if (UI_BITBUT_TEST(lvalue, (but->bitnr))) {
1418                         is_push = state;
1419                 }
1420                 else {
1421                         is_push = !state;
1422                 }
1423         }
1424         else {
1425                 switch (but->type) {
1426                         case UI_BTYPE_BUT:
1427                         case UI_BTYPE_HOTKEY_EVENT:
1428                         case UI_BTYPE_KEY_EVENT:
1429                         case UI_BTYPE_COLOR:
1430                                 is_push = -1;
1431                                 break;
1432                         case UI_BTYPE_BUT_TOGGLE:
1433                         case UI_BTYPE_TOGGLE:
1434                         case UI_BTYPE_ICON_TOGGLE:
1435                         case UI_BTYPE_CHECKBOX:
1436                                 UI_GET_BUT_VALUE_INIT(but, *value);
1437                                 if (*value != (double)but->hardmin) is_push = true;
1438                                 break;
1439                         case UI_BTYPE_ICON_TOGGLE_N:
1440                         case UI_BTYPE_TOGGLE_N:
1441                         case UI_BTYPE_CHECKBOX_N:
1442                                 UI_GET_BUT_VALUE_INIT(but, *value);
1443                                 if (*value == 0.0) is_push = true;
1444                                 break;
1445                         case UI_BTYPE_ROW:
1446                         case UI_BTYPE_LISTROW:
1447                                 UI_GET_BUT_VALUE_INIT(but, *value);
1448                                 /* support for rna enum buts */
1449                                 if (but->rnaprop && (RNA_property_flag(but->rnaprop) & PROP_ENUM_FLAG)) {
1450                                         if ((int)*value & (int)but->hardmax) is_push = true;
1451                                 }
1452                                 else {
1453                                         if (*value == (double)but->hardmax) is_push = true;
1454                                 }
1455                                 break;
1456                         default:
1457                                 is_push = -1;
1458                                 break;
1459                 }
1460         }
1461
1462         return is_push;
1463 }
1464 int ui_but_is_pushed(uiBut *but)
1465 {
1466         double value = UI_BUT_VALUE_UNSET;
1467         return ui_but_is_pushed_ex(but, &value);
1468 }
1469
1470 static void ui_but_update_select_flag(uiBut *but, double *value)
1471 {
1472         switch (ui_but_is_pushed_ex(but, value)) {
1473                 case true:
1474                         but->flag |= UI_SELECT;
1475                         break;
1476                 case false:
1477                         but->flag &= ~UI_SELECT;
1478                         break;
1479         }
1480 }
1481
1482 static uiBut *ui_linkline_find_inlink(uiBlock *block, void *poin)
1483 {
1484         uiBut *but;
1485         
1486         but = block->buttons.first;
1487         while (but) {
1488                 if (but->type == UI_BTYPE_INLINK) {
1489                         if (but->poin == poin) return but;
1490                 }
1491                 but = but->next;
1492         }
1493         return NULL;
1494 }
1495
1496 static void ui_linkline_add(ListBase *listb, uiBut *but, uiBut *bt, short deactive)
1497 {
1498         uiLinkLine *line;
1499         
1500         line = MEM_callocN(sizeof(uiLinkLine), "linkline");
1501         BLI_addtail(listb, line);
1502         line->from = but;
1503         line->to = bt;
1504         line->deactive = deactive;
1505 }
1506
1507 uiBut *UI_block_links_find_inlink(uiBlock *block, void *poin)
1508 {
1509         return ui_linkline_find_inlink(block, poin);
1510 }
1511
1512 void UI_block_links_compose(uiBlock *block)
1513 {
1514         uiBut *but, *bt;
1515         uiLink *link;
1516         void ***ppoin;
1517         int a;
1518         
1519         but = block->buttons.first;
1520         while (but) {
1521                 if (but->type == UI_BTYPE_LINK) {
1522                         link = but->link;
1523                         
1524                         /* for all pointers in the array */
1525                         if (link) {
1526                                 if (link->ppoin) {
1527                                         ppoin = link->ppoin;
1528                                         for (a = 0; a < *(link->totlink); a++) {
1529                                                 bt = ui_linkline_find_inlink(block, (*ppoin)[a]);
1530                                                 if (bt) {
1531                                                         if ((but->flag & UI_BUT_SCA_LINK_GREY) || (bt->flag & UI_BUT_SCA_LINK_GREY)) {
1532                                                                 ui_linkline_add(&link->lines, but, bt, true);
1533                                                         }
1534                                                         else {
1535                                                                 ui_linkline_add(&link->lines, but, bt, false);
1536                                                         }
1537
1538                                                 }
1539                                         }
1540                                 }
1541                                 else if (link->poin) {
1542                                         bt = ui_linkline_find_inlink(block, *link->poin);
1543                                         if (bt) {
1544                                                 if ((but->flag & UI_BUT_SCA_LINK_GREY) || (bt->flag & UI_BUT_SCA_LINK_GREY)) {
1545                                                         ui_linkline_add(&link->lines, but, bt, true);
1546                                                 }
1547                                                 else {
1548                                                         ui_linkline_add(&link->lines, but, bt, false);
1549                                                 }
1550                                         }
1551                                 }
1552                         }
1553                 }
1554                 but = but->next;
1555         }
1556 }
1557
1558
1559 /* ************************************************ */
1560
1561 void UI_block_lock_set(uiBlock *block, bool val, const char *lockstr)
1562 {
1563         if (val) {
1564                 block->lock = val;
1565                 block->lockstr = lockstr;
1566         }
1567 }
1568
1569 void UI_block_lock_clear(uiBlock *block)
1570 {
1571         block->lock = false;
1572         block->lockstr = NULL;
1573 }
1574
1575 /* *************************************************************** */
1576
1577 void ui_linkline_remove(uiLinkLine *line, uiBut *but)
1578 {
1579         uiLink *link;
1580         int a, b;
1581         
1582         BLI_remlink(&but->link->lines, line);
1583
1584         link = line->from->link;
1585
1586         /* are there more pointers allowed? */
1587         if (link->ppoin) {
1588                 
1589                 if (*(link->totlink) == 1) {
1590                         *(link->totlink) = 0;
1591                         MEM_freeN(*(link->ppoin));
1592                         *(link->ppoin) = NULL;
1593                 }
1594                 else {
1595                         b = 0;
1596                         for (a = 0; a < (*(link->totlink)); a++) {
1597                                 if ((*(link->ppoin))[a] != line->to->poin) {
1598                                         (*(link->ppoin))[b] = (*(link->ppoin))[a];
1599                                         b++;
1600                                 }
1601                         }
1602                         (*(link->totlink))--;
1603                 }
1604         }
1605         else {
1606                 *(link->poin) = NULL;
1607         }
1608
1609         MEM_freeN(line);
1610         //REDRAW
1611 }
1612
1613 /* *********************** data get/set ***********************
1614  * this either works with the pointed to data, or can work with
1615  * an edit override pointer while dragging for example */
1616
1617 /* for buttons pointing to color for example */
1618 void ui_but_v3_get(uiBut *but, float vec[3])
1619 {
1620         PropertyRNA *prop;
1621         int a;
1622
1623         if (but->editvec) {
1624                 copy_v3_v3(vec, but->editvec);
1625         }
1626
1627         if (but->rnaprop) {
1628                 prop = but->rnaprop;
1629
1630                 zero_v3(vec);
1631
1632                 if (RNA_property_type(prop) == PROP_FLOAT) {
1633                         int tot = RNA_property_array_length(&but->rnapoin, prop);
1634                         BLI_assert(tot > 0);
1635                         if (tot == 3) {
1636                                 RNA_property_float_get_array(&but->rnapoin, prop, vec);
1637                         }
1638                         else {
1639                                 tot = min_ii(tot, 3);
1640                                 for (a = 0; a < tot; a++) {
1641                                         vec[a] = RNA_property_float_get_index(&but->rnapoin, prop, a);
1642                                 }
1643                         }
1644                 }
1645         }
1646         else if (but->pointype == UI_BUT_POIN_CHAR) {
1647                 const char *cp = (char *)but->poin;
1648
1649                 vec[0] = ((float)cp[0]) / 255.0f;
1650                 vec[1] = ((float)cp[1]) / 255.0f;
1651                 vec[2] = ((float)cp[2]) / 255.0f;
1652         }
1653         else if (but->pointype == UI_BUT_POIN_FLOAT) {
1654                 const float *fp = (float *)but->poin;
1655                 copy_v3_v3(vec, fp);
1656         }
1657         else {
1658                 if (but->editvec == NULL) {
1659                         fprintf(stderr, "%s: can't get color, should never happen\n", __func__);
1660                         zero_v3(vec);
1661                 }
1662         }
1663
1664         if (but->type == UI_BTYPE_UNITVEC) {
1665                 normalize_v3(vec);
1666         }
1667 }
1668
1669 /* for buttons pointing to color for example */
1670 void ui_but_v3_set(uiBut *but, const float vec[3])
1671 {
1672         PropertyRNA *prop;
1673
1674         if (but->editvec) {
1675                 copy_v3_v3(but->editvec, vec);
1676         }
1677
1678         if (but->rnaprop) {
1679                 prop = but->rnaprop;
1680
1681                 if (RNA_property_type(prop) == PROP_FLOAT) {
1682                         int tot;
1683                         int a;
1684
1685                         tot = RNA_property_array_length(&but->rnapoin, prop);
1686                         BLI_assert(tot > 0);
1687                         if (tot == 3) {
1688                                 RNA_property_float_set_array(&but->rnapoin, prop, vec);
1689                         }
1690                         else {
1691                                 tot = min_ii(tot, 3);
1692                                 for (a = 0; a < tot; a++) {
1693                                         RNA_property_float_set_index(&but->rnapoin, prop, a, vec[a]);
1694                                 }
1695                         }
1696                 }
1697         }
1698         else if (but->pointype == UI_BUT_POIN_CHAR) {
1699                 char *cp = (char *)but->poin;
1700                 cp[0] = (char)(0.5f + vec[0] * 255.0f);
1701                 cp[1] = (char)(0.5f + vec[1] * 255.0f);
1702                 cp[2] = (char)(0.5f + vec[2] * 255.0f);
1703         }
1704         else if (but->pointype == UI_BUT_POIN_FLOAT) {
1705                 float *fp = (float *)but->poin;
1706                 copy_v3_v3(fp, vec);
1707         }
1708 }
1709
1710 bool ui_but_is_float(const uiBut *but)
1711 {
1712         if (but->pointype == UI_BUT_POIN_FLOAT && but->poin)
1713                 return true;
1714         
1715         if (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_FLOAT)
1716                 return true;
1717         
1718         return false;
1719 }
1720
1721 bool ui_but_is_bool(const uiBut *but)
1722 {
1723         if (ELEM(but->type, UI_BTYPE_TOGGLE, UI_BTYPE_TOGGLE_N, UI_BTYPE_ICON_TOGGLE, UI_BTYPE_ICON_TOGGLE_N))
1724                 return true;
1725
1726         if (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_BOOLEAN)
1727                 return true;
1728
1729         if ((but->rnaprop && RNA_property_type(but->rnaprop) == PROP_ENUM) && (but->type == UI_BTYPE_ROW))
1730                 return true;
1731
1732         return false;
1733 }
1734
1735
1736 bool ui_but_is_unit(const uiBut *but)
1737 {
1738         UnitSettings *unit = but->block->unit;
1739         const int unit_type = UI_but_unit_type_get(but);
1740
1741         if (unit_type == PROP_UNIT_NONE)
1742                 return false;
1743
1744 #if 1 /* removed so angle buttons get correct snapping */
1745         if (ui_but_is_unit_radians_ex(unit, unit_type))
1746                 return false;
1747 #endif
1748         
1749         /* for now disable time unit conversion */
1750         if (unit_type == PROP_UNIT_TIME)
1751                 return false;
1752
1753         if (unit->system == USER_UNIT_NONE) {
1754                 if (unit_type != PROP_UNIT_ROTATION) {
1755                         return false;
1756                 }
1757         }
1758
1759         return true;
1760 }
1761
1762 /**
1763  * Check if this button is similar enough to be grouped with another.
1764  */
1765 bool ui_but_is_compatible(const uiBut *but_a, const uiBut *but_b)
1766 {
1767         if (but_a->type != but_b->type)
1768                 return false;
1769         if (but_a->pointype != but_b->pointype)
1770                 return false;
1771
1772         if (but_a->rnaprop) {
1773                 /* skip 'rnapoin.data', 'rnapoin.id.data'
1774                  * allow different data to have the same props edited at once */
1775                 if (but_a->rnapoin.type != but_b->rnapoin.type)
1776                         return false;
1777                 if (RNA_property_type(but_a->rnaprop) != RNA_property_type(but_b->rnaprop))
1778                         return false;
1779                 if (RNA_property_subtype(but_a->rnaprop) != RNA_property_subtype(but_b->rnaprop))
1780                         return false;
1781         }
1782
1783         return true;
1784 }
1785
1786 bool ui_but_is_rna_valid(uiBut *but)
1787 {
1788         if (but->rnaprop == NULL || RNA_struct_contains_property(&but->rnapoin, but->rnaprop)) {
1789                 return true;
1790         }
1791         else {
1792                 printf("property removed %s: %p\n", but->drawstr, but->rnaprop);
1793                 return false;
1794         }
1795 }
1796
1797 double ui_but_value_get(uiBut *but)
1798 {
1799         PropertyRNA *prop;
1800         double value = 0.0;
1801
1802         if (but->editval) { return *(but->editval); }
1803         if (but->poin == NULL && but->rnapoin.data == NULL) return 0.0;
1804
1805         if (but->rnaprop) {
1806                 prop = but->rnaprop;
1807
1808                 BLI_assert(but->rnaindex != -1);
1809
1810                 switch (RNA_property_type(prop)) {
1811                         case PROP_BOOLEAN:
1812                                 if (RNA_property_array_check(prop))
1813                                         value = RNA_property_boolean_get_index(&but->rnapoin, prop, but->rnaindex);
1814                                 else
1815                                         value = RNA_property_boolean_get(&but->rnapoin, prop);
1816                                 break;
1817                         case PROP_INT:
1818                                 if (RNA_property_array_check(prop))
1819                                         value = RNA_property_int_get_index(&but->rnapoin, prop, but->rnaindex);
1820                                 else
1821                                         value = RNA_property_int_get(&but->rnapoin, prop);
1822                                 break;
1823                         case PROP_FLOAT:
1824                                 if (RNA_property_array_check(prop))
1825                                         value = RNA_property_float_get_index(&but->rnapoin, prop, but->rnaindex);
1826                                 else
1827                                         value = RNA_property_float_get(&but->rnapoin, prop);
1828                                 break;
1829                         case PROP_ENUM:
1830                                 value = RNA_property_enum_get(&but->rnapoin, prop);
1831                                 break;
1832                         default:
1833                                 value = 0.0;
1834                                 break;
1835                 }
1836         }
1837         else if (but->pointype == UI_BUT_POIN_CHAR) {
1838                 value = *(char *)but->poin;
1839         }
1840         else if (but->pointype == UI_BUT_POIN_SHORT) {
1841                 value = *(short *)but->poin;
1842         }
1843         else if (but->pointype == UI_BUT_POIN_INT) {
1844                 value = *(int *)but->poin;
1845         }
1846         else if (but->pointype == UI_BUT_POIN_FLOAT) {
1847                 value = *(float *)but->poin;
1848         }
1849
1850         return value;
1851 }
1852
1853 void ui_but_value_set(uiBut *but, double value)
1854 {
1855         PropertyRNA *prop;
1856
1857         /* value is a hsv value: convert to rgb */
1858         if (but->rnaprop) {
1859                 prop = but->rnaprop;
1860
1861                 if (RNA_property_editable(&but->rnapoin, prop)) {
1862                         switch (RNA_property_type(prop)) {
1863                                 case PROP_BOOLEAN:
1864                                         if (RNA_property_array_check(prop))
1865                                                 RNA_property_boolean_set_index(&but->rnapoin, prop, but->rnaindex, value);
1866                                         else
1867                                                 RNA_property_boolean_set(&but->rnapoin, prop, value);
1868                                         break;
1869                                 case PROP_INT:
1870                                         if (RNA_property_array_check(prop))
1871                                                 RNA_property_int_set_index(&but->rnapoin, prop, but->rnaindex, (int)value);
1872                                         else
1873                                                 RNA_property_int_set(&but->rnapoin, prop, (int)value);
1874                                         break;
1875                                 case PROP_FLOAT:
1876                                         if (RNA_property_array_check(prop))
1877                                                 RNA_property_float_set_index(&but->rnapoin, prop, but->rnaindex, value);
1878                                         else
1879                                                 RNA_property_float_set(&but->rnapoin, prop, value);
1880                                         break;
1881                                 case PROP_ENUM:
1882                                         if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
1883                                                 int ivalue = (int)value;
1884                                                 ivalue ^= RNA_property_enum_get(&but->rnapoin, prop); /* toggle for enum/flag buttons */
1885                                                 RNA_property_enum_set(&but->rnapoin, prop, ivalue);
1886                                         }
1887                                         else {
1888                                                 RNA_property_enum_set(&but->rnapoin, prop, value);
1889                                         }
1890                                         break;
1891                                 default:
1892                                         break;
1893                         }
1894                 }
1895
1896                 /* we can't be sure what RNA set functions actually do,
1897                  * so leave this unset */
1898                 value = UI_BUT_VALUE_UNSET;
1899         }
1900         else if (but->pointype == 0) {
1901                 /* pass */
1902         }
1903         else {
1904                 /* first do rounding */
1905                 if (but->pointype == UI_BUT_POIN_CHAR) {
1906                         value = (char)floor(value + 0.5);
1907                 }
1908                 else if (but->pointype == UI_BUT_POIN_SHORT) {
1909                         value = (short)floor(value + 0.5);
1910                 }
1911                 else if (but->pointype == UI_BUT_POIN_INT)
1912                         value = (int)floor(value + 0.5);
1913                 else if (but->pointype == UI_BUT_POIN_FLOAT) {
1914                         float fval = (float)value;
1915                         if (fval >= -0.00001f && fval <= 0.00001f) fval = 0.0f;  /* prevent negative zero */
1916                         value = fval;
1917                 }
1918                 
1919                 /* then set value with possible edit override */
1920                 if (but->editval)
1921                         value = *but->editval = value;
1922                 else if (but->pointype == UI_BUT_POIN_CHAR)
1923                         value = *((char *)but->poin) = (char)value;
1924                 else if (but->pointype == UI_BUT_POIN_SHORT)
1925                         value = *((short *)but->poin) = (short)value;
1926                 else if (but->pointype == UI_BUT_POIN_INT)
1927                         value = *((int *)but->poin) = (int)value;
1928                 else if (but->pointype == UI_BUT_POIN_FLOAT)
1929                         value = *((float *)but->poin) = (float)value;
1930         }
1931
1932         ui_but_update_select_flag(but, &value);
1933 }
1934
1935 int ui_but_string_get_max_length(uiBut *but)
1936 {
1937         if (ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU))
1938                 return but->hardmax;
1939         else
1940                 return UI_MAX_DRAW_STR;
1941 }
1942
1943 uiBut *ui_but_drag_multi_edit_get(uiBut *but)
1944 {
1945         uiBut *but_iter;
1946
1947         BLI_assert(but->flag & UI_BUT_DRAG_MULTI);
1948
1949         for (but_iter = but->block->buttons.first; but_iter; but_iter = but_iter->next) {
1950                 if (but_iter->editstr) {
1951                         break;
1952                 }
1953         }
1954
1955         return but_iter;
1956 }
1957
1958 /** \name Check to show extra icons
1959  *
1960  * Extra icons are shown on the right hand side of buttons.
1961  * \{ */
1962
1963 static bool ui_but_icon_extra_is_visible_search_unlink(const uiBut *but)
1964 {
1965         BLI_assert(but->type == UI_BTYPE_SEARCH_MENU);
1966         return ((but->editstr == NULL) &&
1967                 (but->drawstr[0] != '\0') &&
1968                 (but->flag & UI_BUT_SEARCH_UNLINK));
1969 }
1970
1971 static bool ui_but_icon_extra_is_visible_eyedropper(uiBut *but)
1972 {
1973         StructRNA *type;
1974         short idcode;
1975
1976         BLI_assert(but->type == UI_BTYPE_SEARCH_MENU && (but->flag & UI_BUT_SEARCH_UNLINK));
1977
1978         if (but->rnaprop == NULL) {
1979                 return false;
1980         }
1981
1982         type = RNA_property_pointer_type(&but->rnapoin, but->rnaprop);
1983         idcode = RNA_type_to_ID_code(type);
1984
1985
1986         return ((but->editstr == NULL) &&
1987                 (idcode == ID_OB || OB_DATA_SUPPORT_ID(idcode)));
1988 }
1989
1990 uiButExtraIconType ui_but_icon_extra_get(uiBut *but)
1991 {
1992         if ((but->flag & UI_BUT_SEARCH_UNLINK) == 0) {
1993                 /* pass */
1994         }
1995         else if (ui_but_icon_extra_is_visible_search_unlink(but)) {
1996                 return UI_BUT_ICONEXTRA_UNLINK;
1997         }
1998         else if (ui_but_icon_extra_is_visible_eyedropper(but)) {
1999                 return UI_BUT_ICONEXTRA_EYEDROPPER;
2000         }
2001
2002         return UI_BUT_ICONEXTRA_NONE;
2003 }
2004
2005 /** \} */
2006
2007
2008 static double ui_get_but_scale_unit(uiBut *but, double value)
2009 {
2010         UnitSettings *unit = but->block->unit;
2011         int unit_type = UI_but_unit_type_get(but);
2012
2013         /* Time unit is a bit special, not handled by BKE_scene_unit_scale() for now. */
2014         if (unit_type == PROP_UNIT_TIME) { /* WARNING - using evil_C :| */
2015                 Scene *scene = CTX_data_scene(but->block->evil_C);
2016                 return FRA2TIME(value);
2017         }
2018         else {
2019                 return BKE_scene_unit_scale(unit, RNA_SUBTYPE_UNIT_VALUE(unit_type), value);
2020         }
2021 }
2022
2023 /* str will be overwritten */
2024 void ui_but_convert_to_unit_alt_name(uiBut *but, char *str, size_t maxlen)
2025 {
2026         if (ui_but_is_unit(but)) {
2027                 UnitSettings *unit = but->block->unit;
2028                 int unit_type = UI_but_unit_type_get(but);
2029                 char *orig_str;
2030                 
2031                 orig_str = BLI_strdup(str);
2032                 
2033                 bUnit_ToUnitAltName(str, maxlen, orig_str, unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type));
2034                 
2035                 MEM_freeN(orig_str);
2036         }
2037 }
2038
2039 /**
2040  * \param float_precision  Override the button precision.
2041  */
2042 static void ui_get_but_string_unit(uiBut *but, char *str, int len_max, double value, bool pad, int float_precision)
2043 {
2044         UnitSettings *unit = but->block->unit;
2045         const bool do_split = (unit->flag & USER_UNIT_OPT_SPLIT) != 0;
2046         int unit_type = UI_but_unit_type_get(but);
2047         int precision;
2048
2049         if (unit->scale_length < 0.0001f) unit->scale_length = 1.0f;  // XXX do_versions
2050
2051         /* Use precision override? */
2052         if (float_precision == -1) {
2053                 /* Sanity checks */
2054                 precision = (int)but->a2;
2055                 if      (precision > UI_PRECISION_FLOAT_MAX) precision = UI_PRECISION_FLOAT_MAX;
2056                 else if (precision == -1)                    precision = 2;
2057         }
2058         else {
2059                 precision = float_precision;
2060         }
2061
2062         bUnit_AsString(str, len_max, ui_get_but_scale_unit(but, value), precision,
2063                        unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type), do_split, pad);
2064 }
2065
2066 static float ui_get_but_step_unit(uiBut *but, float step_default)
2067 {
2068         int unit_type = RNA_SUBTYPE_UNIT_VALUE(UI_but_unit_type_get(but));
2069         double step;
2070
2071         step = bUnit_ClosestScalar(ui_get_but_scale_unit(but, step_default), but->block->unit->system, unit_type);
2072
2073         /* -1 is an error value */
2074         if (step != -1.0) {
2075                 BLI_assert(step > 0.0);
2076                 return (float)(step / ui_get_but_scale_unit(but, 1.0)) * 100.0f;
2077         }
2078         else {
2079                 return step_default;
2080         }
2081 }
2082
2083 /**
2084  * \param float_precision  For number buttons the precision to use or -1 to fallback to the button default.
2085  */
2086 void ui_but_string_get_ex(uiBut *but, char *str, const size_t maxlen, const int float_precision)
2087 {
2088         if (but->rnaprop && ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU)) {
2089                 PropertyType type;
2090                 const char *buf = NULL;
2091                 int buf_len;
2092
2093                 type = RNA_property_type(but->rnaprop);
2094
2095                 if (type == PROP_STRING) {
2096                         /* RNA string */
2097                         buf = RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, str, maxlen, &buf_len);
2098                 }
2099                 else if (type == PROP_ENUM) {
2100                         /* RNA enum */
2101                         int value = RNA_property_enum_get(&but->rnapoin, but->rnaprop);
2102                         if (RNA_property_enum_name(but->block->evil_C, &but->rnapoin, but->rnaprop, value, &buf)) {
2103                                 BLI_strncpy(str, buf, maxlen);
2104                                 buf = str;
2105                         }
2106                 }
2107                 else if (type == PROP_POINTER) {
2108                         /* RNA pointer */
2109                         PointerRNA ptr = RNA_property_pointer_get(&but->rnapoin, but->rnaprop);
2110                         buf = RNA_struct_name_get_alloc(&ptr, str, maxlen, &buf_len);
2111                 }
2112                 else {
2113                         BLI_assert(0);
2114                 }
2115
2116                 if (!buf) {
2117                         str[0] = '\0';
2118                 }
2119                 else if (buf && buf != str) {
2120                         /* string was too long, we have to truncate */
2121                         memcpy(str, buf, MIN2(maxlen, (size_t)(buf_len + 1)));
2122                         MEM_freeN((void *)buf);
2123                 }
2124         }
2125         else if (but->type == UI_BTYPE_TEXT) {
2126                 /* string */
2127                 BLI_strncpy(str, but->poin, maxlen);
2128                 return;
2129         }
2130         else if (but->type == UI_BTYPE_SEARCH_MENU) {
2131                 /* string */
2132                 BLI_strncpy(str, but->poin, maxlen);
2133                 return;
2134         }
2135         else if (ui_but_anim_expression_get(but, str, maxlen)) {
2136                 /* driver expression */
2137         }
2138         else {
2139                 /* number editing */
2140                 double value;
2141
2142                 value = ui_but_value_get(but);
2143
2144                 if (ui_but_is_float(but)) {
2145                         if (ui_but_is_unit(but)) {
2146                                 ui_get_but_string_unit(but, str, maxlen, value, false, float_precision);
2147                         }
2148                         else {
2149                                 const int prec = (float_precision == -1) ? ui_but_calc_float_precision(but, value) : float_precision;
2150                                 BLI_snprintf(str, maxlen, "%.*f", prec, value);
2151                         }
2152                 }
2153                 else
2154                         BLI_snprintf(str, maxlen, "%d", (int)value);
2155         }
2156 }
2157 void ui_but_string_get(uiBut *but, char *str, const size_t maxlen)
2158 {
2159         ui_but_string_get_ex(but, str, maxlen, -1);
2160 }
2161
2162 #ifdef WITH_PYTHON
2163
2164 static bool ui_set_but_string_eval_num_unit(bContext *C, uiBut *but, const char *str, double *value)
2165 {
2166         char str_unit_convert[256];
2167         const int unit_type = UI_but_unit_type_get(but);
2168
2169         BLI_strncpy(str_unit_convert, str, sizeof(str_unit_convert));
2170
2171         /* ugly, use the draw string to get the value,
2172          * this could cause problems if it includes some text which resolves to a unit */
2173         bUnit_ReplaceString(str_unit_convert, sizeof(str_unit_convert), but->drawstr,
2174                             ui_get_but_scale_unit(but, 1.0), but->block->unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type));
2175
2176         return (BPY_button_exec(C, str_unit_convert, value, true) != -1);
2177 }
2178
2179 #endif /* WITH_PYTHON */
2180
2181
2182 bool ui_but_string_set_eval_num(bContext *C, uiBut *but, const char *str, double *value)
2183 {
2184         bool ok = false;
2185
2186 #ifdef WITH_PYTHON
2187
2188         if (str[0] != '\0') {
2189                 bool is_unit_but = (ui_but_is_float(but) && ui_but_is_unit(but));
2190                 /* only enable verbose if we won't run again with units */
2191                 if (BPY_button_exec(C, str, value, is_unit_but == false) != -1) {
2192                         /* if the value parsed ok without unit conversion this button may still need a unit multiplier */
2193                         if (is_unit_but) {
2194                                 char str_new[128];
2195
2196                                 BLI_snprintf(str_new, sizeof(str_new), "%f", *value);
2197                                 ok = ui_set_but_string_eval_num_unit(C, but, str_new, value);
2198                         }
2199                         else {
2200                                 ok = true; /* parse normal string via py (no unit conversion needed) */
2201                         }
2202                 }
2203                 else if (is_unit_but) {
2204                         /* parse failed, this is a unit but so run replacements and parse again */
2205                         ok = ui_set_but_string_eval_num_unit(C, but, str, value);
2206                 }
2207         }
2208
2209 #else /* WITH_PYTHON */
2210
2211         *value = atof(str);
2212         ok = true;
2213
2214         (void)C;
2215         (void)but;
2216
2217 #endif /* WITH_PYTHON */
2218
2219         return ok;
2220 }
2221
2222 /* just the assignment/free part */
2223 static void ui_but_string_set_internal(uiBut *but, const char *str, size_t str_len)
2224 {
2225         BLI_assert(str_len == strlen(str));
2226         BLI_assert(but->str == NULL);
2227         str_len += 1;
2228
2229         if (str_len > UI_MAX_NAME_STR) {
2230                 but->str = MEM_mallocN(str_len, "ui_def_but str");
2231         }
2232         else {
2233                 but->str = but->strdata;
2234         }
2235         memcpy(but->str, str, str_len);
2236 }
2237
2238 static void ui_but_string_free_internal(uiBut *but)
2239 {
2240         if (but->str) {
2241                 if (but->str != but->strdata) {
2242                         MEM_freeN(but->str);
2243                 }
2244                 /* must call 'ui_but_string_set_internal' after */
2245                 but->str = NULL;
2246         }
2247 }
2248
2249 bool ui_but_string_set(bContext *C, uiBut *but, const char *str)
2250 {
2251         if (but->rnaprop && ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU)) {
2252                 if (RNA_property_editable(&but->rnapoin, but->rnaprop)) {
2253                         PropertyType type;
2254
2255                         type = RNA_property_type(but->rnaprop);
2256
2257                         if (type == PROP_STRING) {
2258                                 /* RNA string */
2259                                 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
2260                                 return true;
2261                         }
2262                         else if (type == PROP_POINTER) {
2263                                 /* RNA pointer */
2264                                 PointerRNA ptr, rptr;
2265                                 PropertyRNA *prop;
2266
2267                                 if (str[0] == '\0') {
2268                                         RNA_property_pointer_set(&but->rnapoin, but->rnaprop, PointerRNA_NULL);
2269                                         return true;
2270                                 }
2271                                 else {
2272                                         ptr = but->rnasearchpoin;
2273                                         prop = but->rnasearchprop;
2274                                         
2275                                         if (prop && RNA_property_collection_lookup_string(&ptr, prop, str, &rptr))
2276                                                 RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr);
2277
2278                                         return true;
2279                                 }
2280
2281                                 return false;
2282                         }
2283                         else if (type == PROP_ENUM) {
2284                                 int value;
2285                                 if (RNA_property_enum_value(but->block->evil_C, &but->rnapoin, but->rnaprop, str, &value)) {
2286                                         RNA_property_enum_set(&but->rnapoin, but->rnaprop, value);
2287                                         return true;
2288                                 }
2289                                 return false;
2290                         }
2291                         else {
2292                                 BLI_assert(0);
2293                         }
2294                 }
2295         }
2296         else if (but->type == UI_BTYPE_TEXT) {
2297                 /* string */
2298                 if (ui_but_is_utf8(but)) BLI_strncpy_utf8(but->poin, str, but->hardmax);
2299                 else BLI_strncpy(but->poin, str, but->hardmax);
2300
2301                 return true;
2302         }
2303         else if (but->type == UI_BTYPE_SEARCH_MENU) {
2304                 /* string */
2305                 BLI_strncpy(but->poin, str, but->hardmax);
2306                 return true;
2307         }
2308         else if (ui_but_anim_expression_set(but, str)) {
2309                 /* driver expression */
2310                 return true;
2311         }
2312         else if (str[0] == '#') {
2313                 /* shortcut to create new driver expression (versus immediate Py-execution) */
2314                 return ui_but_anim_expression_create(but, str + 1);
2315         }
2316         else {
2317                 /* number editing */
2318                 double value;
2319
2320                 if (ui_but_string_set_eval_num(C, but, str, &value) == false) {
2321                         WM_report_banner_show(C);
2322                         return false;
2323                 }
2324
2325                 if (!ui_but_is_float(but)) value = (int)floor(value + 0.5);
2326
2327                 /* not that we use hard limits here */
2328                 if (value < (double)but->hardmin) value = but->hardmin;
2329                 if (value > (double)but->hardmax) value = but->hardmax;
2330
2331                 ui_but_value_set(but, value);
2332                 return true;
2333         }
2334
2335         return false;
2336 }
2337
2338 void ui_but_default_set(bContext *C, const bool all, const bool use_afterfunc)
2339 {
2340         wmOperatorType *ot = WM_operatortype_find("UI_OT_reset_default_button", true);
2341
2342         if (use_afterfunc) {
2343                 PointerRNA *ptr;
2344                 ptr = ui_handle_afterfunc_add_operator(ot, WM_OP_EXEC_DEFAULT, true);
2345                 RNA_boolean_set(ptr, "all", all);
2346         }
2347         else {
2348                 PointerRNA ptr;
2349                 WM_operator_properties_create_ptr(&ptr, ot);
2350                 RNA_boolean_set(&ptr, "all", all);
2351                 WM_operator_name_call_ptr(C, ot, WM_OP_EXEC_DEFAULT, &ptr);
2352                 WM_operator_properties_free(&ptr);
2353         }
2354 }
2355
2356 static double soft_range_round_up(double value, double max)
2357 {
2358         /* round up to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, ..
2359          * checking for 0.0 prevents floating point exceptions */
2360         double newmax = (value != 0.0) ? pow(10.0, ceil(log(value) / M_LN10)) : 0.0;
2361
2362         if (newmax * 0.2 >= max && newmax * 0.2 >= value)
2363                 return newmax * 0.2;
2364         else if (newmax * 0.5 >= max && newmax * 0.5 >= value)
2365                 return newmax * 0.5;
2366         else
2367                 return newmax;
2368 }
2369
2370 static double soft_range_round_down(double value, double max)
2371 {
2372         /* round down to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, ..
2373          * checking for 0.0 prevents floating point exceptions */
2374         double newmax = (value != 0.0) ? pow(10.0, floor(log(value) / M_LN10)) : 0.0;
2375
2376         if (newmax * 5.0 <= max && newmax * 5.0 <= value)
2377                 return newmax * 5.0;
2378         else if (newmax * 2.0 <= max && newmax * 2.0 <= value)
2379                 return newmax * 2.0;
2380         else
2381                 return newmax;
2382 }
2383
2384 /* note: this could be split up into functions which handle arrays and not */
2385 static void ui_set_but_soft_range(uiBut *but)
2386 {
2387         /* ideally we would not limit this but practically, its more than
2388          * enough worst case is very long vectors wont use a smart soft-range
2389          * which isn't so bad. */
2390
2391         if (but->rnaprop) {
2392                 const PropertyType type = RNA_property_type(but->rnaprop);
2393                 double softmin, softmax /*, step, precision*/;
2394                 double value_min;
2395                 double value_max;
2396
2397                 /* clamp button range to something reasonable in case
2398                  * we get -inf/inf from RNA properties */
2399                 if (type == PROP_INT) {
2400                         const bool is_array = RNA_property_array_check(but->rnaprop);
2401                         int imin, imax, istep;
2402
2403                         RNA_property_int_ui_range(&but->rnapoin, but->rnaprop, &imin, &imax, &istep);
2404                         softmin = (imin == INT_MIN) ? -1e4 : imin;
2405                         softmax = (imin == INT_MAX) ? 1e4 : imax;
2406                         /*step = istep;*/ /*UNUSED*/
2407                         /*precision = 1;*/ /*UNUSED*/
2408
2409                         if (is_array) {
2410                                 int value_range[2];
2411                                 RNA_property_int_get_array_range(&but->rnapoin, but->rnaprop, value_range);
2412                                 value_min = (double)value_range[0];
2413                                 value_max = (double)value_range[1];
2414                         }
2415                         else {
2416                                 value_min = value_max = (double)RNA_property_int_get(&but->rnapoin, but->rnaprop);
2417                         }
2418                 }
2419                 else if (type == PROP_FLOAT) {
2420                         const bool is_array = RNA_property_array_check(but->rnaprop);
2421                         float fmin, fmax, fstep, fprecision;
2422
2423                         RNA_property_float_ui_range(&but->rnapoin, but->rnaprop, &fmin, &fmax, &fstep, &fprecision);
2424                         softmin = (fmin == -FLT_MAX) ? (float)-1e4 : fmin;
2425                         softmax = (fmax == FLT_MAX) ? (float)1e4 : fmax;
2426                         /*step = fstep;*/ /*UNUSED*/
2427                         /*precision = fprecision;*/ /*UNUSED*/
2428
2429                         if (is_array) {
2430                                 float value_range[2];
2431                                 RNA_property_float_get_array_range(&but->rnapoin, but->rnaprop, value_range);
2432                                 value_min = (double)value_range[0];
2433                                 value_max = (double)value_range[1];
2434                         }
2435                         else {
2436                                 value_min = value_max = (double)RNA_property_float_get(&but->rnapoin, but->rnaprop);
2437                         }
2438                 }
2439                 else {
2440                         return;
2441                 }
2442
2443                 /* if the value goes out of the soft/max range, adapt the range */
2444                 if (value_min + 1e-10 < softmin) {
2445                         if (value_min < 0.0)
2446                                 softmin = -soft_range_round_up(-value_min, -softmin);
2447                         else
2448                                 softmin = soft_range_round_down(value_min, softmin);
2449
2450                         if (softmin < (double)but->hardmin)
2451                                 softmin = (double)but->hardmin;
2452                 }
2453                 if (value_max - 1e-10 > softmax) {
2454                         if (value_max < 0.0)
2455                                 softmax = -soft_range_round_down(-value_max, -softmax);
2456                         else
2457                                 softmax = soft_range_round_up(value_max, softmax);
2458
2459                         if (softmax > (double)but->hardmax)
2460                                 softmax = but->hardmax;
2461                 }
2462
2463                 but->softmin = softmin;
2464                 but->softmax = softmax;
2465         }
2466         else if (but->poin && (but->pointype & UI_BUT_POIN_TYPES)) {
2467                 float value = ui_but_value_get(but);
2468                 CLAMP(value, but->hardmin, but->hardmax);
2469                 but->softmin = min_ff(but->softmin, value);
2470                 but->softmax = max_ff(but->softmax, value);
2471         }
2472         else {
2473                 BLI_assert(0);
2474         }
2475 }
2476
2477 /* ******************* Free ********************/
2478
2479 static void ui_free_link(uiLink *link)
2480 {
2481         if (link) {
2482                 BLI_freelistN(&link->lines);
2483                 MEM_freeN(link);
2484         }
2485 }
2486
2487 /* can be called with C==NULL */
2488 static void ui_but_free(const bContext *C, uiBut *but)
2489 {
2490         if (but->opptr) {
2491                 WM_operator_properties_free(but->opptr);
2492                 MEM_freeN(but->opptr);
2493         }
2494
2495         if (but->func_argN) {
2496                 MEM_freeN(but->func_argN);
2497         }
2498
2499         if (but->tip_argN) {
2500                 MEM_freeN(but->tip_argN);
2501         }
2502
2503         if (but->active) {
2504                 /* XXX solve later, buttons should be free-able without context ideally,
2505                  * however they may have open tooltips or popup windows, which need to
2506                  * be closed using a context pointer */
2507                 if (C) {
2508                         ui_but_active_free(C, but);
2509                 }
2510                 else {
2511                         if (but->active) {
2512                                 MEM_freeN(but->active);
2513                         }
2514                 }
2515         }
2516         if (but->str && but->str != but->strdata) {
2517                 MEM_freeN(but->str);
2518         }
2519         ui_free_link(but->link);
2520
2521         if ((but->type == UI_BTYPE_IMAGE) && but->poin) {
2522                 IMB_freeImBuf((struct ImBuf *)but->poin);
2523         }
2524
2525         BLI_assert(UI_butstore_is_registered(but->block, but) == false);
2526
2527         MEM_freeN(but);
2528 }
2529
2530 /* can be called with C==NULL */
2531 void UI_block_free(const bContext *C, uiBlock *block)
2532 {
2533         uiBut *but;
2534
2535         UI_butstore_clear(block);
2536
2537         while ((but = BLI_pophead(&block->buttons))) {
2538                 ui_but_free(C, but);
2539         }
2540
2541         if (block->unit) {
2542                 MEM_freeN(block->unit);
2543         }
2544
2545         if (block->func_argN) {
2546                 MEM_freeN(block->func_argN);
2547         }
2548
2549         CTX_store_free_list(&block->contexts);
2550
2551         BLI_freelistN(&block->saferct);
2552         BLI_freelistN(&block->color_pickers.list);
2553         
2554         MEM_freeN(block);
2555 }
2556
2557 /* can be called with C==NULL */
2558 void UI_blocklist_free(const bContext *C, ListBase *lb)
2559 {
2560         uiBlock *block;
2561         
2562         while ((block = BLI_pophead(lb))) {
2563                 UI_block_free(C, block);
2564         }
2565 }
2566
2567 void UI_blocklist_free_inactive(const bContext *C, ListBase *lb)
2568 {
2569         uiBlock *block, *nextblock;
2570
2571         for (block = lb->first; block; block = nextblock) {
2572                 nextblock = block->next;
2573         
2574                 if (!block->handle) {
2575                         if (!block->active) {
2576                                 BLI_remlink(lb, block);
2577                                 UI_block_free(C, block);
2578                         }
2579                         else
2580                                 block->active = 0;
2581                 }
2582         }
2583 }
2584
2585 void UI_block_region_set(uiBlock *block, ARegion *region)
2586 {
2587         ListBase *lb = &region->uiblocks;
2588         uiBlock *oldblock = NULL;
2589
2590         /* each listbase only has one block with this name, free block
2591          * if is already there so it can be rebuilt from scratch */
2592         if (lb) {
2593                 oldblock = BLI_findstring(lb, block->name, offsetof(uiBlock, name));
2594
2595                 if (oldblock) {
2596                         oldblock->active = 0;
2597                         oldblock->panel = NULL;
2598                         oldblock->handle = NULL;
2599                 }
2600
2601                 /* at the beginning of the list! for dynamical menus/blocks */
2602                 BLI_addhead(lb, block);
2603         }
2604
2605         block->oldblock = oldblock;
2606 }
2607
2608 uiBlock *UI_block_begin(const bContext *C, ARegion *region, const char *name, short dt)
2609 {
2610         uiBlock *block;
2611         wmWindow *window;
2612         Scene *scn;
2613         int getsizex, getsizey;
2614
2615         window = CTX_wm_window(C);
2616         scn = CTX_data_scene(C);
2617
2618         block = MEM_callocN(sizeof(uiBlock), "uiBlock");
2619         block->active = 1;
2620         block->dt = dt;
2621         block->evil_C = (void *)C;  /* XXX */
2622
2623         if (scn) {
2624                 block->color_profile = true;
2625
2626                 /* store display device name, don't lookup for transformations yet
2627                  * block could be used for non-color displays where looking up for transformation
2628                  * would slow down redraw, so only lookup for actual transform when it's indeed
2629                  * needed
2630                  */
2631                 BLI_strncpy(block->display_device, scn->display_settings.display_device, sizeof(block->display_device));
2632
2633                 /* copy to avoid crash when scene gets deleted with ui still open */
2634                 block->unit = MEM_mallocN(sizeof(scn->unit), "UI UnitSettings");
2635                 memcpy(block->unit, &scn->unit, sizeof(scn->unit));
2636         }
2637
2638         BLI_strncpy(block->name, name, sizeof(block->name));
2639
2640         if (region)
2641                 UI_block_region_set(block, region);
2642
2643         /* window matrix and aspect */
2644         if (region && region->swinid) {
2645                 wm_subwindow_matrix_get(window, region->swinid, block->winmat);
2646                 wm_subwindow_size_get(window, region->swinid, &getsizex, &getsizey);
2647
2648                 block->aspect = 2.0f / fabsf(getsizex * block->winmat[0][0]);
2649         }
2650         else {
2651                 /* no subwindow created yet, for menus for example, so we
2652                  * use the main window instead, since buttons are created
2653                  * there anyway */
2654                 wm_subwindow_matrix_get(window, window->screen->mainwin, block->winmat);
2655                 wm_subwindow_size_get(window, window->screen->mainwin, &getsizex, &getsizey);
2656
2657                 block->aspect = 2.0f / fabsf(getsizex * block->winmat[0][0]);
2658                 block->auto_open = true;
2659                 block->flag |= UI_BLOCK_LOOP; /* tag as menu */
2660         }
2661
2662         return block;
2663 }
2664
2665 uiBlock *UI_block_find_in_region(const char *name, ARegion *ar)
2666 {
2667         return BLI_findstring(&ar->uiblocks, name, offsetof(uiBlock, name));
2668 }
2669
2670 void UI_block_emboss_set(uiBlock *block, char dt)
2671 {
2672         block->dt = dt;
2673 }
2674
2675 void ui_but_update(uiBut *but)
2676 {
2677         /* if something changed in the button */
2678         double value = UI_BUT_VALUE_UNSET;
2679 //      float okwidth; // UNUSED
2680         
2681         ui_but_update_select_flag(but, &value);
2682         
2683         /* only update soft range while not editing */
2684         if (!(but->editval || but->editstr || but->editvec)) {
2685                 if ((but->rnaprop != NULL) ||
2686                     (but->poin && (but->pointype & UI_BUT_POIN_TYPES)))
2687                 {
2688                         ui_set_but_soft_range(but);
2689                 }
2690         }
2691
2692         /* test for min and max, icon sliders, etc */
2693         switch (but->type) {
2694                 case UI_BTYPE_NUM:
2695                 case UI_BTYPE_SCROLL:
2696                 case UI_BTYPE_NUM_SLIDER:
2697                         UI_GET_BUT_VALUE_INIT(but, value);
2698                         if      (value < (double)but->hardmin) ui_but_value_set(but, but->hardmin);
2699                         else if (value > (double)but->hardmax) ui_but_value_set(but, but->hardmax);
2700
2701                         /* max must never be smaller than min! Both being equal is allowed though */
2702                         BLI_assert(but->softmin <= but->softmax &&
2703                                    but->hardmin <= but->hardmax);
2704                         break;
2705                         
2706                 case UI_BTYPE_ICON_TOGGLE:
2707                 case UI_BTYPE_ICON_TOGGLE_N:
2708                         if (!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
2709                                 if (but->flag & UI_SELECT) but->iconadd = 1;
2710                                 else but->iconadd = 0;
2711                         }
2712                         break;
2713                         
2714                         /* quiet warnings for unhandled types */
2715                 default:
2716                         break;
2717         }
2718         
2719         
2720         /* safety is 4 to enable small number buttons (like 'users') */
2721         // okwidth = -4 + (BLI_rcti_size_x(&but->rect)); // UNUSED
2722         
2723         /* name: */
2724         switch (but->type) {
2725
2726                 case UI_BTYPE_MENU:
2727                         if (BLI_rctf_size_x(&but->rect) > 24.0f) {
2728                                 /* only needed for menus in popup blocks that don't recreate buttons on redraw */
2729                                 if (but->block->flag & UI_BLOCK_LOOP) {
2730                                         if (but->rnaprop && (RNA_property_type(but->rnaprop) == PROP_ENUM)) {
2731                                                 int value = RNA_property_enum_get(&but->rnapoin, but->rnaprop);
2732                                                 const char *buf;
2733                                                 if (RNA_property_enum_name_gettexted(but->block->evil_C,
2734                                                                                      &but->rnapoin, but->rnaprop, value, &buf))
2735                                                 {
2736                                                         size_t slen = strlen(buf);
2737                                                         ui_but_string_free_internal(but);
2738                                                         ui_but_string_set_internal(but, buf, slen);
2739                                                 }
2740                                         }
2741                                 }
2742                                 BLI_strncpy(but->drawstr, but->str, sizeof(but->drawstr));
2743                         }
2744                         break;
2745
2746                 case UI_BTYPE_NUM:
2747                 case UI_BTYPE_NUM_SLIDER:
2748
2749                         if (!but->editstr) {
2750                                 const char *drawstr_suffix = NULL;
2751                                 size_t slen;
2752
2753                                 UI_GET_BUT_VALUE_INIT(but, value);
2754
2755                                 slen = BLI_strncpy_rlen(but->drawstr, but->str, sizeof(but->drawstr));
2756
2757                                 if (ui_but_is_float(but)) {
2758                                         if (value == (double) FLT_MAX) {
2759                                                 slen += BLI_strncpy_rlen(but->drawstr + slen, "inf", sizeof(but->drawstr) - slen);
2760                                         }
2761                                         else if (value == (double) -FLT_MAX) {
2762                                                 slen += BLI_strncpy_rlen(but->drawstr + slen, "-inf", sizeof(but->drawstr) - slen);
2763                                         }
2764                                         /* support length type buttons */
2765                                         else if (ui_but_is_unit(but)) {
2766                                                 char new_str[sizeof(but->drawstr)];
2767                                                 ui_get_but_string_unit(but, new_str, sizeof(new_str), value, true, -1);
2768                                                 slen += BLI_strncpy_rlen(but->drawstr + slen, new_str, sizeof(but->drawstr) - slen);
2769                                         }
2770                                         else {
2771                                                 const int prec = ui_but_calc_float_precision(but, value);
2772                                                 slen += BLI_snprintf_rlen(but->drawstr + slen, sizeof(but->drawstr) - slen, "%.*f", prec, value);
2773                                         }
2774                                 }
2775                                 else {
2776                                         slen += BLI_snprintf_rlen(but->drawstr + slen, sizeof(but->drawstr) - slen, "%d", (int)value);
2777                                 }
2778
2779                                 if (but->rnaprop) {
2780                                         PropertySubType pstype = RNA_property_subtype(but->rnaprop);
2781
2782                                         if (pstype == PROP_PERCENTAGE) {
2783                                                 drawstr_suffix = "%";
2784                                         }
2785                                         else if (pstype == PROP_PIXEL) {
2786                                                 drawstr_suffix = " px";
2787                                         }
2788                                 }
2789
2790                                 if (drawstr_suffix) {
2791                                         BLI_strncpy(but->drawstr + slen, drawstr_suffix, sizeof(but->drawstr) - slen);
2792                                 }
2793
2794                         }
2795                         break;
2796
2797                 case UI_BTYPE_LABEL:
2798                         if (ui_but_is_float(but)) {
2799                                 int prec;
2800                                 UI_GET_BUT_VALUE_INIT(but, value);
2801                                 prec = ui_but_calc_float_precision(but, value);
2802                                 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%.*f", but->str, prec, value);
2803                         }
2804                         else {
2805                                 BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2806                         }
2807                 
2808                         break;
2809
2810                 case UI_BTYPE_TEXT:
2811                 case UI_BTYPE_SEARCH_MENU:
2812                         if (!but->editstr) {
2813                                 char str[UI_MAX_DRAW_STR];
2814
2815                                 ui_but_string_get(but, str, UI_MAX_DRAW_STR);
2816                                 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%s", but->str, str);
2817                         }
2818                         break;
2819         
2820                 case UI_BTYPE_KEY_EVENT:
2821                 {
2822                         const char *str;
2823                         if (but->flag & UI_SELECT) {
2824                                 str = "Press a key";
2825                         }
2826                         else {
2827                                 UI_GET_BUT_VALUE_INIT(but, value);
2828                                 str = WM_key_event_string((short)value);
2829                         }
2830                         BLI_snprintf(but->drawstr, UI_MAX_DRAW_STR, "%s%s", but->str, str);
2831                         break;
2832                 }
2833                 case UI_BTYPE_HOTKEY_EVENT:
2834                         if (but->flag & UI_SELECT) {
2835
2836                                 if (but->modifier_key) {
2837                                         char *str = but->drawstr;
2838                                         but->drawstr[0] = '\0';
2839
2840                                         if (but->modifier_key & KM_SHIFT)
2841                                                 str += BLI_strcpy_rlen(str, "Shift ");
2842                                         if (but->modifier_key & KM_CTRL)
2843                                                 str += BLI_strcpy_rlen(str, "Ctrl ");
2844                                         if (but->modifier_key & KM_ALT)
2845                                                 str += BLI_strcpy_rlen(str, "Alt ");
2846                                         if (but->modifier_key & KM_OSKEY)
2847                                                 str += BLI_strcpy_rlen(str, "Cmd ");
2848
2849                                         (void)str; /* UNUSED */
2850                                 }
2851                                 else {
2852                                         BLI_strncpy(but->drawstr, "Press a key", UI_MAX_DRAW_STR);
2853                                 }
2854                         }
2855                         else
2856                                 BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2857
2858                         break;
2859
2860                 case UI_BTYPE_HSVCUBE:
2861                 case UI_BTYPE_HSVCIRCLE:
2862                         break;
2863                 default:
2864                         BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2865                         break;
2866                 
2867         }
2868
2869         /* if we are doing text editing, this will override the drawstr */
2870         if (but->editstr)
2871                 but->drawstr[0] = '\0';
2872         
2873         /* text clipping moved to widget drawing code itself */
2874 }
2875
2876
2877 void UI_block_align_begin(uiBlock *block)
2878 {
2879         /* if other align was active, end it */
2880         if (block->flag & UI_BUT_ALIGN) UI_block_align_end(block);
2881
2882         block->flag |= UI_BUT_ALIGN_DOWN;
2883         block->alignnr++;
2884
2885         /* buttons declared after this call will get this align nr */ // XXX flag?
2886 }
2887
2888 static bool buts_are_horiz(uiBut *but1, uiBut *but2)
2889 {
2890         float dx, dy;
2891
2892         /* simple case which can fail if buttons shift apart
2893          * with proportional layouts, see: [#38602] */
2894         if ((but1->rect.ymin == but2->rect.ymin) &&
2895             (but1->rect.xmin != but2->rect.xmin))
2896         {
2897                 return true;
2898         }
2899
2900         dx = fabsf(but1->rect.xmax - but2->rect.xmin);
2901         dy = fabsf(but1->rect.ymin - but2->rect.ymax);
2902         
2903         return (dx <= dy);
2904 }
2905
2906 void UI_block_align_end(uiBlock *block)
2907 {
2908         block->flag &= ~UI_BUT_ALIGN;  /* all 4 flags */
2909 }
2910
2911 bool ui_but_can_align(uiBut *but)
2912 {
2913         return !ELEM(but->type, UI_BTYPE_LABEL, UI_BTYPE_CHECKBOX, UI_BTYPE_CHECKBOX_N, UI_BTYPE_SEPR, UI_BTYPE_SEPR_LINE);
2914 }
2915
2916 static void ui_block_align_calc_but(uiBut *first, short nr)
2917 {
2918         uiBut *prev, *but = NULL, *next;
2919         int flag = 0, cols = 0, rows = 0;
2920         
2921         /* auto align */
2922
2923         for (but = first; but && but->alignnr == nr; but = but->next) {
2924                 if (but->next && but->next->alignnr == nr) {
2925                         if (buts_are_horiz(but, but->next)) cols++;
2926                         else rows++;
2927                 }
2928         }
2929
2930         /* rows == 0: 1 row, cols == 0: 1 column */
2931         
2932         /* note;  how it uses 'flag' in loop below (either set it, or OR it) is confusing */
2933         for (but = first, prev = NULL; but && but->alignnr == nr; prev = but, but = but->next) {
2934                 next = but->next;
2935                 if (next && next->alignnr != nr)
2936                         next = NULL;
2937
2938                 /* clear old flag */
2939                 but->drawflag &= ~UI_BUT_ALIGN;
2940                         
2941                 if (flag == 0) {  /* first case */
2942                         if (next) {
2943                                 if (buts_are_horiz(but, next)) {
2944                                         if (rows == 0)
2945                                                 flag = UI_BUT_ALIGN_RIGHT;
2946                                         else 
2947                                                 flag = UI_BUT_ALIGN_DOWN | UI_BUT_ALIGN_RIGHT;
2948                                 }
2949                                 else {
2950                                         flag = UI_BUT_ALIGN_DOWN;
2951                                 }
2952                         }
2953                 }
2954                 else if (next == NULL) {  /* last case */
2955                         if (prev) {
2956                                 if (buts_are_horiz(prev, but)) {
2957                                         if (rows == 0)
2958                                                 flag = UI_BUT_ALIGN_LEFT;
2959                                         else
2960                                                 flag = UI_BUT_ALIGN_TOP | UI_BUT_ALIGN_LEFT;
2961                                 }
2962                                 else {
2963                                         flag = UI_BUT_ALIGN_TOP;
2964                                 }
2965                         }
2966                 }
2967                 else if (buts_are_horiz(but, next)) {
2968                         /* check if this is already second row */
2969                         if (prev && buts_are_horiz(prev, but) == 0) {
2970                                 flag &= ~UI_BUT_ALIGN_LEFT;
2971                                 flag |= UI_BUT_ALIGN_TOP;
2972                                 /* exception case: bottom row */
2973                                 if (rows > 0) {
2974                                         uiBut *bt = but;
2975                                         while (bt && bt->alignnr == nr) {
2976                                                 if (bt->next && bt->next->alignnr == nr && buts_are_horiz(bt, bt->next) == 0) {
2977                                                         break;
2978                                                 }
2979                                                 bt = bt->next;
2980                                         }
2981                                         if (bt == NULL || bt->alignnr != nr) flag = UI_BUT_ALIGN_TOP | UI_BUT_ALIGN_RIGHT;
2982                                 }
2983                         }
2984                         else {
2985                                 flag |= UI_BUT_ALIGN_LEFT;
2986                         }
2987                 }
2988                 else {
2989                         if (cols == 0) {
2990                                 flag |= UI_BUT_ALIGN_TOP;
2991                         }
2992                         else {  /* next button switches to new row */
2993                                 
2994                                 if (prev && buts_are_horiz(prev, but))
2995                                         flag |= UI_BUT_ALIGN_LEFT;
2996                                 else {
2997                                         flag &= ~UI_BUT_ALIGN_LEFT;
2998                                         flag |= UI_BUT_ALIGN_TOP;
2999                                 }
3000                                 
3001                                 if ((flag & UI_BUT_ALIGN_TOP) == 0) {  /* stil top row */
3002                                         if (prev) {
3003                                                 if (next && buts_are_horiz(but, next))
3004                                                         flag = UI_BUT_ALIGN_DOWN | UI_BUT_ALIGN_LEFT | UI_BUT_ALIGN_RIGHT;
3005                                                 else {
3006                                                         /* last button in top row */
3007                                                         flag = UI_BUT_ALIGN_DOWN | UI_BUT_ALIGN_LEFT;
3008                                                 }
3009                                         }
3010                                         else 
3011                                                 flag |= UI_BUT_ALIGN_DOWN;
3012                                 }
3013                                 else 
3014                                         flag |= UI_BUT_ALIGN_TOP;
3015                         }
3016                 }
3017                 
3018                 but->drawflag |= flag;
3019                 
3020                 /* merge coordinates */
3021                 if (prev) {
3022                         /* simple cases */
3023                         if (rows == 0) {
3024                                 but->rect.xmin = (prev->rect.xmax + but->rect.xmin) / 2.0f;
3025                                 prev->rect.xmax = but->rect.xmin;
3026                         }
3027                         else if (cols == 0) {
3028                                 but->rect.ymax = (prev->rect.ymin + but->rect.ymax) / 2.0f;
3029                                 prev->rect.ymin = but->rect.ymax;
3030                         }
3031                         else {
3032                                 if (buts_are_horiz(prev, but)) {
3033                                         but->rect.xmin = (prev->rect.xmax + but->rect.xmin) / 2.0f;
3034                                         prev->rect.xmax = but->rect.xmin;
3035                                         /* copy height too */
3036                                         but->rect.ymax = prev->rect.ymax;
3037                                 }
3038                                 else if (prev->prev && buts_are_horiz(prev->prev, prev) == 0) {
3039                                         /* the previous button is a single one in its row */
3040                                         but->rect.ymax = (prev->rect.ymin + but->rect.ymax) / 2.0f;
3041                                         prev->rect.ymin = but->rect.ymax;
3042                                         
3043                                         but->rect.xmin = prev->rect.xmin;
3044                                         if (next && buts_are_horiz(but, next) == 0)
3045                                                 but->rect.xmax = prev->rect.xmax;
3046                                 }
3047                                 else {
3048                                         /* the previous button is not a single one in its row */
3049                                         but->rect.ymax = prev->rect.ymin;
3050                                 }
3051                         }
3052                 }
3053         }
3054 }
3055
3056 void ui_block_align_calc(uiBlock *block)
3057 {
3058         uiBut *but;
3059         short nr;
3060
3061         /* align buttons with same align nr */
3062         for (but = block->buttons.first; but; ) {
3063                 if (but->alignnr) {
3064                         nr = but->alignnr;
3065                         ui_block_align_calc_but(but, nr);
3066
3067                         /* skip with same number */
3068                         for (; but && but->alignnr == nr; but = but->next) {
3069                                 /* pass */
3070                         }
3071
3072                         if (!but) {
3073                                 break;
3074                         }
3075                 }
3076                 else {
3077                         but = but->next;
3078                 }
3079         }
3080 }
3081
3082 struct ColorManagedDisplay *ui_block_cm_display_get(uiBlock *block)
3083 {
3084         return IMB_colormanagement_display_get_named(block->display_device);
3085 }
3086
3087 void ui_block_cm_to_display_space_v3(uiBlock *block, float pixel[3])
3088 {
3089    &nbs