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