Cleanup: style, spelling
[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, buf_len, buf)) {
964                         found = true;
965                 }
966         }
967         else if ((mt = UI_but_menutype_get(but))) {
968                 IDProperty *prop_menu;
969                 IDProperty *prop_menu_name;
970
971                 /* annoying, create a property */
972                 IDPropertyTemplate val = {0};
973                 prop_menu = IDP_New(IDP_GROUP, &val, __func__); /* dummy, name is unimportant  */
974                 IDP_AddToGroup(prop_menu, (prop_menu_name = IDP_NewString("", "name", sizeof(mt->idname))));
975
976                 IDP_AssignString(prop_menu_name, mt->idname, sizeof(mt->idname));
977
978                 if (WM_key_event_operator_string(C, "WM_OT_call_menu", WM_OP_INVOKE_REGION_WIN, prop_menu,
979                                                  true, buf_len, buf))
980                 {
981                         found = true;
982                 }
983
984                 IDP_FreeProperty(prop_menu);
985                 MEM_freeN(prop_menu);
986         }
987
988         return found;
989 }
990
991 static bool ui_but_event_property_operator_string(const bContext *C, uiBut *but, char *buf, const size_t buf_len)
992 {       
993         /* context toggle operator names to check... */
994         const char *ctx_toggle_opnames[] = {
995                 "WM_OT_context_toggle",
996                 "WM_OT_context_toggle_enum",
997                 "WM_OT_context_cycle_int",
998                 "WM_OT_context_cycle_enum",
999                 "WM_OT_context_cycle_array",
1000                 "WM_OT_context_menu_enum",
1001                 NULL
1002         };              
1003         const size_t num_ops = sizeof(ctx_toggle_opnames) / sizeof(const char *);
1004         
1005         bool found = false;
1006         
1007         /* this version is only for finding hotkeys for properties (which get set via context using operators) */
1008         if (but->rnaprop) {
1009                 /* to avoid massive slowdowns on property panels, for now, we only check the 
1010                  * hotkeys for Editor / Scene settings...
1011                  *
1012                  * TODO: userpref settings?
1013                  */
1014                 // TODO: value (for enum stuff)?
1015                 char *data_path = NULL;
1016                 
1017                 if (but->rnapoin.id.data) {
1018                         ID *id = but->rnapoin.id.data;
1019                         
1020                         if (GS(id->name) == ID_SCR) {
1021                                 /* screen/editor property 
1022                                  * NOTE: in most cases, there is actually no info for backwards tracing 
1023                                  * how to get back to ID from the editor data we may be dealing with
1024                                  */
1025                                 if (RNA_struct_is_a(but->rnapoin.type, &RNA_Space)) {
1026                                         /* data should be directly on here... */
1027                                         data_path = BLI_sprintfN("space_data.%s", RNA_property_identifier(but->rnaprop));
1028                                 }
1029                                 else {
1030                                         /* special exceptions for common nested data in editors... */
1031                                         if (RNA_struct_is_a(but->rnapoin.type, &RNA_DopeSheet)) {
1032                                                 /* dopesheet filtering options... */
1033                                                 data_path = BLI_sprintfN("space_data.dopesheet.%s", RNA_property_identifier(but->rnaprop));
1034                                         }
1035                                 }
1036                         }
1037                         else if (GS(id->name) == ID_SCE) {
1038                                 if (RNA_struct_is_a(but->rnapoin.type, &RNA_ToolSettings)) {
1039                                         /* toolsettings property 
1040                                          * NOTE: toolsettings is usually accessed directly (i.e. not through scene)
1041                                          */
1042                                         data_path = RNA_path_from_ID_to_property(&but->rnapoin, but->rnaprop);
1043                                 }
1044                                 else {
1045                                         /* scene property */
1046                                         char *path = RNA_path_from_ID_to_property(&but->rnapoin, but->rnaprop);
1047                                         
1048                                         if (path) {
1049                                                 data_path = BLI_sprintfN("scene.%s", path);
1050                                                 MEM_freeN(path);
1051                                         }
1052 #if 0
1053                                         else {
1054                                                 printf("ERROR in %s(): Couldn't get path for scene property - %s\n",
1055                                                        __func__, RNA_property_identifier(but->rnaprop));
1056                                         }
1057 #endif
1058                                 }
1059                         }
1060                         else {
1061                                 //puts("other id");
1062                         }
1063                         
1064                         //printf("prop shortcut: '%s' (%s)\n", RNA_property_identifier(but->rnaprop), data_path);
1065                 }
1066                 
1067                 /* we have a datapath! */
1068                 if (data_path) {
1069                         size_t i;
1070                         
1071                         /* create a property to host the "datapath" property we're sending to the operators */
1072                         IDProperty *prop_path;
1073                         IDProperty *prop_path_value;
1074                         
1075                         IDPropertyTemplate val = {0};
1076                         prop_path = IDP_New(IDP_GROUP, &val, __func__);
1077                         prop_path_value = IDP_NewString(data_path, "data_path", strlen(data_path) + 1);
1078                         IDP_AddToGroup(prop_path, prop_path_value);
1079                         
1080                         /* check each until one works... */
1081                         for (i = 0; (i < num_ops) && (ctx_toggle_opnames[i]); i++) {
1082                                 //printf("\t%s\n", ctx_toggle_opnames[i]);
1083                                 if (WM_key_event_operator_string(C, ctx_toggle_opnames[i], WM_OP_INVOKE_REGION_WIN, prop_path, false,
1084                                                                  buf_len, buf))
1085                                 {
1086                                         found = true;
1087                                         break;
1088                                 }
1089                         }
1090                         
1091                         /* cleanup */
1092                         IDP_FreeProperty(prop_path);
1093                         MEM_freeN(prop_path);
1094                         MEM_freeN(data_path);
1095                 }
1096         }
1097         
1098         return found;
1099 }
1100
1101 /**
1102  * This goes in a seemingly weird pattern:
1103  *
1104  * <pre>
1105  *     4
1106  *  5     6
1107  * 1       2
1108  *  7     8
1109  *     3
1110  * </pre>
1111  *
1112  * but it's actually quite logical. It's designed to be 'upwards compatible'
1113  * for muscle memory so that the menu item locations are fixed and don't move
1114  * as new items are added to the menu later on. It also optimises efficiency -
1115  * a radial menu is best kept symmetrical, with as large an angle between
1116  * items as possible, so that the gestural mouse movements can be fast and inexact.
1117
1118  * It starts off with two opposite sides for the first two items
1119  * then joined by the one below for the third (this way, even with three items,
1120  * the menu seems to still be 'in order' reading left to right). Then the fourth is
1121  * added to complete the compass directions. From here, it's just a matter of
1122  * subdividing the rest of the angles for the last 4 items.
1123  *
1124  * --Matt 07/2006
1125  */
1126 const char ui_radial_dir_order[8] = {
1127     UI_RADIAL_W,  UI_RADIAL_E,  UI_RADIAL_S,  UI_RADIAL_N,
1128     UI_RADIAL_NW, UI_RADIAL_NE, UI_RADIAL_SW, UI_RADIAL_SE};
1129
1130 const char  ui_radial_dir_to_numpad[8] = {8, 9, 6, 3, 2, 1, 4, 7};
1131 const short ui_radial_dir_to_angle[8] =  {90, 45, 0, 315, 270, 225, 180, 135};
1132
1133 static void ui_but_pie_direction_string(uiBut *but, char *buf, int size)
1134 {
1135         BLI_assert(but->pie_dir < ARRAY_SIZE(ui_radial_dir_to_numpad));
1136         BLI_snprintf(buf, size, "%d", ui_radial_dir_to_numpad[but->pie_dir]);
1137 }
1138
1139 static void ui_menu_block_set_keymaps(const bContext *C, uiBlock *block)
1140 {
1141         uiBut *but;
1142         char buf[128];
1143
1144         /* only do it before bounding */
1145         if (block->rect.xmin != block->rect.xmax)
1146                 return;
1147
1148         if (block->flag & UI_BLOCK_RADIAL) {
1149                 for (but = block->buttons.first; but; but = but->next) {
1150                         if (but->pie_dir != UI_RADIAL_NONE) {
1151                                 ui_but_pie_direction_string(but, buf, sizeof(buf));
1152                                 ui_but_add_shortcut(but, buf, false);
1153                         }
1154                 }
1155         }
1156         else {
1157                 for (but = block->buttons.first; but; but = but->next) {
1158
1159                         if (ui_but_event_operator_string(C, but, buf, sizeof(buf))) {
1160                                 ui_but_add_shortcut(but, buf, false);
1161                         }
1162                         else if (ui_but_event_property_operator_string(C, but, buf, sizeof(buf))) {
1163                                 ui_but_add_shortcut(but, buf, false);
1164                         }
1165                 }
1166         }
1167 }
1168
1169 void UI_block_update_from_old(const bContext *C, uiBlock *block)
1170 {
1171         uiBut *but_old;
1172         uiBut *but;
1173
1174         if (!block->oldblock)
1175                 return;
1176
1177         but_old = block->oldblock->buttons.first;
1178
1179         if (BLI_listbase_is_empty(&block->oldblock->butstore) == false) {
1180                 UI_butstore_update(block);
1181         }
1182
1183         for (but = block->buttons.first; but; but = but->next) {
1184                 if (ui_but_update_from_old_block(C, block, &but, &but_old)) {
1185                         ui_but_update(but);
1186                 }
1187         }
1188
1189         block->auto_open = block->oldblock->auto_open;
1190         block->auto_open_last = block->oldblock->auto_open_last;
1191         block->tooltipdisabled = block->oldblock->tooltipdisabled;
1192         BLI_movelisttolist(&block->color_pickers.list, &block->oldblock->color_pickers.list);
1193
1194         block->oldblock = NULL;
1195 }
1196
1197 void UI_block_end_ex(const bContext *C, uiBlock *block, const int xy[2])
1198 {
1199         wmWindow *window = CTX_wm_window(C);
1200         Scene *scene = CTX_data_scene(C);
1201         uiBut *but;
1202
1203         BLI_assert(block->active);
1204
1205         UI_block_update_from_old(C, block);
1206
1207         /* inherit flags from 'old' buttons that was drawn here previous, based
1208          * on matching buttons, we need this to make button event handling non
1209          * blocking, while still allowing buttons to be remade each redraw as it
1210          * is expected by blender code */
1211         for (but = block->buttons.first; but; but = but->next) {
1212                 /* temp? Proper check for graying out */
1213                 if (but->optype) {
1214                         wmOperatorType *ot = but->optype;
1215
1216                         if (but->context)
1217                                 CTX_store_set((bContext *)C, but->context);
1218
1219                         if (ot == NULL || WM_operator_poll_context((bContext *)C, ot, but->opcontext) == 0) {
1220                                 but->flag |= UI_BUT_DISABLED;
1221                                 but->lock = true;
1222                         }
1223
1224                         if (but->context)
1225                                 CTX_store_set((bContext *)C, NULL);
1226                 }
1227
1228                 ui_but_anim_flag(but, (scene) ? scene->r.cfra : 0.0f);
1229         }
1230
1231
1232
1233         /* handle pending stuff */
1234         if (block->layouts.first) {
1235                 UI_block_layout_resolve(block, NULL, NULL);
1236         }
1237         ui_block_align_calc(block);
1238         if ((block->flag & UI_BLOCK_LOOP) && (block->flag & UI_BLOCK_NUMSELECT)) {
1239                 ui_menu_block_set_keyaccels(block); /* could use a different flag to check */
1240         }
1241
1242         if (block->flag & UI_BLOCK_LOOP) {
1243                 ui_menu_block_set_keymaps(C, block);
1244         }
1245         
1246         /* after keymaps! */
1247         switch (block->bounds_type) {
1248                 case UI_BLOCK_BOUNDS_NONE:
1249                         break;
1250                 case UI_BLOCK_BOUNDS:
1251                         ui_block_bounds_calc(block);
1252                         break;
1253                 case UI_BLOCK_BOUNDS_TEXT:
1254                         ui_block_bounds_calc_text(block, 0.0f);
1255                         break;
1256                 case UI_BLOCK_BOUNDS_POPUP_CENTER:
1257                         ui_block_bounds_calc_centered(window, block);
1258                         break;
1259                 case UI_BLOCK_BOUNDS_PIE_CENTER:
1260                         ui_block_bounds_calc_centered_pie(block);
1261                         break;
1262
1263                         /* fallback */
1264                 case UI_BLOCK_BOUNDS_POPUP_MOUSE:
1265                 case UI_BLOCK_BOUNDS_POPUP_MENU:
1266                         ui_block_bounds_calc_popup(window, block, block->bounds_type, xy);
1267                         break;
1268         }
1269
1270         if (block->rect.xmin == 0.0f && block->rect.xmax == 0.0f) {
1271                 UI_block_bounds_set_normal(block, 0);
1272         }
1273         if (block->flag & UI_BUT_ALIGN) {
1274                 UI_block_align_end(block);
1275         }
1276
1277         block->endblock = 1;
1278 }
1279
1280 void UI_block_end(const bContext *C, uiBlock *block)
1281 {
1282         wmWindow *window = CTX_wm_window(C);
1283
1284         UI_block_end_ex(C, block, &window->eventstate->x);
1285 }
1286
1287 /* ************** BLOCK DRAWING FUNCTION ************* */
1288
1289 void ui_fontscale(short *points, float aspect)
1290 {
1291         if (aspect < 0.9f || aspect > 1.1f) {
1292                 float pointsf = *points;
1293                 
1294                 /* for some reason scaling fonts goes too fast compared to widget size */
1295                 /* XXX not true anymore? (ton) */
1296                 //aspect = sqrt(aspect);
1297                 pointsf /= aspect;
1298                 
1299                 if (aspect > 1.0f)
1300                         *points = ceilf(pointsf);
1301                 else
1302                         *points = floorf(pointsf);
1303         }
1304 }
1305
1306 /* project button or block (but==NULL) to pixels in regionspace */
1307 static void ui_but_to_pixelrect(rcti *rect, const ARegion *ar, uiBlock *block, uiBut *but)
1308 {
1309         rctf rectf;
1310
1311         ui_block_to_window_rctf(ar, block, &rectf, (but) ? &but->rect : &block->rect);
1312
1313         rectf.xmin -= ar->winrct.xmin;
1314         rectf.ymin -= ar->winrct.ymin;
1315         rectf.xmax -= ar->winrct.xmin;
1316         rectf.ymax -= ar->winrct.ymin;
1317
1318         rect->xmin = floorf(rectf.xmin);
1319         rect->ymin = floorf(rectf.ymin);
1320         rect->xmax = floorf(rectf.xmax);
1321         rect->ymax = floorf(rectf.ymax);
1322 }
1323
1324 /* uses local copy of style, to scale things down, and allow widgets to change stuff */
1325 void UI_block_draw(const bContext *C, uiBlock *block)
1326 {
1327         uiStyle style = *UI_style_get_dpi();  /* XXX pass on as arg */
1328         ARegion *ar;
1329         uiBut *but;
1330         rcti rect;
1331         int multisample_enabled;
1332         
1333         /* get menu region or area region */
1334         ar = CTX_wm_menu(C);
1335         if (!ar)
1336                 ar = CTX_wm_region(C);
1337
1338         if (!block->endblock)
1339                 UI_block_end(C, block);
1340
1341         /* disable AA, makes widgets too blurry */
1342         multisample_enabled = glIsEnabled(GL_MULTISAMPLE_ARB);
1343         if (multisample_enabled)
1344                 glDisable(GL_MULTISAMPLE_ARB);
1345
1346         /* we set this only once */
1347         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1348         
1349         /* scale fonts */
1350         ui_fontscale(&style.paneltitle.points, block->aspect);
1351         ui_fontscale(&style.grouplabel.points, block->aspect);
1352         ui_fontscale(&style.widgetlabel.points, block->aspect);
1353         ui_fontscale(&style.widget.points, block->aspect);
1354         
1355         /* scale block min/max to rect */
1356         ui_but_to_pixelrect(&rect, ar, block, NULL);
1357         
1358         /* pixel space for AA widgets */
1359         glMatrixMode(GL_PROJECTION);
1360         glPushMatrix();
1361         glMatrixMode(GL_MODELVIEW);
1362         glPushMatrix();
1363         glLoadIdentity();
1364
1365         wmOrtho2_region_ui(ar);
1366         
1367         /* back */
1368         if (block->flag & UI_BLOCK_RADIAL)
1369                 ui_draw_pie_center(block);
1370         else if (block->flag & UI_BLOCK_LOOP)
1371                 ui_draw_menu_back(&style, block, &rect);
1372         else if (block->panel)
1373                 ui_draw_aligned_panel(&style, block, &rect, UI_panel_category_is_visible(ar));
1374
1375         /* widgets */
1376         for (but = block->buttons.first; but; but = but->next) {
1377                 if (!(but->flag & (UI_HIDDEN | UI_SCROLLED))) {
1378                         ui_but_to_pixelrect(&rect, ar, block, but);
1379                 
1380                         /* XXX: figure out why invalid coordinates happen when closing render window */
1381                         /* and material preview is redrawn in main window (temp fix for bug #23848) */
1382                         if (rect.xmin < rect.xmax && rect.ymin < rect.ymax)
1383                                 ui_draw_but(C, ar, &style, but, &rect);
1384                 }
1385         }
1386         
1387         /* restore matrix */
1388         glMatrixMode(GL_PROJECTION);
1389         glPopMatrix();
1390         glMatrixMode(GL_MODELVIEW);
1391         glPopMatrix();
1392
1393         if (multisample_enabled)
1394                 glEnable(GL_MULTISAMPLE_ARB);
1395         
1396         ui_draw_links(block);
1397 }
1398
1399 /* ************* EVENTS ************* */
1400
1401 /**
1402  * Check if the button is pushed, this is only meaningful for some button types.
1403  *
1404  * \return (0 == UNSELECT), (1 == SELECT), (-1 == DO-NOTHING)
1405  */
1406 int ui_but_is_pushed_ex(uiBut *but, double *value)
1407 {
1408         int is_push = 0;
1409
1410         if (but->bit) {
1411                 const bool state = ELEM(but->type, UI_BTYPE_TOGGLE_N, UI_BTYPE_ICON_TOGGLE_N, UI_BTYPE_CHECKBOX_N) ? false : true;
1412                 int lvalue;
1413                 UI_GET_BUT_VALUE_INIT(but, *value);
1414                 lvalue = (int)*value;
1415                 if (UI_BITBUT_TEST(lvalue, (but->bitnr))) {
1416                         is_push = state;
1417                 }
1418                 else {
1419                         is_push = !state;
1420                 }
1421         }
1422         else {
1423                 switch (but->type) {
1424                         case UI_BTYPE_BUT:
1425                         case UI_BTYPE_HOTKEY_EVENT:
1426                         case UI_BTYPE_KEY_EVENT:
1427                         case UI_BTYPE_COLOR:
1428                                 is_push = -1;
1429                                 break;
1430                         case UI_BTYPE_BUT_TOGGLE:
1431                         case UI_BTYPE_TOGGLE:
1432                         case UI_BTYPE_ICON_TOGGLE:
1433                         case UI_BTYPE_CHECKBOX:
1434                                 UI_GET_BUT_VALUE_INIT(but, *value);
1435                                 if (*value != (double)but->hardmin) is_push = true;
1436                                 break;
1437                         case UI_BTYPE_ICON_TOGGLE_N:
1438                         case UI_BTYPE_TOGGLE_N:
1439                         case UI_BTYPE_CHECKBOX_N:
1440                                 UI_GET_BUT_VALUE_INIT(but, *value);
1441                                 if (*value == 0.0) is_push = true;
1442                                 break;
1443                         case UI_BTYPE_ROW:
1444                         case UI_BTYPE_LISTROW:
1445                                 UI_GET_BUT_VALUE_INIT(but, *value);
1446                                 /* support for rna enum buts */
1447                                 if (but->rnaprop && (RNA_property_flag(but->rnaprop) & PROP_ENUM_FLAG)) {
1448                                         if ((int)*value & (int)but->hardmax) is_push = true;
1449                                 }
1450                                 else {
1451                                         if (*value == (double)but->hardmax) is_push = true;
1452                                 }
1453                                 break;
1454                         default:
1455                                 is_push = -1;
1456                                 break;
1457                 }
1458         }
1459
1460         return is_push;
1461 }
1462 int ui_but_is_pushed(uiBut *but)
1463 {
1464         double value = UI_BUT_VALUE_UNSET;
1465         return ui_but_is_pushed_ex(but, &value);
1466 }
1467
1468 static void ui_but_update_select_flag(uiBut *but, double *value)
1469 {
1470         switch (ui_but_is_pushed_ex(but, value)) {
1471                 case true:
1472                         but->flag |= UI_SELECT;
1473                         break;
1474                 case false:
1475                         but->flag &= ~UI_SELECT;
1476                         break;
1477         }
1478 }
1479
1480 static uiBut *ui_linkline_find_inlink(uiBlock *block, void *poin)
1481 {
1482         uiBut *but;
1483         
1484         but = block->buttons.first;
1485         while (but) {
1486                 if (but->type == UI_BTYPE_INLINK) {
1487                         if (but->poin == poin) return but;
1488                 }
1489                 but = but->next;
1490         }
1491         return NULL;
1492 }
1493
1494 static void ui_linkline_add(ListBase *listb, uiBut *but, uiBut *bt, short deactive)
1495 {
1496         uiLinkLine *line;
1497         
1498         line = MEM_callocN(sizeof(uiLinkLine), "linkline");
1499         BLI_addtail(listb, line);
1500         line->from = but;
1501         line->to = bt;
1502         line->deactive = deactive;
1503 }
1504
1505 uiBut *UI_block_links_find_inlink(uiBlock *block, void *poin)
1506 {
1507         return ui_linkline_find_inlink(block, poin);
1508 }
1509
1510 void UI_block_links_compose(uiBlock *block)
1511 {
1512         uiBut *but, *bt;
1513         uiLink *link;
1514         void ***ppoin;
1515         int a;
1516         
1517         but = block->buttons.first;
1518         while (but) {
1519                 if (but->type == UI_BTYPE_LINK) {
1520                         link = but->link;
1521                         
1522                         /* for all pointers in the array */
1523                         if (link) {
1524                                 if (link->ppoin) {
1525                                         ppoin = link->ppoin;
1526                                         for (a = 0; a < *(link->totlink); a++) {
1527                                                 bt = ui_linkline_find_inlink(block, (*ppoin)[a]);
1528                                                 if (bt) {
1529                                                         if ((but->flag & UI_BUT_SCA_LINK_GREY) || (bt->flag & UI_BUT_SCA_LINK_GREY)) {
1530                                                                 ui_linkline_add(&link->lines, but, bt, true);
1531                                                         }
1532                                                         else {
1533                                                                 ui_linkline_add(&link->lines, but, bt, false);
1534                                                         }
1535
1536                                                 }
1537                                         }
1538                                 }
1539                                 else if (link->poin) {
1540                                         bt = ui_linkline_find_inlink(block, *link->poin);
1541                                         if (bt) {
1542                                                 if ((but->flag & UI_BUT_SCA_LINK_GREY) || (bt->flag & UI_BUT_SCA_LINK_GREY)) {
1543                                                         ui_linkline_add(&link->lines, but, bt, true);
1544                                                 }
1545                                                 else {
1546                                                         ui_linkline_add(&link->lines, but, bt, false);
1547                                                 }
1548                                         }
1549                                 }
1550                         }
1551                 }
1552                 but = but->next;
1553         }
1554 }
1555
1556
1557 /* ************************************************ */
1558
1559 void UI_block_lock_set(uiBlock *block, bool val, const char *lockstr)
1560 {
1561         if (val) {
1562                 block->lock = val;
1563                 block->lockstr = lockstr;
1564         }
1565 }
1566
1567 void UI_block_lock_clear(uiBlock *block)
1568 {
1569         block->lock = false;
1570         block->lockstr = NULL;
1571 }
1572
1573 /* *************************************************************** */
1574
1575 void ui_linkline_remove(uiLinkLine *line, uiBut *but)
1576 {
1577         uiLink *link;
1578         int a, b;
1579         
1580         BLI_remlink(&but->link->lines, line);
1581
1582         link = line->from->link;
1583
1584         /* are there more pointers allowed? */
1585         if (link->ppoin) {
1586                 
1587                 if (*(link->totlink) == 1) {
1588                         *(link->totlink) = 0;
1589                         MEM_freeN(*(link->ppoin));
1590                         *(link->ppoin) = NULL;
1591                 }
1592                 else {
1593                         b = 0;
1594                         for (a = 0; a < (*(link->totlink)); a++) {
1595                                 if ((*(link->ppoin))[a] != line->to->poin) {
1596                                         (*(link->ppoin))[b] = (*(link->ppoin))[a];
1597                                         b++;
1598                                 }
1599                         }
1600                         (*(link->totlink))--;
1601                 }
1602         }
1603         else {
1604                 *(link->poin) = NULL;
1605         }
1606
1607         MEM_freeN(line);
1608         //REDRAW
1609 }
1610
1611 /* *********************** data get/set ***********************
1612  * this either works with the pointed to data, or can work with
1613  * an edit override pointer while dragging for example */
1614
1615 /* for buttons pointing to color for example */
1616 void ui_but_v3_get(uiBut *but, float vec[3])
1617 {
1618         PropertyRNA *prop;
1619         int a;
1620
1621         if (but->editvec) {
1622                 copy_v3_v3(vec, but->editvec);
1623         }
1624
1625         if (but->rnaprop) {
1626                 prop = but->rnaprop;
1627
1628                 zero_v3(vec);
1629
1630                 if (RNA_property_type(prop) == PROP_FLOAT) {
1631                         int tot = RNA_property_array_length(&but->rnapoin, prop);
1632                         BLI_assert(tot > 0);
1633                         if (tot == 3) {
1634                                 RNA_property_float_get_array(&but->rnapoin, prop, vec);
1635                         }
1636                         else {
1637                                 tot = min_ii(tot, 3);
1638                                 for (a = 0; a < tot; a++) {
1639                                         vec[a] = RNA_property_float_get_index(&but->rnapoin, prop, a);
1640                                 }
1641                         }
1642                 }
1643         }
1644         else if (but->pointype == UI_BUT_POIN_CHAR) {
1645                 const char *cp = (char *)but->poin;
1646
1647                 vec[0] = ((float)cp[0]) / 255.0f;
1648                 vec[1] = ((float)cp[1]) / 255.0f;
1649                 vec[2] = ((float)cp[2]) / 255.0f;
1650         }
1651         else if (but->pointype == UI_BUT_POIN_FLOAT) {
1652                 const float *fp = (float *)but->poin;
1653                 copy_v3_v3(vec, fp);
1654         }
1655         else {
1656                 if (but->editvec == NULL) {
1657                         fprintf(stderr, "%s: can't get color, should never happen\n", __func__);
1658                         zero_v3(vec);
1659                 }
1660         }
1661
1662         if (but->type == UI_BTYPE_UNITVEC) {
1663                 normalize_v3(vec);
1664         }
1665 }
1666
1667 /* for buttons pointing to color for example */
1668 void ui_but_v3_set(uiBut *but, const float vec[3])
1669 {
1670         PropertyRNA *prop;
1671
1672         if (but->editvec) {
1673                 copy_v3_v3(but->editvec, vec);
1674         }
1675
1676         if (but->rnaprop) {
1677                 prop = but->rnaprop;
1678
1679                 if (RNA_property_type(prop) == PROP_FLOAT) {
1680                         int tot;
1681                         int a;
1682
1683                         tot = RNA_property_array_length(&but->rnapoin, prop);
1684                         BLI_assert(tot > 0);
1685                         if (tot == 3) {
1686                                 RNA_property_float_set_array(&but->rnapoin, prop, vec);
1687                         }
1688                         else {
1689                                 tot = min_ii(tot, 3);
1690                                 for (a = 0; a < tot; a++) {
1691                                         RNA_property_float_set_index(&but->rnapoin, prop, a, vec[a]);
1692                                 }
1693                         }
1694                 }
1695         }
1696         else if (but->pointype == UI_BUT_POIN_CHAR) {
1697                 char *cp = (char *)but->poin;
1698                 cp[0] = (char)(0.5f + vec[0] * 255.0f);
1699                 cp[1] = (char)(0.5f + vec[1] * 255.0f);
1700                 cp[2] = (char)(0.5f + vec[2] * 255.0f);
1701         }
1702         else if (but->pointype == UI_BUT_POIN_FLOAT) {
1703                 float *fp = (float *)but->poin;
1704                 copy_v3_v3(fp, vec);
1705         }
1706 }
1707
1708 bool ui_but_is_float(const uiBut *but)
1709 {
1710         if (but->pointype == UI_BUT_POIN_FLOAT && but->poin)
1711                 return true;
1712         
1713         if (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_FLOAT)
1714                 return true;
1715         
1716         return false;
1717 }
1718
1719 bool ui_but_is_bool(const uiBut *but)
1720 {
1721         if (ELEM(but->type, UI_BTYPE_TOGGLE, UI_BTYPE_TOGGLE_N, UI_BTYPE_ICON_TOGGLE, UI_BTYPE_ICON_TOGGLE_N))
1722                 return true;
1723
1724         if (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_BOOLEAN)
1725                 return true;
1726
1727         if ((but->rnaprop && RNA_property_type(but->rnaprop) == PROP_ENUM) && (but->type == UI_BTYPE_ROW))
1728                 return true;
1729
1730         return false;
1731 }
1732
1733
1734 bool ui_but_is_unit(const uiBut *but)
1735 {
1736         UnitSettings *unit = but->block->unit;
1737         const int unit_type = UI_but_unit_type_get(but);
1738
1739         if (unit_type == PROP_UNIT_NONE)
1740                 return false;
1741
1742 #if 1 /* removed so angle buttons get correct snapping */
1743         if (ui_but_is_unit_radians_ex(unit, unit_type))
1744                 return false;
1745 #endif
1746         
1747         /* for now disable time unit conversion */
1748         if (unit_type == PROP_UNIT_TIME)
1749                 return false;
1750
1751         if (unit->system == USER_UNIT_NONE) {
1752                 if (unit_type != PROP_UNIT_ROTATION) {
1753                         return false;
1754                 }
1755         }
1756
1757         return true;
1758 }
1759
1760 /**
1761  * Check if this button is similar enough to be grouped with another.
1762  */
1763 bool ui_but_is_compatible(const uiBut *but_a, const uiBut *but_b)
1764 {
1765         if (but_a->type != but_b->type)
1766                 return false;
1767         if (but_a->pointype != but_b->pointype)
1768                 return false;
1769
1770         if (but_a->rnaprop) {
1771                 /* skip 'rnapoin.data', 'rnapoin.id.data'
1772                  * allow different data to have the same props edited at once */
1773                 if (but_a->rnapoin.type != but_b->rnapoin.type)
1774                         return false;
1775                 if (RNA_property_type(but_a->rnaprop) != RNA_property_type(but_b->rnaprop))
1776                         return false;
1777                 if (RNA_property_subtype(but_a->rnaprop) != RNA_property_subtype(but_b->rnaprop))
1778                         return false;
1779         }
1780
1781         return true;
1782 }
1783
1784 bool ui_but_is_rna_valid(uiBut *but)
1785 {
1786         if (but->rnaprop == NULL || RNA_struct_contains_property(&but->rnapoin, but->rnaprop)) {
1787                 return true;
1788         }
1789         else {
1790                 printf("property removed %s: %p\n", but->drawstr, but->rnaprop);
1791                 return false;
1792         }
1793 }
1794
1795 double ui_but_value_get(uiBut *but)
1796 {
1797         PropertyRNA *prop;
1798         double value = 0.0;
1799
1800         if (but->editval) { return *(but->editval); }
1801         if (but->poin == NULL && but->rnapoin.data == NULL) return 0.0;
1802
1803         if (but->rnaprop) {
1804                 prop = but->rnaprop;
1805
1806                 BLI_assert(but->rnaindex != -1);
1807
1808                 switch (RNA_property_type(prop)) {
1809                         case PROP_BOOLEAN:
1810                                 if (RNA_property_array_check(prop))
1811                                         value = RNA_property_boolean_get_index(&but->rnapoin, prop, but->rnaindex);
1812                                 else
1813                                         value = RNA_property_boolean_get(&but->rnapoin, prop);
1814                                 break;
1815                         case PROP_INT:
1816                                 if (RNA_property_array_check(prop))
1817                                         value = RNA_property_int_get_index(&but->rnapoin, prop, but->rnaindex);
1818                                 else
1819                                         value = RNA_property_int_get(&but->rnapoin, prop);
1820                                 break;
1821                         case PROP_FLOAT:
1822                                 if (RNA_property_array_check(prop))
1823                                         value = RNA_property_float_get_index(&but->rnapoin, prop, but->rnaindex);
1824                                 else
1825                                         value = RNA_property_float_get(&but->rnapoin, prop);
1826                                 break;
1827                         case PROP_ENUM:
1828                                 value = RNA_property_enum_get(&but->rnapoin, prop);
1829                                 break;
1830                         default:
1831                                 value = 0.0;
1832                                 break;
1833                 }
1834         }
1835         else if (but->pointype == UI_BUT_POIN_CHAR) {
1836                 value = *(char *)but->poin;
1837         }
1838         else if (but->pointype == UI_BUT_POIN_SHORT) {
1839                 value = *(short *)but->poin;
1840         }
1841         else if (but->pointype == UI_BUT_POIN_INT) {
1842                 value = *(int *)but->poin;
1843         }
1844         else if (but->pointype == UI_BUT_POIN_FLOAT) {
1845                 value = *(float *)but->poin;
1846         }
1847
1848         return value;
1849 }
1850
1851 void ui_but_value_set(uiBut *but, double value)
1852 {
1853         PropertyRNA *prop;
1854
1855         /* value is a hsv value: convert to rgb */
1856         if (but->rnaprop) {
1857                 prop = but->rnaprop;
1858
1859                 if (RNA_property_editable(&but->rnapoin, prop)) {
1860                         switch (RNA_property_type(prop)) {
1861                                 case PROP_BOOLEAN:
1862                                         if (RNA_property_array_check(prop))
1863                                                 RNA_property_boolean_set_index(&but->rnapoin, prop, but->rnaindex, value);
1864                                         else
1865                                                 RNA_property_boolean_set(&but->rnapoin, prop, value);
1866                                         break;
1867                                 case PROP_INT:
1868                                         if (RNA_property_array_check(prop))
1869                                                 RNA_property_int_set_index(&but->rnapoin, prop, but->rnaindex, (int)value);
1870                                         else
1871                                                 RNA_property_int_set(&but->rnapoin, prop, (int)value);
1872                                         break;
1873                                 case PROP_FLOAT:
1874                                         if (RNA_property_array_check(prop))
1875                                                 RNA_property_float_set_index(&but->rnapoin, prop, but->rnaindex, value);
1876                                         else
1877                                                 RNA_property_float_set(&but->rnapoin, prop, value);
1878                                         break;
1879                                 case PROP_ENUM:
1880                                         if (RNA_property_flag(prop) & PROP_ENUM_FLAG) {
1881                                                 int ivalue = (int)value;
1882                                                 ivalue ^= RNA_property_enum_get(&but->rnapoin, prop); /* toggle for enum/flag buttons */
1883                                                 RNA_property_enum_set(&but->rnapoin, prop, ivalue);
1884                                         }
1885                                         else {
1886                                                 RNA_property_enum_set(&but->rnapoin, prop, value);
1887                                         }
1888                                         break;
1889                                 default:
1890                                         break;
1891                         }
1892                 }
1893
1894                 /* we can't be sure what RNA set functions actually do,
1895                  * so leave this unset */
1896                 value = UI_BUT_VALUE_UNSET;
1897         }
1898         else if (but->pointype == 0) {
1899                 /* pass */
1900         }
1901         else {
1902                 /* first do rounding */
1903                 if (but->pointype == UI_BUT_POIN_CHAR) {
1904                         value = (char)floor(value + 0.5);
1905                 }
1906                 else if (but->pointype == UI_BUT_POIN_SHORT) {
1907                         value = (short)floor(value + 0.5);
1908                 }
1909                 else if (but->pointype == UI_BUT_POIN_INT)
1910                         value = (int)floor(value + 0.5);
1911                 else if (but->pointype == UI_BUT_POIN_FLOAT) {
1912                         float fval = (float)value;
1913                         if (fval >= -0.00001f && fval <= 0.00001f) fval = 0.0f;  /* prevent negative zero */
1914                         value = fval;
1915                 }
1916                 
1917                 /* then set value with possible edit override */
1918                 if (but->editval)
1919                         value = *but->editval = value;
1920                 else if (but->pointype == UI_BUT_POIN_CHAR)
1921                         value = *((char *)but->poin) = (char)value;
1922                 else if (but->pointype == UI_BUT_POIN_SHORT)
1923                         value = *((short *)but->poin) = (short)value;
1924                 else if (but->pointype == UI_BUT_POIN_INT)
1925                         value = *((int *)but->poin) = (int)value;
1926                 else if (but->pointype == UI_BUT_POIN_FLOAT)
1927                         value = *((float *)but->poin) = (float)value;
1928         }
1929
1930         ui_but_update_select_flag(but, &value);
1931 }
1932
1933 int ui_but_string_get_max_length(uiBut *but)
1934 {
1935         if (ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU))
1936                 return but->hardmax;
1937         else
1938                 return UI_MAX_DRAW_STR;
1939 }
1940
1941 uiBut *ui_but_drag_multi_edit_get(uiBut *but)
1942 {
1943         uiBut *but_iter;
1944
1945         BLI_assert(but->flag & UI_BUT_DRAG_MULTI);
1946
1947         for (but_iter = but->block->buttons.first; but_iter; but_iter = but_iter->next) {
1948                 if (but_iter->editstr) {
1949                         break;
1950                 }
1951         }
1952
1953         return but_iter;
1954 }
1955
1956 /** \name Check to show extra icons
1957  *
1958  * Extra icons are shown on the right hand side of buttons.
1959  * \{ */
1960
1961 static bool ui_but_icon_extra_is_visible_search_unlink(const uiBut *but)
1962 {
1963         BLI_assert(but->type == UI_BTYPE_SEARCH_MENU);
1964         return ((but->editstr == NULL) &&
1965                 (but->drawstr[0] != '\0') &&
1966                 (but->flag & UI_BUT_SEARCH_UNLINK));
1967 }
1968
1969 static bool ui_but_icon_extra_is_visible_eyedropper(uiBut *but)
1970 {
1971         StructRNA *type;
1972         short idcode;
1973
1974         BLI_assert(but->type == UI_BTYPE_SEARCH_MENU && (but->flag & UI_BUT_SEARCH_UNLINK));
1975
1976         if (but->rnaprop == NULL) {
1977                 return false;
1978         }
1979
1980         type = RNA_property_pointer_type(&but->rnapoin, but->rnaprop);
1981         idcode = RNA_type_to_ID_code(type);
1982
1983
1984         return ((but->editstr == NULL) &&
1985                 (idcode == ID_OB || OB_DATA_SUPPORT_ID(idcode)));
1986 }
1987
1988 uiButExtraIconType ui_but_icon_extra_get(uiBut *but)
1989 {
1990         if ((but->flag & UI_BUT_SEARCH_UNLINK) == 0) {
1991                 /* pass */
1992         }
1993         else if (ui_but_icon_extra_is_visible_search_unlink(but)) {
1994                 return UI_BUT_ICONEXTRA_UNLINK;
1995         }
1996         else if (ui_but_icon_extra_is_visible_eyedropper(but)) {
1997                 return UI_BUT_ICONEXTRA_EYEDROPPER;
1998         }
1999
2000         return UI_BUT_ICONEXTRA_NONE;
2001 }
2002
2003 /** \} */
2004
2005
2006 static double ui_get_but_scale_unit(uiBut *but, double value)
2007 {
2008         UnitSettings *unit = but->block->unit;
2009         int unit_type = UI_but_unit_type_get(but);
2010
2011         /* Time unit is a bit special, not handled by BKE_scene_unit_scale() for now. */
2012         if (unit_type == PROP_UNIT_TIME) { /* WARNING - using evil_C :| */
2013                 Scene *scene = CTX_data_scene(but->block->evil_C);
2014                 return FRA2TIME(value);
2015         }
2016         else {
2017                 return BKE_scene_unit_scale(unit, RNA_SUBTYPE_UNIT_VALUE(unit_type), value);
2018         }
2019 }
2020
2021 /* str will be overwritten */
2022 void ui_but_convert_to_unit_alt_name(uiBut *but, char *str, size_t maxlen)
2023 {
2024         if (ui_but_is_unit(but)) {
2025                 UnitSettings *unit = but->block->unit;
2026                 int unit_type = UI_but_unit_type_get(but);
2027                 char *orig_str;
2028                 
2029                 orig_str = BLI_strdup(str);
2030                 
2031                 bUnit_ToUnitAltName(str, maxlen, orig_str, unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type));
2032                 
2033                 MEM_freeN(orig_str);
2034         }
2035 }
2036
2037 /**
2038  * \param float_precision  Override the button precision.
2039  */
2040 static void ui_get_but_string_unit(uiBut *but, char *str, int len_max, double value, bool pad, int float_precision)
2041 {
2042         UnitSettings *unit = but->block->unit;
2043         const bool do_split = (unit->flag & USER_UNIT_OPT_SPLIT) != 0;
2044         int unit_type = UI_but_unit_type_get(but);
2045         int precision;
2046
2047         if (unit->scale_length < 0.0001f) unit->scale_length = 1.0f;  // XXX do_versions
2048
2049         /* Use precision override? */
2050         if (float_precision == -1) {
2051                 /* Sanity checks */
2052                 precision = (int)but->a2;
2053                 if      (precision > UI_PRECISION_FLOAT_MAX) precision = UI_PRECISION_FLOAT_MAX;
2054                 else if (precision == -1)                    precision = 2;
2055         }
2056         else {
2057                 precision = float_precision;
2058         }
2059
2060         bUnit_AsString(str, len_max, ui_get_but_scale_unit(but, value), precision,
2061                        unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type), do_split, pad);
2062 }
2063
2064 static float ui_get_but_step_unit(uiBut *but, float step_default)
2065 {
2066         int unit_type = RNA_SUBTYPE_UNIT_VALUE(UI_but_unit_type_get(but));
2067         double step;
2068
2069         step = bUnit_ClosestScalar(ui_get_but_scale_unit(but, step_default), but->block->unit->system, unit_type);
2070
2071         /* -1 is an error value */
2072         if (step != -1.0) {
2073                 BLI_assert(step > 0.0);
2074                 return (float)(step / ui_get_but_scale_unit(but, 1.0)) * 100.0f;
2075         }
2076         else {
2077                 return step_default;
2078         }
2079 }
2080
2081 /**
2082  * \param float_precision  For number buttons the precision to use or -1 to fallback to the button default.
2083  */
2084 void ui_but_string_get_ex(uiBut *but, char *str, const size_t maxlen, const int float_precision)
2085 {
2086         if (but->rnaprop && ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU)) {
2087                 PropertyType type;
2088                 const char *buf = NULL;
2089                 int buf_len;
2090
2091                 type = RNA_property_type(but->rnaprop);
2092
2093                 if (type == PROP_STRING) {
2094                         /* RNA string */
2095                         buf = RNA_property_string_get_alloc(&but->rnapoin, but->rnaprop, str, maxlen, &buf_len);
2096                 }
2097                 else if (type == PROP_ENUM) {
2098                         /* RNA enum */
2099                         int value = RNA_property_enum_get(&but->rnapoin, but->rnaprop);
2100                         if (RNA_property_enum_name(but->block->evil_C, &but->rnapoin, but->rnaprop, value, &buf)) {
2101                                 BLI_strncpy(str, buf, maxlen);
2102                                 buf = str;
2103                         }
2104                 }
2105                 else if (type == PROP_POINTER) {
2106                         /* RNA pointer */
2107                         PointerRNA ptr = RNA_property_pointer_get(&but->rnapoin, but->rnaprop);
2108                         buf = RNA_struct_name_get_alloc(&ptr, str, maxlen, &buf_len);
2109                 }
2110                 else {
2111                         BLI_assert(0);
2112                 }
2113
2114                 if (!buf) {
2115                         str[0] = '\0';
2116                 }
2117                 else if (buf && buf != str) {
2118                         /* string was too long, we have to truncate */
2119                         memcpy(str, buf, MIN2(maxlen, (size_t)(buf_len + 1)));
2120                         MEM_freeN((void *)buf);
2121                 }
2122         }
2123         else if (but->type == UI_BTYPE_TEXT) {
2124                 /* string */
2125                 BLI_strncpy(str, but->poin, maxlen);
2126                 return;
2127         }
2128         else if (but->type == UI_BTYPE_SEARCH_MENU) {
2129                 /* string */
2130                 BLI_strncpy(str, but->poin, maxlen);
2131                 return;
2132         }
2133         else if (ui_but_anim_expression_get(but, str, maxlen)) {
2134                 /* driver expression */
2135         }
2136         else {
2137                 /* number editing */
2138                 double value;
2139
2140                 value = ui_but_value_get(but);
2141
2142                 if (ui_but_is_float(but)) {
2143                         if (ui_but_is_unit(but)) {
2144                                 ui_get_but_string_unit(but, str, maxlen, value, false, float_precision);
2145                         }
2146                         else {
2147                                 const int prec = (float_precision == -1) ? ui_but_calc_float_precision(but, value) : float_precision;
2148                                 BLI_snprintf(str, maxlen, "%.*f", prec, value);
2149                         }
2150                 }
2151                 else
2152                         BLI_snprintf(str, maxlen, "%d", (int)value);
2153         }
2154 }
2155 void ui_but_string_get(uiBut *but, char *str, const size_t maxlen)
2156 {
2157         ui_but_string_get_ex(but, str, maxlen, -1);
2158 }
2159
2160 #ifdef WITH_PYTHON
2161
2162 static bool ui_set_but_string_eval_num_unit(bContext *C, uiBut *but, const char *str, double *value)
2163 {
2164         char str_unit_convert[256];
2165         const int unit_type = UI_but_unit_type_get(but);
2166
2167         BLI_strncpy(str_unit_convert, str, sizeof(str_unit_convert));
2168
2169         /* ugly, use the draw string to get the value,
2170          * this could cause problems if it includes some text which resolves to a unit */
2171         bUnit_ReplaceString(str_unit_convert, sizeof(str_unit_convert), but->drawstr,
2172                             ui_get_but_scale_unit(but, 1.0), but->block->unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type));
2173
2174         return (BPY_button_exec(C, str_unit_convert, value, true) != -1);
2175 }
2176
2177 #endif /* WITH_PYTHON */
2178
2179
2180 bool ui_but_string_set_eval_num(bContext *C, uiBut *but, const char *str, double *value)
2181 {
2182         bool ok = false;
2183
2184 #ifdef WITH_PYTHON
2185
2186         if (str[0] != '\0') {
2187                 bool is_unit_but = (ui_but_is_float(but) && ui_but_is_unit(but));
2188                 /* only enable verbose if we won't run again with units */
2189                 if (BPY_button_exec(C, str, value, is_unit_but == false) != -1) {
2190                         /* if the value parsed ok without unit conversion this button may still need a unit multiplier */
2191                         if (is_unit_but) {
2192                                 char str_new[128];
2193
2194                                 BLI_snprintf(str_new, sizeof(str_new), "%f", *value);
2195                                 ok = ui_set_but_string_eval_num_unit(C, but, str_new, value);
2196                         }
2197                         else {
2198                                 ok = true; /* parse normal string via py (no unit conversion needed) */
2199                         }
2200                 }
2201                 else if (is_unit_but) {
2202                         /* parse failed, this is a unit but so run replacements and parse again */
2203                         ok = ui_set_but_string_eval_num_unit(C, but, str, value);
2204                 }
2205         }
2206
2207 #else /* WITH_PYTHON */
2208
2209         *value = atof(str);
2210         ok = true;
2211
2212         (void)C;
2213         (void)but;
2214
2215 #endif /* WITH_PYTHON */
2216
2217         return ok;
2218 }
2219
2220 /* just the assignment/free part */
2221 static void ui_but_string_set_internal(uiBut *but, const char *str, size_t str_len)
2222 {
2223         BLI_assert(str_len == strlen(str));
2224         BLI_assert(but->str == NULL);
2225         str_len += 1;
2226
2227         if (str_len > UI_MAX_NAME_STR) {
2228                 but->str = MEM_mallocN(str_len, "ui_def_but str");
2229         }
2230         else {
2231                 but->str = but->strdata;
2232         }
2233         memcpy(but->str, str, str_len);
2234 }
2235
2236 static void ui_but_string_free_internal(uiBut *but)
2237 {
2238         if (but->str) {
2239                 if (but->str != but->strdata) {
2240                         MEM_freeN(but->str);
2241                 }
2242                 /* must call 'ui_but_string_set_internal' after */
2243                 but->str = NULL;
2244         }
2245 }
2246
2247 bool ui_but_string_set(bContext *C, uiBut *but, const char *str)
2248 {
2249         if (but->rnaprop && ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU)) {
2250                 if (RNA_property_editable(&but->rnapoin, but->rnaprop)) {
2251                         PropertyType type;
2252
2253                         type = RNA_property_type(but->rnaprop);
2254
2255                         if (type == PROP_STRING) {
2256                                 /* RNA string */
2257                                 RNA_property_string_set(&but->rnapoin, but->rnaprop, str);
2258                                 return true;
2259                         }
2260                         else if (type == PROP_POINTER) {
2261                                 /* RNA pointer */
2262                                 PointerRNA ptr, rptr;
2263                                 PropertyRNA *prop;
2264
2265                                 if (str[0] == '\0') {
2266                                         RNA_property_pointer_set(&but->rnapoin, but->rnaprop, PointerRNA_NULL);
2267                                         return true;
2268                                 }
2269                                 else {
2270                                         ptr = but->rnasearchpoin;
2271                                         prop = but->rnasearchprop;
2272                                         
2273                                         if (prop && RNA_property_collection_lookup_string(&ptr, prop, str, &rptr))
2274                                                 RNA_property_pointer_set(&but->rnapoin, but->rnaprop, rptr);
2275
2276                                         return true;
2277                                 }
2278
2279                                 return false;
2280                         }
2281                         else if (type == PROP_ENUM) {
2282                                 int value;
2283                                 if (RNA_property_enum_value(but->block->evil_C, &but->rnapoin, but->rnaprop, str, &value)) {
2284                                         RNA_property_enum_set(&but->rnapoin, but->rnaprop, value);
2285                                         return true;
2286                                 }
2287                                 return false;
2288                         }
2289                         else {
2290                                 BLI_assert(0);
2291                         }
2292                 }
2293         }
2294         else if (but->type == UI_BTYPE_TEXT) {
2295                 /* string */
2296                 if (ui_but_is_utf8(but)) BLI_strncpy_utf8(but->poin, str, but->hardmax);
2297                 else BLI_strncpy(but->poin, str, but->hardmax);
2298
2299                 return true;
2300         }
2301         else if (but->type == UI_BTYPE_SEARCH_MENU) {
2302                 /* string */
2303                 BLI_strncpy(but->poin, str, but->hardmax);
2304                 return true;
2305         }
2306         else if (ui_but_anim_expression_set(but, str)) {
2307                 /* driver expression */
2308                 return true;
2309         }
2310         else if (str[0] == '#') {
2311                 /* shortcut to create new driver expression (versus immediate Py-execution) */
2312                 return ui_but_anim_expression_create(but, str + 1);
2313         }
2314         else {
2315                 /* number editing */
2316                 double value;
2317
2318                 if (ui_but_string_set_eval_num(C, but, str, &value) == false) {
2319                         WM_report_banner_show(C);
2320                         return false;
2321                 }
2322
2323                 if (!ui_but_is_float(but)) value = (int)floor(value + 0.5);
2324
2325                 /* not that we use hard limits here */
2326                 if (value < (double)but->hardmin) value = but->hardmin;
2327                 if (value > (double)but->hardmax) value = but->hardmax;
2328
2329                 ui_but_value_set(but, value);
2330                 return true;
2331         }
2332
2333         return false;
2334 }
2335
2336 void ui_but_default_set(bContext *C, const bool all, const bool use_afterfunc)
2337 {
2338         wmOperatorType *ot = WM_operatortype_find("UI_OT_reset_default_button", true);
2339
2340         if (use_afterfunc) {
2341                 PointerRNA *ptr;
2342                 ptr = ui_handle_afterfunc_add_operator(ot, WM_OP_EXEC_DEFAULT, true);
2343                 RNA_boolean_set(ptr, "all", all);
2344         }
2345         else {
2346                 PointerRNA ptr;
2347                 WM_operator_properties_create_ptr(&ptr, ot);
2348                 RNA_boolean_set(&ptr, "all", all);
2349                 WM_operator_name_call_ptr(C, ot, WM_OP_EXEC_DEFAULT, &ptr);
2350                 WM_operator_properties_free(&ptr);
2351         }
2352 }
2353
2354 static double soft_range_round_up(double value, double max)
2355 {
2356         /* round up to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, ..
2357          * checking for 0.0 prevents floating point exceptions */
2358         double newmax = (value != 0.0) ? pow(10.0, ceil(log(value) / M_LN10)) : 0.0;
2359
2360         if (newmax * 0.2 >= max && newmax * 0.2 >= value)
2361                 return newmax * 0.2;
2362         else if (newmax * 0.5 >= max && newmax * 0.5 >= value)
2363                 return newmax * 0.5;
2364         else
2365                 return newmax;
2366 }
2367
2368 static double soft_range_round_down(double value, double max)
2369 {
2370         /* round down to .., 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, ..
2371          * checking for 0.0 prevents floating point exceptions */
2372         double newmax = (value != 0.0) ? pow(10.0, floor(log(value) / M_LN10)) : 0.0;
2373
2374         if (newmax * 5.0 <= max && newmax * 5.0 <= value)
2375                 return newmax * 5.0;
2376         else if (newmax * 2.0 <= max && newmax * 2.0 <= value)
2377                 return newmax * 2.0;
2378         else
2379                 return newmax;
2380 }
2381
2382 /* note: this could be split up into functions which handle arrays and not */
2383 static void ui_set_but_soft_range(uiBut *but)
2384 {
2385         /* ideally we would not limit this but practically, its more than
2386          * enough worst case is very long vectors wont use a smart soft-range
2387          * which isn't so bad. */
2388
2389         if (but->rnaprop) {
2390                 const PropertyType type = RNA_property_type(but->rnaprop);
2391                 double softmin, softmax /*, step, precision*/;
2392                 double value_min;
2393                 double value_max;
2394
2395                 /* clamp button range to something reasonable in case
2396                  * we get -inf/inf from RNA properties */
2397                 if (type == PROP_INT) {
2398                         const bool is_array = RNA_property_array_check(but->rnaprop);
2399                         int imin, imax, istep;
2400
2401                         RNA_property_int_ui_range(&but->rnapoin, but->rnaprop, &imin, &imax, &istep);
2402                         softmin = (imin == INT_MIN) ? -1e4 : imin;
2403                         softmax = (imin == INT_MAX) ? 1e4 : imax;
2404                         /*step = istep;*/ /*UNUSED*/
2405                         /*precision = 1;*/ /*UNUSED*/
2406
2407                         if (is_array) {
2408                                 int value_range[2];
2409                                 RNA_property_int_get_array_range(&but->rnapoin, but->rnaprop, value_range);
2410                                 value_min = (double)value_range[0];
2411                                 value_max = (double)value_range[1];
2412                         }
2413                         else {
2414                                 value_min = value_max = (double)RNA_property_int_get(&but->rnapoin, but->rnaprop);
2415                         }
2416                 }
2417                 else if (type == PROP_FLOAT) {
2418                         const bool is_array = RNA_property_array_check(but->rnaprop);
2419                         float fmin, fmax, fstep, fprecision;
2420
2421                         RNA_property_float_ui_range(&but->rnapoin, but->rnaprop, &fmin, &fmax, &fstep, &fprecision);
2422                         softmin = (fmin == -FLT_MAX) ? (float)-1e4 : fmin;
2423                         softmax = (fmax == FLT_MAX) ? (float)1e4 : fmax;
2424                         /*step = fstep;*/ /*UNUSED*/
2425                         /*precision = fprecision;*/ /*UNUSED*/
2426
2427                         if (is_array) {
2428                                 float value_range[2];
2429                                 RNA_property_float_get_array_range(&but->rnapoin, but->rnaprop, value_range);
2430                                 value_min = (double)value_range[0];
2431                                 value_max = (double)value_range[1];
2432                         }
2433                         else {
2434                                 value_min = value_max = (double)RNA_property_float_get(&but->rnapoin, but->rnaprop);
2435                         }
2436                 }
2437                 else {
2438                         return;
2439                 }
2440
2441                 /* if the value goes out of the soft/max range, adapt the range */
2442                 if (value_min + 1e-10 < softmin) {
2443                         if (value_min < 0.0)
2444                                 softmin = -soft_range_round_up(-value_min, -softmin);
2445                         else
2446                                 softmin = soft_range_round_down(value_min, softmin);
2447
2448                         if (softmin < (double)but->hardmin)
2449                                 softmin = (double)but->hardmin;
2450                 }
2451                 if (value_max - 1e-10 > softmax) {
2452                         if (value_max < 0.0)
2453                                 softmax = -soft_range_round_down(-value_max, -softmax);
2454                         else
2455                                 softmax = soft_range_round_up(value_max, softmax);
2456
2457                         if (softmax > (double)but->hardmax)
2458                                 softmax = but->hardmax;
2459                 }
2460
2461                 but->softmin = softmin;
2462                 but->softmax = softmax;
2463         }
2464         else if (but->poin && (but->pointype & UI_BUT_POIN_TYPES)) {
2465                 float value = ui_but_value_get(but);
2466                 CLAMP(value, but->hardmin, but->hardmax);
2467                 but->softmin = min_ff(but->softmin, value);
2468                 but->softmax = max_ff(but->softmax, value);
2469         }
2470         else {
2471                 BLI_assert(0);
2472         }
2473 }
2474
2475 /* ******************* Free ********************/
2476
2477 static void ui_free_link(uiLink *link)
2478 {
2479         if (link) {
2480                 BLI_freelistN(&link->lines);
2481                 MEM_freeN(link);
2482         }
2483 }
2484
2485 /* can be called with C==NULL */
2486 static void ui_but_free(const bContext *C, uiBut *but)
2487 {
2488         if (but->opptr) {
2489                 WM_operator_properties_free(but->opptr);
2490                 MEM_freeN(but->opptr);
2491         }
2492
2493         if (but->func_argN) {
2494                 MEM_freeN(but->func_argN);
2495         }
2496
2497         if (but->tip_argN) {
2498                 MEM_freeN(but->tip_argN);
2499         }
2500
2501         if (but->active) {
2502                 /* XXX solve later, buttons should be free-able without context ideally,
2503                  * however they may have open tooltips or popup windows, which need to
2504                  * be closed using a context pointer */
2505                 if (C) {
2506                         ui_but_active_free(C, but);
2507                 }
2508                 else {
2509                         if (but->active) {
2510                                 MEM_freeN(but->active);
2511                         }
2512                 }
2513         }
2514         if (but->str && but->str != but->strdata) {
2515                 MEM_freeN(but->str);
2516         }
2517         ui_free_link(but->link);
2518
2519         if ((but->type == UI_BTYPE_IMAGE) && but->poin) {
2520                 IMB_freeImBuf((struct ImBuf *)but->poin);
2521         }
2522
2523         BLI_assert(UI_butstore_is_registered(but->block, but) == false);
2524
2525         MEM_freeN(but);
2526 }
2527
2528 /* can be called with C==NULL */
2529 void UI_block_free(const bContext *C, uiBlock *block)
2530 {
2531         uiBut *but;
2532
2533         UI_butstore_clear(block);
2534
2535         while ((but = BLI_pophead(&block->buttons))) {
2536                 ui_but_free(C, but);
2537         }
2538
2539         if (block->unit) {
2540                 MEM_freeN(block->unit);
2541         }
2542
2543         if (block->func_argN) {
2544                 MEM_freeN(block->func_argN);
2545         }
2546
2547         CTX_store_free_list(&block->contexts);
2548
2549         BLI_freelistN(&block->saferct);
2550         BLI_freelistN(&block->color_pickers.list);
2551         
2552         MEM_freeN(block);
2553 }
2554
2555 /* can be called with C==NULL */
2556 void UI_blocklist_free(const bContext *C, ListBase *lb)
2557 {
2558         uiBlock *block;
2559         
2560         while ((block = BLI_pophead(lb))) {
2561                 UI_block_free(C, block);
2562         }
2563 }
2564
2565 void UI_blocklist_free_inactive(const bContext *C, ListBase *lb)
2566 {
2567         uiBlock *block, *nextblock;
2568
2569         for (block = lb->first; block; block = nextblock) {
2570                 nextblock = block->next;
2571         
2572                 if (!block->handle) {
2573                         if (!block->active) {
2574                                 BLI_remlink(lb, block);
2575                                 UI_block_free(C, block);
2576                         }
2577                         else
2578                                 block->active = 0;
2579                 }
2580         }
2581 }
2582
2583 void UI_block_region_set(uiBlock *block, ARegion *region)
2584 {
2585         ListBase *lb = &region->uiblocks;
2586         uiBlock *oldblock = NULL;
2587
2588         /* each listbase only has one block with this name, free block
2589          * if is already there so it can be rebuilt from scratch */
2590         if (lb) {
2591                 oldblock = BLI_findstring(lb, block->name, offsetof(uiBlock, name));
2592
2593                 if (oldblock) {
2594                         oldblock->active = 0;
2595                         oldblock->panel = NULL;
2596                         oldblock->handle = NULL;
2597                 }
2598
2599                 /* at the beginning of the list! for dynamical menus/blocks */
2600                 BLI_addhead(lb, block);
2601         }
2602
2603         block->oldblock = oldblock;
2604 }
2605
2606 uiBlock *UI_block_begin(const bContext *C, ARegion *region, const char *name, short dt)
2607 {
2608         uiBlock *block;
2609         wmWindow *window;
2610         Scene *scn;
2611         int getsizex, getsizey;
2612
2613         window = CTX_wm_window(C);
2614         scn = CTX_data_scene(C);
2615
2616         block = MEM_callocN(sizeof(uiBlock), "uiBlock");
2617         block->active = 1;
2618         block->dt = dt;
2619         block->evil_C = (void *)C;  /* XXX */
2620
2621         if (scn) {
2622                 block->color_profile = true;
2623
2624                 /* store display device name, don't lookup for transformations yet
2625                  * block could be used for non-color displays where looking up for transformation
2626                  * would slow down redraw, so only lookup for actual transform when it's indeed
2627                  * needed
2628                  */
2629                 BLI_strncpy(block->display_device, scn->display_settings.display_device, sizeof(block->display_device));
2630
2631                 /* copy to avoid crash when scene gets deleted with ui still open */
2632                 block->unit = MEM_mallocN(sizeof(scn->unit), "UI UnitSettings");
2633                 memcpy(block->unit, &scn->unit, sizeof(scn->unit));
2634         }
2635
2636         BLI_strncpy(block->name, name, sizeof(block->name));
2637
2638         if (region)
2639                 UI_block_region_set(block, region);
2640
2641         /* window matrix and aspect */
2642         if (region && region->swinid) {
2643                 wm_subwindow_matrix_get(window, region->swinid, block->winmat);
2644                 wm_subwindow_size_get(window, region->swinid, &getsizex, &getsizey);
2645
2646                 block->aspect = 2.0f / fabsf(getsizex * block->winmat[0][0]);
2647         }
2648         else {
2649                 /* no subwindow created yet, for menus for example, so we
2650                  * use the main window instead, since buttons are created
2651                  * there anyway */
2652                 wm_subwindow_matrix_get(window, window->screen->mainwin, block->winmat);
2653                 wm_subwindow_size_get(window, window->screen->mainwin, &getsizex, &getsizey);
2654
2655                 block->aspect = 2.0f / fabsf(getsizex * block->winmat[0][0]);
2656                 block->auto_open = true;
2657                 block->flag |= UI_BLOCK_LOOP; /* tag as menu */
2658         }
2659
2660         return block;
2661 }
2662
2663 uiBlock *UI_block_find_in_region(const char *name, ARegion *ar)
2664 {
2665         return BLI_findstring(&ar->uiblocks, name, offsetof(uiBlock, name));
2666 }
2667
2668 void UI_block_emboss_set(uiBlock *block, char dt)
2669 {
2670         block->dt = dt;
2671 }
2672
2673 void ui_but_update(uiBut *but)
2674 {
2675         /* if something changed in the button */
2676         double value = UI_BUT_VALUE_UNSET;
2677 //      float okwidth; // UNUSED
2678         
2679         ui_but_update_select_flag(but, &value);
2680         
2681         /* only update soft range while not editing */
2682         if (!(but->editval || but->editstr || but->editvec)) {
2683                 if ((but->rnaprop != NULL) ||
2684                     (but->poin && (but->pointype & UI_BUT_POIN_TYPES)))
2685                 {
2686                         ui_set_but_soft_range(but);
2687                 }
2688         }
2689
2690         /* test for min and max, icon sliders, etc */
2691         switch (but->type) {
2692                 case UI_BTYPE_NUM:
2693                 case UI_BTYPE_SCROLL:
2694                 case UI_BTYPE_NUM_SLIDER:
2695                         UI_GET_BUT_VALUE_INIT(but, value);
2696                         if      (value < (double)but->hardmin) ui_but_value_set(but, but->hardmin);
2697                         else if (value > (double)but->hardmax) ui_but_value_set(but, but->hardmax);
2698
2699                         /* max must never be smaller than min! Both being equal is allowed though */
2700                         BLI_assert(but->softmin <= but->softmax &&
2701                                    but->hardmin <= but->hardmax);
2702                         break;
2703                         
2704                 case UI_BTYPE_ICON_TOGGLE:
2705                 case UI_BTYPE_ICON_TOGGLE_N:
2706                         if (!but->rnaprop || (RNA_property_flag(but->rnaprop) & PROP_ICONS_CONSECUTIVE)) {
2707                                 if (but->flag & UI_SELECT) but->iconadd = 1;
2708                                 else but->iconadd = 0;
2709                         }
2710                         break;
2711                         
2712                         /* quiet warnings for unhandled types */
2713                 default:
2714                         break;
2715         }
2716         
2717         
2718         /* safety is 4 to enable small number buttons (like 'users') */
2719         // okwidth = -4 + (BLI_rcti_size_x(&but->rect)); // UNUSED
2720         
2721         /* name: */
2722         switch (but->type) {
2723
2724                 case UI_BTYPE_MENU:
2725                         if (BLI_rctf_size_x(&but->rect) > 24.0f) {
2726                                 /* only needed for menus in popup blocks that don't recreate buttons on redraw */
2727                                 if (but->block->flag & UI_BLOCK_LOOP) {
2728                                         if (but->rnaprop && (RNA_property_type(but->rnaprop) == PROP_ENUM)) {
2729                                                 int value = RNA_property_enum_get(&but->rnapoin, but->rnaprop);
2730                                                 const char *buf;
2731                                                 if (RNA_property_enum_name_gettexted(but->block->evil_C,
2732                                                                                      &but->rnapoin, but->rnaprop, value, &buf))
2733                                                 {
2734                                                         size_t slen = strlen(buf);
2735                                                         ui_but_string_free_internal(but);
2736                                                         ui_but_string_set_internal(but, buf, slen);
2737                                                 }
2738                                         }
2739                                 }
2740                                 BLI_strncpy(but->drawstr, but->str, sizeof(but->drawstr));
2741                         }
2742                         break;
2743
2744                 case UI_BTYPE_NUM:
2745                 case UI_BTYPE_NUM_SLIDER:
2746
2747                         if (!but->editstr) {
2748                                 const char *drawstr_suffix = NULL;
2749                                 size_t slen;
2750
2751                                 UI_GET_BUT_VALUE_INIT(but, value);
2752
2753                                 slen = BLI_strncpy_rlen(but->drawstr, but->str, sizeof(but->drawstr));
2754
2755                                 if (ui_but_is_float(but)) {
2756                                         if (value == (double) FLT_MAX) {
2757                                                 slen += BLI_strncpy_rlen(but->drawstr + slen, "inf", sizeof(but->drawstr) - slen);
2758                                         }
2759                                         else if (value == (double) -FLT_MAX) {
2760                                                 slen += BLI_strncpy_rlen(but->drawstr + slen, "-inf", sizeof(but->drawstr) - slen);
2761                                         }
2762                                         /* support length type buttons */
2763                                         else if (ui_but_is_unit(but)) {
2764                                                 char new_str[sizeof(but->drawstr)];
2765                                                 ui_get_but_string_unit(but, new_str, sizeof(new_str), value, true, -1);
2766                                                 slen += BLI_strncpy_rlen(but->drawstr + slen, new_str, sizeof(but->drawstr) - slen);
2767                                         }
2768                                         else {
2769                                                 const int prec = ui_but_calc_float_precision(but, value);
2770                                                 slen += BLI_snprintf_rlen(but->drawstr + slen, sizeof(but->drawstr) - slen, "%.*f", prec, value);
2771                                         }
2772                                 }
2773                                 else {
2774                                         slen += BLI_snprintf_rlen(but->drawstr + slen, sizeof(but->drawstr) - slen, "%d", (int)value);
2775                                 }
2776
2777                                 if (but->rnaprop) {
2778                                         PropertySubType pstype = RNA_property_subtype(but->rnaprop);
2779
2780                                         if (pstype == PROP_PERCENTAGE) {
2781                                                 drawstr_suffix = "%";
2782                                         }
2783                                         else if (pstype == PROP_PIXEL) {
2784                                                 drawstr_suffix = " px";
2785                                         }
2786                                 }
2787
2788                                 if (drawstr_suffix) {
2789                                         BLI_strncpy(but->drawstr + slen, drawstr_suffix, sizeof(but->drawstr) - slen);
2790                                 }
2791
2792                         }
2793                         break;
2794
2795                 case UI_BTYPE_LABEL:
2796                         if (ui_but_is_float(but)) {
2797                                 int prec;
2798                                 UI_GET_BUT_VALUE_INIT(but, value);
2799                                 prec = ui_but_calc_float_precision(but, value);
2800                                 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%.*f", but->str, prec, value);
2801                         }
2802                         else {
2803                                 BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2804                         }
2805                 
2806                         break;
2807
2808                 case UI_BTYPE_TEXT:
2809                 case UI_BTYPE_SEARCH_MENU:
2810                         if (!but->editstr) {
2811                                 char str[UI_MAX_DRAW_STR];
2812
2813                                 ui_but_string_get(but, str, UI_MAX_DRAW_STR);
2814                                 BLI_snprintf(but->drawstr, sizeof(but->drawstr), "%s%s", but->str, str);
2815                         }
2816                         break;
2817         
2818                 case UI_BTYPE_KEY_EVENT:
2819                 {
2820                         const char *str;
2821                         if (but->flag & UI_SELECT) {
2822                                 str = "Press a key";
2823                         }
2824                         else {
2825                                 UI_GET_BUT_VALUE_INIT(but, value);
2826                                 str = WM_key_event_string((short)value, false);
2827                         }
2828                         BLI_snprintf(but->drawstr, UI_MAX_DRAW_STR, "%s%s", but->str, str);
2829                         break;
2830                 }
2831                 case UI_BTYPE_HOTKEY_EVENT:
2832                         if (but->flag & UI_SELECT) {
2833
2834                                 if (but->modifier_key) {
2835                                         char *str = but->drawstr;
2836                                         but->drawstr[0] = '\0';
2837
2838                                         if (but->modifier_key & KM_SHIFT)
2839                                                 str += BLI_strcpy_rlen(str, "Shift ");
2840                                         if (but->modifier_key & KM_CTRL)
2841                                                 str += BLI_strcpy_rlen(str, "Ctrl ");
2842                                         if (but->modifier_key & KM_ALT)
2843                                                 str += BLI_strcpy_rlen(str, "Alt ");
2844                                         if (but->modifier_key & KM_OSKEY)
2845                                                 str += BLI_strcpy_rlen(str, "Cmd ");
2846
2847                                         (void)str; /* UNUSED */
2848                                 }
2849                                 else {
2850                                         BLI_strncpy(but->drawstr, "Press a key", UI_MAX_DRAW_STR);
2851                                 }
2852                         }
2853                         else
2854                                 BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2855
2856                         break;
2857
2858                 case UI_BTYPE_HSVCUBE:
2859                 case UI_BTYPE_HSVCIRCLE:
2860                         break;
2861                 default:
2862                         BLI_strncpy(but->drawstr, but->str, UI_MAX_DRAW_STR);
2863                         break;
2864                 
2865         }
2866
2867         /* if we are doing text editing, this will override the drawstr */
2868         if (but->editstr)
2869                 but->drawstr[0] = '\0';
2870         
2871         /* text clipping moved to widget drawing code itself */
2872 }
2873
2874
2875 void UI_block_align_begin(uiBlock *block)
2876 {
2877         /* if other align was active, end it */
2878         if (block->flag & UI_BUT_ALIGN) UI_block_align_end(block);
2879
2880         block->flag |= UI_BUT_ALIGN_DOWN;
2881         block->alignnr++;
2882
2883         /* buttons declared after this call will get this align nr */ // XXX flag?
2884 }
2885
2886 static bool buts_are_horiz(uiBut *but1, uiBut *but2)
2887 {
2888         float dx, dy;
2889
2890         /* simple case which can fail if buttons shift apart
2891          * with proportional layouts, see: [#38602] */
2892         if ((but1->rect.ymin == but2->rect.ymin) &&
2893             (but1->rect.xmin != but2->rect.xmin))
2894         {
2895                 return true;
2896         }
2897
2898         dx = fabsf(but1->rect.xmax - but2->rect.xmin);
2899         dy = fabsf(but1->rect.ymin - but2->rect.ymax);
2900         
2901         return (dx <= dy);
2902 }
2903
2904 void UI_block_align_end(uiBlock *block)
2905 {
2906         block->flag &= ~UI_BUT_ALIGN;  /* all 4 flags */
2907 }
2908
2909 bool ui_but_can_align(uiBut *but)
2910 {
2911         return !ELEM(but->type, UI_BTYPE_LABEL, UI_BTYPE_CHECKBOX, UI_BTYPE_CHECKBOX_N, UI_BTYPE_SEPR, UI_BTYPE_SEPR_LINE);
2912 }
2913
2914 static void ui_block_align_calc_but(uiBut *first, short nr)
2915 {
2916         uiBut *prev, *but = NULL, *next;
2917         int flag = 0, cols = 0, rows = 0;
2918         
2919         /* auto align */
2920
2921         for (but = first; but && but->alignnr == nr; but = but->next) {
2922                 if (but->next && but->next->alignnr == nr) {
2923                         if (buts_are_horiz(but, but->next)) cols++;
2924                         else rows++;
2925                 }
2926         }
2927
2928         /* rows == 0: 1 row, cols == 0: 1 column */
2929         
2930         /* note;  how it uses 'flag' in loop below (either set it, or OR it) is confusing */
2931         for (but = first, prev = NULL; but && but->alignnr == nr; prev = but, but = but->next) {
2932                 next = but->next;
2933                 if (next && next->alignnr != nr)
2934                         next = NULL;
2935
2936                 /* clear old flag */
2937                 but->drawflag &= ~UI_BUT_ALIGN;
2938                         
2939                 if (flag == 0) {  /* first case */
2940                         if (next) {
2941                                 if (buts_are_horiz(but, next)) {
2942                                         if (rows == 0)
2943                                                 flag = UI_BUT_ALIGN_RIGHT;
2944                                         else 
2945                                                 flag = UI_BUT_ALIGN_DOWN | UI_BUT_ALIGN_RIGHT;
2946                                 }
2947                                 else {
2948                                         flag = UI_BUT_ALIGN_DOWN;
2949                                 }
2950                         }
2951                 }
2952                 else if (next == NULL) {  /* last case */
2953                         if (prev) {
2954                                 if (buts_are_horiz(prev, but)) {
2955                                         if (rows == 0)
2956                                                 flag = UI_BUT_ALIGN_LEFT;
2957                                         else
2958                                                 flag = UI_BUT_ALIGN_TOP | UI_BUT_ALIGN_LEFT;
2959                                 }
2960                                 else {
2961                                         flag = UI_BUT_ALIGN_TOP;
2962                                 }
2963                         }
2964                 }
2965                 else if (buts_are_horiz(but, next)) {
2966                         /* check if this is already second row */
2967                         if (prev && buts_are_horiz(prev, but) == 0) {
2968                                 flag &= ~UI_BUT_ALIGN_LEFT;
2969                                 flag |= UI_BUT_ALIGN_TOP;
2970                                 /* exception case: bottom row */
2971                                 if (rows > 0) {
2972                                         uiBut *bt = but;
2973                                         while (bt && bt->alignnr == nr) {
2974                                                 if (bt->next && bt->next->alignnr == nr && buts_are_horiz(bt, bt->next) == 0) {
2975                                                         break;
2976                                                 }
2977                                                 bt = bt->next;
2978                                         }
2979                                         if (bt == NULL || bt->alignnr != nr) flag = UI_BUT_ALIGN_TOP | UI_BUT_ALIGN_RIGHT;
2980                                 }
2981                         }
2982                         else {
2983                                 flag |= UI_BUT_ALIGN_LEFT;
2984                         }
2985                 }
2986                 else {
2987                         if (cols == 0) {
2988                                 flag |= UI_BUT_ALIGN_TOP;
2989                         }
2990                         else {  /* next button switches to new row */
2991                                 
2992                                 if (prev && buts_are_horiz(prev, but))
2993                                         flag |= UI_BUT_ALIGN_LEFT;
2994                                 else {
2995                                         flag &= ~UI_BUT_ALIGN_LEFT;
2996                                         flag |= UI_BUT_ALIGN_TOP;
2997                                 }
2998                                 
2999                                 if ((flag & UI_BUT_ALIGN_TOP) == 0) {  /* stil top row */
3000                                         if (prev) {
3001                                                 if (next && buts_are_horiz(but, next))
3002                                                         flag = UI_BUT_ALIGN_DOWN | UI_BUT_ALIGN_LEFT | UI_BUT_ALIGN_RIGHT;
3003                                                 else {
3004                                                         /* last button in top row */
3005                                                         flag = UI_BUT_ALIGN_DOWN | UI_BUT_ALIGN_LEFT;
3006                                                 }
3007                                         }
3008                                         else 
3009                                                 flag |= UI_BUT_ALIGN_DOWN;
3010                                 }
3011                                 else 
3012                                         flag |= UI_BUT_ALIGN_TOP;
3013                         }
3014                 }
3015                 
3016                 but->drawflag |= flag;
3017                 
3018                 /* merge coordinates */
3019                 if (prev) {
3020                         /* simple cases */
3021                         if (rows == 0) {
3022                                 but->rect.xmin = (prev->rect.xmax + but->rect.xmin) / 2.0f;
3023                                 prev->rect.xmax = but->rect.xmin;
3024                         }
3025                         else if (cols == 0) {
3026                                 but->rect.ymax = (prev->rect.ymin + but->rect.ymax) / 2.0f;
3027                                 prev->rect.ymin = but->rect.ymax;
3028                         }
3029                         else {
3030                                 if (buts_are_horiz(prev, but)) {
3031                                         but->rect.xmin = (prev->rect.xmax + but->rect.xmin) / 2.0f;
3032                                         prev->rect.xmax = but->rect.xmin;
3033                                         /* copy height too */
3034                                         but->rect.ymax = prev->rect.ymax;
3035                                 }
3036                                 else if (prev->prev && buts_are_horiz(prev->prev, prev) == 0) {
3037                                         /* the previous button is a single one in its row */
3038                                         but->rect.ymax = (prev->rect.ymin + but->rect.ymax) / 2.0f;
3039                                         prev->rect.ymin = but->rect.ymax;
3040                                         
3041                                         but->rect.xmin = prev->rect.xmin;
3042                                         if (next && buts_are_horiz(but, next) == 0)
3043                                                 but->rect.xmax = prev->rect.xmax;
3044                                 }
3045                                 else {
3046                                         /* the previous button is not a single one in its row */
3047                                         but->rect.ymax = prev->rect.ymin;
3048                                 }
3049                         }
3050                 }
3051         }
3052 }
3053
3054 void ui_block_align_calc(uiBlock *block)
3055 {
3056         uiBut *but;
3057         short nr;
3058
3059         /* align buttons with same align nr */
3060         for (but = block->buttons.first; but; ) {
3061                 if (but->alignnr) {
3062                         nr = but->alignnr;
3063                         ui_block_align_calc_but(but, nr);
3064
3065                         /* skip with same number */
3066                         for (; but && but->alignnr == nr; but = but->next) {
3067                                 /* pass */
3068                         }
3069
3070                         if (!but) {
3071                                 break;
3072                         }
3073                 }
3074                 else {
3075                         but = but->next;
3076                 }
3077         }
3078 }
3079
3080 struct ColorManagedDisplay *ui_block_cm_display_get(uiBlock *block)
3081 {
3082         return IMB_colormanagement_display_get_named(block->display_device);
3083 }
3084
3085 void ui_block_cm_to_display_space_v3(uiBlock *block, float pixel[3])
3086 {
3087         struct ColorManagedDisplay *display = ui_block_cm_display_get(block);
3088
3089         IMB_colormanagement_scene_linear_to_display_v3(pixel, display);