b4a120b2dea785028676c8f3659d39db8842ac96
[blender.git] / source / blender / editors / interface / view2d.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) 2008 Blender Foundation.
19  * All rights reserved.
20  * 
21  * Contributor(s): Blender Foundation, Joshua Leung
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  */
25
26 /** \file blender/editors/interface/view2d.c
27  *  \ingroup edinterface
28  */
29
30
31 #include <float.h>
32 #include <limits.h>
33 #include <math.h>
34 #include <string.h>
35
36 #include "MEM_guardedalloc.h"
37
38 #include "DNA_scene_types.h"
39 #include "DNA_userdef_types.h"
40
41 #include "BLI_utildefines.h"
42 #include "BLI_link_utils.h"
43 #include "BLI_rect.h"
44 #include "BLI_math.h"
45 #include "BLI_memarena.h"
46 #include "BLI_timecode.h"
47
48 #include "BKE_context.h"
49 #include "BKE_screen.h"
50 #include "BKE_global.h"
51
52
53 #include "WM_api.h"
54
55 #include "BIF_gl.h"
56
57 #include "BLF_api.h"
58
59 #include "ED_screen.h"
60
61 #include "UI_interface.h"
62 #include "UI_view2d.h"
63
64 #include "interface_intern.h"
65
66 static void ui_view2d_curRect_validate_resize(View2D *v2d, int resize, int mask_scrollers);
67
68 /* *********************************************************************** */
69
70 BLI_INLINE int clamp_float_to_int(const float f)
71 {
72         const float min = INT_MIN;
73         const float max = INT_MAX;
74
75         if (UNLIKELY(f < min)) {
76                 return min;
77         }
78         else if (UNLIKELY(f > max)) {
79                 return max;
80         }
81         else {
82                 return (int)f;
83         }
84 }
85
86 /**
87  * use instead of #BLI_rcti_rctf_copy so we have consistent behavior
88  * with users of #clamp_float_to_int.
89  */
90 BLI_INLINE void clamp_rctf_to_rcti(rcti *dst, const rctf *src)
91 {
92         dst->xmin = clamp_float_to_int(src->xmin);
93         dst->xmax = clamp_float_to_int(src->xmax);
94         dst->ymin = clamp_float_to_int(src->ymin);
95         dst->ymax = clamp_float_to_int(src->ymax);
96 }
97
98
99 /* XXX still unresolved: scrolls hide/unhide vs region mask handling */
100 /* XXX there's V2D_SCROLL_HORIZONTAL_HIDE and V2D_SCROLL_HORIZONTAL_FULLR ... */
101
102 /* helper to allow scrollbars to dynamically hide
103  *  - returns a copy of the scrollbar settings with the flags to display
104  *        horizontal/vertical scrollbars removed
105  *      - input scroll value is the v2d->scroll var
106  *      - hide flags are set per region at drawtime
107  */
108 static int view2d_scroll_mapped(int scroll)
109 {
110         if (scroll & V2D_SCROLL_HORIZONTAL_FULLR)
111                 scroll &= ~(V2D_SCROLL_HORIZONTAL);
112         if (scroll & V2D_SCROLL_VERTICAL_FULLR)
113                 scroll &= ~(V2D_SCROLL_VERTICAL);
114         return scroll;
115 }
116
117 /* called each time cur changes, to dynamically update masks */
118 static void view2d_masks(View2D *v2d, int check_scrollers)
119 {
120         int scroll;
121         
122         /* mask - view frame */
123         v2d->mask.xmin = v2d->mask.ymin = 0;
124         v2d->mask.xmax = v2d->winx - 1; /* -1 yes! masks are pixels */
125         v2d->mask.ymax = v2d->winy - 1;
126
127         if (check_scrollers) {
128                 /* check size if hiding flag is set: */
129                 if (v2d->scroll & V2D_SCROLL_HORIZONTAL_HIDE) {
130                         if (!(v2d->scroll & V2D_SCROLL_SCALE_HORIZONTAL)) {
131                                 if (BLI_rctf_size_x(&v2d->tot) > BLI_rctf_size_x(&v2d->cur))
132                                         v2d->scroll &= ~V2D_SCROLL_HORIZONTAL_FULLR;
133                                 else
134                                         v2d->scroll |= V2D_SCROLL_HORIZONTAL_FULLR;
135                         }
136                 }
137                 if (v2d->scroll & V2D_SCROLL_VERTICAL_HIDE) {
138                         if (!(v2d->scroll & V2D_SCROLL_SCALE_VERTICAL)) {
139                                 if (BLI_rctf_size_y(&v2d->tot) + 0.01f > BLI_rctf_size_y(&v2d->cur))
140                                         v2d->scroll &= ~V2D_SCROLL_VERTICAL_FULLR;
141                                 else
142                                         v2d->scroll |= V2D_SCROLL_VERTICAL_FULLR;
143                         }
144                 }
145         }
146         
147         scroll = view2d_scroll_mapped(v2d->scroll);
148         
149         /* scrollers shrink mask area, but should be based off regionsize 
150          *      - they can only be on one to two edges of the region they define
151          *      - if they overlap, they must not occupy the corners (which are reserved for other widgets)
152          */
153         if (scroll) {
154                 /* vertical scroller */
155                 if (scroll & V2D_SCROLL_LEFT) {
156                         /* on left-hand edge of region */
157                         v2d->vert = v2d->mask;
158                         v2d->vert.xmax = V2D_SCROLL_WIDTH;
159                         v2d->mask.xmin = v2d->vert.xmax + 1;
160                 }
161                 else if (scroll & V2D_SCROLL_RIGHT) {
162                         /* on right-hand edge of region */
163                         v2d->vert = v2d->mask;
164                         v2d->vert.xmax++; /* one pixel extra... was leaving a minor gap... */
165                         v2d->vert.xmin = v2d->vert.xmax - V2D_SCROLL_WIDTH;
166                         v2d->mask.xmax = v2d->vert.xmin - 1;
167                 }
168                 
169                 /* horizontal scroller */
170                 if (scroll & (V2D_SCROLL_BOTTOM)) {
171                         /* on bottom edge of region */
172                         v2d->hor = v2d->mask;
173                         v2d->hor.ymax = V2D_SCROLL_HEIGHT;
174                         v2d->mask.ymin = v2d->hor.ymax + 1;
175                 }
176                 else if (scroll & V2D_SCROLL_TOP) {
177                         /* on upper edge of region */
178                         v2d->hor = v2d->mask;
179                         v2d->hor.ymin = v2d->hor.ymax - V2D_SCROLL_HEIGHT;
180                         v2d->mask.ymax = v2d->hor.ymin - 1;
181                 }
182                 
183                 /* adjust vertical scroller if there's a horizontal scroller, to leave corner free */
184                 if (scroll & V2D_SCROLL_VERTICAL) {
185                         /* just set y min/max for vertical scroller to y min/max of mask as appropriate */
186                         if (scroll & (V2D_SCROLL_BOTTOM)) {
187                                 /* on bottom edge of region */
188                                 v2d->vert.ymin = v2d->mask.ymin;
189                         }
190                         else if (scroll & V2D_SCROLL_TOP) {
191                                 /* on upper edge of region */
192                                 v2d->vert.ymax = v2d->mask.ymax;
193                         }
194                 }
195         }
196 }
197
198 /* Refresh and Validation */
199
200 /* Initialize all relevant View2D data (including view rects if first time) and/or refresh mask sizes after view resize
201  *      - for some of these presets, it is expected that the region will have defined some
202  *    additional settings necessary for the customization of the 2D viewport to its requirements
203  *      - this function should only be called from region init() callbacks, where it is expected that
204  *        this is called before UI_view2d_size_update(), as this one checks that the rects are properly initialized. 
205  */
206 void UI_view2d_region_reinit(View2D *v2d, short type, int winx, int winy)
207 {
208         bool tot_changed = false, do_init;
209         uiStyle *style = UI_style_get();
210
211         do_init = (v2d->flag & V2D_IS_INITIALISED) == 0;
212                 
213         /* see eView2D_CommonViewTypes in UI_view2d.h for available view presets */
214         switch (type) {
215                 /* 'standard view' - optimum setup for 'standard' view behavior,
216                  *  that should be used new views as basis for their
217                  *  own unique View2D settings, which should be used instead of this in most cases...
218                  */
219                 case V2D_COMMONVIEW_STANDARD:
220                 {
221                         /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
222                         v2d->keepzoom = (V2D_KEEPASPECT | V2D_LIMITZOOM);
223                         v2d->minzoom = 0.01f;
224                         v2d->maxzoom = 1000.0f;
225                         
226                         /* tot rect and cur should be same size, and aligned using 'standard' OpenGL coordinates for now 
227                          *      - region can resize 'tot' later to fit other data
228                          *      - keeptot is only within bounds, as strict locking is not that critical
229                          *      - view is aligned for (0,0) -> (winx-1, winy-1) setup
230                          */
231                         v2d->align = (V2D_ALIGN_NO_NEG_X | V2D_ALIGN_NO_NEG_Y);
232                         v2d->keeptot = V2D_KEEPTOT_BOUNDS;
233                         
234                         if (do_init) {
235                                 v2d->tot.xmin = v2d->tot.ymin = 0.0f;
236                                 v2d->tot.xmax = (float)(winx - 1);
237                                 v2d->tot.ymax = (float)(winy - 1);
238                                 
239                                 v2d->cur = v2d->tot;
240                         }
241                         /* scrollers - should we have these by default? */
242                         /* XXX for now, we don't override this, or set it either! */
243                         break;
244                 }
245                 /* 'list/channel view' - zoom, aspect ratio, and alignment restrictions are set here */
246                 case V2D_COMMONVIEW_LIST:
247                 {
248                         /* zoom + aspect ratio are locked */
249                         v2d->keepzoom = (V2D_LOCKZOOM_X | V2D_LOCKZOOM_Y | V2D_LIMITZOOM | V2D_KEEPASPECT);
250                         v2d->minzoom = v2d->maxzoom = 1.0f;
251                         
252                         /* tot rect has strictly regulated placement, and must only occur in +/- quadrant */
253                         v2d->align = (V2D_ALIGN_NO_NEG_X | V2D_ALIGN_NO_POS_Y);
254                         v2d->keeptot = V2D_KEEPTOT_STRICT;
255                         tot_changed = do_init;
256                         
257                         /* scroller settings are currently not set here... that is left for regions... */
258                         break;
259                 }
260                 /* 'stack view' - practically the same as list/channel view, except is located in the pos y half instead. 
261                  *  zoom, aspect ratio, and alignment restrictions are set here */
262                 case V2D_COMMONVIEW_STACK:
263                 {
264                         /* zoom + aspect ratio are locked */
265                         v2d->keepzoom = (V2D_LOCKZOOM_X | V2D_LOCKZOOM_Y | V2D_LIMITZOOM | V2D_KEEPASPECT);
266                         v2d->minzoom = v2d->maxzoom = 1.0f;
267                         
268                         /* tot rect has strictly regulated placement, and must only occur in +/+ quadrant */
269                         v2d->align = (V2D_ALIGN_NO_NEG_X | V2D_ALIGN_NO_NEG_Y);
270                         v2d->keeptot = V2D_KEEPTOT_STRICT;
271                         tot_changed = do_init;
272                         
273                         /* scroller settings are currently not set here... that is left for regions... */
274                         break;
275                 }
276                 /* 'header' regions - zoom, aspect ratio, alignment, and panning restrictions are set here */
277                 case V2D_COMMONVIEW_HEADER:
278                 {
279                         /* zoom + aspect ratio are locked */
280                         v2d->keepzoom = (V2D_LOCKZOOM_X | V2D_LOCKZOOM_Y | V2D_LIMITZOOM | V2D_KEEPASPECT);
281                         v2d->minzoom = v2d->maxzoom = 1.0f;
282                         
283                         if (do_init) {
284                                 v2d->tot.xmin = 0.0f;
285                                 v2d->tot.xmax = winx;
286                                 v2d->tot.ymin = 0.0f;
287                                 v2d->tot.ymax = winy;
288                                 v2d->cur = v2d->tot;
289                                 
290                                 v2d->min[0] = v2d->max[0] = (float)(winx - 1);
291                                 v2d->min[1] = v2d->max[1] = (float)(winy - 1);
292                         }
293                         /* tot rect has strictly regulated placement, and must only occur in +/+ quadrant */
294                         v2d->align = (V2D_ALIGN_NO_NEG_X | V2D_ALIGN_NO_NEG_Y);
295                         v2d->keeptot = V2D_KEEPTOT_STRICT;
296                         tot_changed = do_init;
297                         
298                         /* panning in y-axis is prohibited */
299                         v2d->keepofs = V2D_LOCKOFS_Y;
300                         
301                         /* absolutely no scrollers allowed */
302                         v2d->scroll = 0;
303                         break;
304                 }
305                 /* panels view, with horizontal/vertical align */
306                 case V2D_COMMONVIEW_PANELS_UI:
307                 {
308                         
309                         /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
310                         v2d->keepzoom = (V2D_KEEPASPECT | V2D_LIMITZOOM | V2D_KEEPZOOM);
311                         v2d->minzoom = 0.5f;
312                         v2d->maxzoom = 2.0f;
313                         
314                         v2d->align = (V2D_ALIGN_NO_NEG_X | V2D_ALIGN_NO_POS_Y);
315                         v2d->keeptot = V2D_KEEPTOT_BOUNDS;
316                         
317                         /* note, scroll is being flipped in ED_region_panels() drawing */
318                         v2d->scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
319                         v2d->scroll |= V2D_SCROLL_VERTICAL_HIDE;
320
321                         if (do_init) {
322                                 float panelzoom = (style) ? style->panelzoom : 1.0f;
323                                 float scrolw = v2d->scroll & V2D_SCROLL_RIGHT ? V2D_SCROLL_WIDTH : 0.0f;
324                                 
325                                 v2d->tot.xmin = 0.0f;
326                                 v2d->tot.xmax = winx - scrolw;
327                                 
328                                 v2d->tot.ymax = 0.0f;
329                                 v2d->tot.ymin = -winy;
330                                 
331                                 v2d->cur.xmin = 0.0f;
332                                 v2d->cur.xmax = (winx) * panelzoom - scrolw;
333                                 
334                                 v2d->cur.ymax = 0.0f;
335                                 v2d->cur.ymin = (-winy) * panelzoom;
336                         }
337                         break;
338                 }
339                 /* other view types are completely defined using their own settings already */
340                 default:
341                         /* we don't do anything here, as settings should be fine, but just make sure that rect */
342                         break;
343         }
344         
345         /* set initialized flag so that View2D doesn't get reinitialised next time again */
346         v2d->flag |= V2D_IS_INITIALISED;
347
348         /* store view size */
349         v2d->winx = winx;
350         v2d->winy = winy;
351         
352         /* set masks (always do), but leave scroller scheck to totrect_set */
353         view2d_masks(v2d, 0);
354         
355         /* set 'tot' rect before setting cur? */
356         /* XXX confusing stuff here still - I made this function not check scroller hide - that happens in totrect_set */
357         if (tot_changed)
358                 UI_view2d_totRect_set_resize(v2d, winx, winy, !do_init);
359         else
360                 ui_view2d_curRect_validate_resize(v2d, !do_init, 0);
361         
362 }
363
364 /* Ensure View2D rects remain in a viable configuration 
365  *      - cur is not allowed to be: larger than max, smaller than min, or outside of tot
366  */
367 // XXX pre2.5 -> this used to be called  test_view2d()
368 static void ui_view2d_curRect_validate_resize(View2D *v2d, int resize, int mask_scrollers)
369 {
370         float totwidth, totheight, curwidth, curheight, width, height;
371         float winx, winy;
372         rctf *cur, *tot;
373         
374         /* use mask as size of region that View2D resides in, as it takes into account
375          * scrollbars already - keep in sync with zoomx/zoomy in view_zoomstep_apply_ex! */
376         winx = (float)(BLI_rcti_size_x(&v2d->mask) + 1);
377         winy = (float)(BLI_rcti_size_y(&v2d->mask) + 1);
378         
379         /* get pointers to rcts for less typing */
380         cur = &v2d->cur;
381         tot = &v2d->tot;
382         
383         /* we must satisfy the following constraints (in decreasing order of importance):
384          *      - alignment restrictions are respected
385          *      - cur must not fall outside of tot
386          *      - axis locks (zoom and offset) must be maintained
387          *      - zoom must not be excessive (check either sizes or zoom values)
388          *      - aspect ratio should be respected (NOTE: this is quite closely related to zoom too)
389          */
390         
391         /* Step 1: if keepzoom, adjust the sizes of the rects only
392          *      - firstly, we calculate the sizes of the rects
393          *      - curwidth and curheight are saved as reference... modify width and height values here
394          */
395         totwidth  = BLI_rctf_size_x(tot);
396         totheight = BLI_rctf_size_y(tot);
397         /* keep in sync with zoomx/zoomy in view_zoomstep_apply_ex! */
398         curwidth  = width  = BLI_rctf_size_x(cur);
399         curheight = height = BLI_rctf_size_y(cur);
400         
401         /* if zoom is locked, size on the appropriate axis is reset to mask size */
402         if (v2d->keepzoom & V2D_LOCKZOOM_X)
403                 width = winx;
404         if (v2d->keepzoom & V2D_LOCKZOOM_Y)
405                 height = winy;
406                 
407         /* values used to divide, so make it safe 
408          * NOTE: width and height must use FLT_MIN instead of 1, otherwise it is impossible to
409          *       get enough resolution in Graph Editor for editing some curves
410          */
411         if (width < FLT_MIN) width = 1;
412         if (height < FLT_MIN) height = 1;
413         if (winx < 1) winx = 1;
414         if (winy < 1) winy = 1;
415         
416         /* V2D_LIMITZOOM indicates that zoom level should be preserved when the window size changes */
417         if (resize && (v2d->keepzoom & V2D_KEEPZOOM)) {
418                 float zoom, oldzoom;
419
420                 if ((v2d->keepzoom & V2D_LOCKZOOM_X) == 0) {
421                         zoom = winx / width;
422                         oldzoom = v2d->oldwinx / curwidth;
423
424                         if (oldzoom != zoom)
425                                 width *= zoom / oldzoom;
426                 }
427
428                 if ((v2d->keepzoom & V2D_LOCKZOOM_Y) == 0) {
429                         zoom = winy / height;
430                         oldzoom = v2d->oldwiny / curheight;
431
432                         if (oldzoom != zoom)
433                                 height *= zoom / oldzoom;
434                 }
435         }
436         /* keepzoom (V2D_LIMITZOOM set), indicates that zoom level on each axis must not exceed limits 
437          * NOTE: in general, it is not expected that the lock-zoom will be used in conjunction with this
438          */
439         else if (v2d->keepzoom & V2D_LIMITZOOM) {
440                 
441                 /* check if excessive zoom on x-axis */
442                 if ((v2d->keepzoom & V2D_LOCKZOOM_X) == 0) {
443                         const float zoom = winx / width;
444                         if (zoom < v2d->minzoom) {
445                                 width = winx / v2d->minzoom;
446                         }
447                         else if (zoom > v2d->maxzoom) {
448                                 width = winx / v2d->maxzoom;
449                         }
450                 }
451                 
452                 /* check if excessive zoom on y-axis */
453                 if ((v2d->keepzoom & V2D_LOCKZOOM_Y) == 0) {
454                         const float zoom = winy / height;
455                         if (zoom < v2d->minzoom) {
456                                 height = winy / v2d->minzoom;
457                         }
458                         else if (zoom > v2d->maxzoom) {
459                                 height = winy / v2d->maxzoom;
460                         }
461                 }
462         }
463         else {
464                 /* make sure sizes don't exceed that of the min/max sizes (even though we're not doing zoom clamping) */
465                 CLAMP(width, v2d->min[0], v2d->max[0]);
466                 CLAMP(height, v2d->min[1], v2d->max[1]);
467         }
468         
469         /* check if we should restore aspect ratio (if view size changed) */
470         if (v2d->keepzoom & V2D_KEEPASPECT) {
471                 bool do_x = false, do_y = false, do_cur /* , do_win */ /* UNUSED */;
472                 float /* curRatio, */ /* UNUSED */ winRatio;
473                 
474                 /* when a window edge changes, the aspect ratio can't be used to
475                  * find which is the best new 'cur' rect. thats why it stores 'old' 
476                  */
477                 if (winx != v2d->oldwinx) do_x = true;
478                 if (winy != v2d->oldwiny) do_y = true;
479                 
480                 /* curRatio = height / width; */ /* UNUSED */
481                 winRatio = winy / winx;
482                 
483                 /* both sizes change (area/region maximized)  */
484                 if (do_x == do_y) {
485                         if (do_x && do_y) {
486                                 /* here is 1,1 case, so all others must be 0,0 */
487                                 if (fabsf(winx - v2d->oldwinx) > fabsf(winy - v2d->oldwiny)) do_y = false;
488                                 else do_x = false;
489                         }
490                         else if (winRatio > 1.0f) {
491                                 do_x = false;
492                         }
493                         else {
494                                 do_x = true;
495                         }
496                 }
497                 do_cur = do_x;
498                 /* do_win = do_y; */ /* UNUSED */
499                 
500                 if (do_cur) {
501                         if ((v2d->keeptot == V2D_KEEPTOT_STRICT) && (winx != v2d->oldwinx)) {
502                                 /* special exception for Outliner (and later channel-lists):
503                                  *  - The view may be moved left to avoid contents being pushed out of view when view shrinks.
504                                  *      - The keeptot code will make sure cur->xmin will not be less than tot->xmin (which cannot be allowed)
505                                  *      - width is not adjusted for changed ratios here...
506                                  */
507                                 if (winx < v2d->oldwinx) {
508                                         float temp = v2d->oldwinx - winx;
509                                         
510                                         cur->xmin -= temp;
511                                         cur->xmax -= temp;
512                                         
513                                         /* width does not get modified, as keepaspect here is just set to make 
514                                          * sure visible area adjusts to changing view shape! 
515                                          */
516                                 }
517                         }
518                         else {
519                                 /* portrait window: correct for x */
520                                 width = height / winRatio;
521                         }
522                 }
523                 else {
524                         if ((v2d->keeptot == V2D_KEEPTOT_STRICT) && (winy != v2d->oldwiny)) {
525                                 /* special exception for Outliner (and later channel-lists):
526                                  *      - Currently, no actions need to be taken here...
527                                  */
528
529                                 if (winy < v2d->oldwiny) {
530                                         float temp = v2d->oldwiny - winy;
531                                         
532                                         if (v2d->align & V2D_ALIGN_NO_NEG_Y) {
533                                                 cur->ymin -= temp;
534                                                 cur->ymax -= temp;
535                                         }
536                                         else { /* Assume V2D_ALIGN_NO_POS_Y or combination */
537                                                 cur->ymin += temp;
538                                                 cur->ymax += temp;
539                                         }
540                                 }
541
542                         }
543                         else {
544                                 /* landscape window: correct for y */
545                                 height = width * winRatio;
546                         }
547                 }
548                 
549                 /* store region size for next time */
550                 v2d->oldwinx = (short)winx;
551                 v2d->oldwiny = (short)winy;
552         }
553         
554         /* Step 2: apply new sizes to cur rect, but need to take into account alignment settings here... */
555         if ((width != curwidth) || (height != curheight)) {
556                 float temp, dh;
557                 
558                 /* resize from centerpoint, unless otherwise specified */
559                 if (width != curwidth) {
560                         if (v2d->keepofs & V2D_LOCKOFS_X) {
561                                 cur->xmax += width - BLI_rctf_size_x(cur);
562                         }
563                         else if (v2d->keepofs & V2D_KEEPOFS_X) {
564                                 if (v2d->align & V2D_ALIGN_NO_POS_X)
565                                         cur->xmin -= width - BLI_rctf_size_x(cur);
566                                 else
567                                         cur->xmax += width - BLI_rctf_size_x(cur);
568                         }
569                         else {
570                                 temp = BLI_rctf_cent_x(cur);
571                                 dh = width * 0.5f;
572                                 
573                                 cur->xmin = temp - dh;
574                                 cur->xmax = temp + dh;
575                         }
576                 }
577                 if (height != curheight) {
578                         if (v2d->keepofs & V2D_LOCKOFS_Y) {
579                                 cur->ymax += height - BLI_rctf_size_y(cur);
580                         }
581                         else if (v2d->keepofs & V2D_KEEPOFS_Y) {
582                                 if (v2d->align & V2D_ALIGN_NO_POS_Y)
583                                         cur->ymin -= height - BLI_rctf_size_y(cur);
584                                 else
585                                         cur->ymax += height - BLI_rctf_size_y(cur);
586                         }
587                         else {
588                                 temp = BLI_rctf_cent_y(cur);
589                                 dh = height * 0.5f;
590                                 
591                                 cur->ymin = temp - dh;
592                                 cur->ymax = temp + dh;
593                         }
594                 }
595         }
596         
597         /* Step 3: adjust so that it doesn't fall outside of bounds of 'tot' */
598         if (v2d->keeptot) {
599                 float temp, diff;
600                 
601                 /* recalculate extents of cur */
602                 curwidth  = BLI_rctf_size_x(cur);
603                 curheight = BLI_rctf_size_y(cur);
604                 
605                 /* width */
606                 if ((curwidth > totwidth) && !(v2d->keepzoom & (V2D_KEEPZOOM | V2D_LOCKZOOM_X | V2D_LIMITZOOM))) {
607                         /* if zoom doesn't have to be maintained, just clamp edges */
608                         if (cur->xmin < tot->xmin) cur->xmin = tot->xmin;
609                         if (cur->xmax > tot->xmax) cur->xmax = tot->xmax;
610                 }
611                 else if (v2d->keeptot == V2D_KEEPTOT_STRICT) {
612                         /* This is an exception for the outliner (and later channel-lists, headers) 
613                          *      - must clamp within tot rect (absolutely no excuses)
614                          *      --> therefore, cur->xmin must not be less than tot->xmin
615                          */
616                         if (cur->xmin < tot->xmin) {
617                                 /* move cur across so that it sits at minimum of tot */
618                                 temp = tot->xmin - cur->xmin;
619                                 
620                                 cur->xmin += temp;
621                                 cur->xmax += temp;
622                         }
623                         else if (cur->xmax > tot->xmax) {
624                                 /* - only offset by difference of cur-xmax and tot-xmax if that would not move 
625                                  *  cur-xmin to lie past tot-xmin
626                                  * - otherwise, simply shift to tot-xmin???
627                                  */
628                                 temp = cur->xmax - tot->xmax;
629                                 
630                                 if ((cur->xmin - temp) < tot->xmin) {
631                                         /* only offset by difference from cur-min and tot-min */
632                                         temp = cur->xmin - tot->xmin;
633                                         
634                                         cur->xmin -= temp;
635                                         cur->xmax -= temp;
636                                 }
637                                 else {
638                                         cur->xmin -= temp;
639                                         cur->xmax -= temp;
640                                 }
641                         }
642                 }
643                 else {
644                         /* This here occurs when:
645                          *  - width too big, but maintaining zoom (i.e. widths cannot be changed)
646                          *      - width is OK, but need to check if outside of boundaries
647                          * 
648                          * So, resolution is to just shift view by the gap between the extremities.
649                          * We favour moving the 'minimum' across, as that's origin for most things
650                          * (XXX - in the past, max was favored... if there are bugs, swap!)
651                          */
652                         if ((cur->xmin < tot->xmin) && (cur->xmax > tot->xmax)) {
653                                 /* outside boundaries on both sides, so take middle-point of tot, and place in balanced way */
654                                 temp = BLI_rctf_cent_x(tot);
655                                 diff = curwidth * 0.5f;
656                                 
657                                 cur->xmin = temp - diff;
658                                 cur->xmax = temp + diff;
659                         }
660                         else if (cur->xmin < tot->xmin) {
661                                 /* move cur across so that it sits at minimum of tot */
662                                 temp = tot->xmin - cur->xmin;
663                                 
664                                 cur->xmin += temp;
665                                 cur->xmax += temp;
666                         }
667                         else if (cur->xmax > tot->xmax) {
668                                 /* - only offset by difference of cur-xmax and tot-xmax if that would not move 
669                                  *  cur-xmin to lie past tot-xmin
670                                  * - otherwise, simply shift to tot-xmin???
671                                  */
672                                 temp = cur->xmax - tot->xmax;
673                                 
674                                 if ((cur->xmin - temp) < tot->xmin) {
675                                         /* only offset by difference from cur-min and tot-min */
676                                         temp = cur->xmin - tot->xmin;
677                                         
678                                         cur->xmin -= temp;
679                                         cur->xmax -= temp;
680                                 }
681                                 else {
682                                         cur->xmin -= temp;
683                                         cur->xmax -= temp;
684                                 }
685                         }
686                 }
687                 
688                 /* height */
689                 if ((curheight > totheight) && !(v2d->keepzoom & (V2D_KEEPZOOM | V2D_LOCKZOOM_Y | V2D_LIMITZOOM))) {
690                         /* if zoom doesn't have to be maintained, just clamp edges */
691                         if (cur->ymin < tot->ymin) cur->ymin = tot->ymin;
692                         if (cur->ymax > tot->ymax) cur->ymax = tot->ymax;
693                 }
694                 else {
695                         /* This here occurs when:
696                          *  - height too big, but maintaining zoom (i.e. heights cannot be changed)
697                          *      - height is OK, but need to check if outside of boundaries
698                          * 
699                          * So, resolution is to just shift view by the gap between the extremities.
700                          * We favour moving the 'minimum' across, as that's origin for most things
701                          */
702                         if ((cur->ymin < tot->ymin) && (cur->ymax > tot->ymax)) {
703                                 /* outside boundaries on both sides, so take middle-point of tot, and place in balanced way */
704                                 temp = BLI_rctf_cent_y(tot);
705                                 diff = curheight * 0.5f;
706                                 
707                                 cur->ymin = temp - diff;
708                                 cur->ymax = temp + diff;
709                         }
710                         else if (cur->ymin < tot->ymin) {
711                                 /* there's still space remaining, so shift up */
712                                 temp = tot->ymin - cur->ymin;
713                                 
714                                 cur->ymin += temp;
715                                 cur->ymax += temp;
716                         }
717                         else if (cur->ymax > tot->ymax) {
718                                 /* there's still space remaining, so shift down */
719                                 temp = cur->ymax - tot->ymax;
720                                 
721                                 cur->ymin -= temp;
722                                 cur->ymax -= temp;
723                         }
724                 }
725         }
726         
727         /* Step 4: Make sure alignment restrictions are respected */
728         if (v2d->align) {
729                 /* If alignment flags are set (but keeptot is not), they must still be respected, as although
730                  * they don't specify any particular bounds to stay within, they do define ranges which are 
731                  * invalid.
732                  *
733                  * Here, we only check to make sure that on each axis, the 'cur' rect doesn't stray into these 
734                  * invalid zones, otherwise we offset.
735                  */
736                 
737                 /* handle width - posx and negx flags are mutually exclusive, so watch out */
738                 if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
739                         /* width is in negative-x half */
740                         if (v2d->cur.xmax > 0) {
741                                 v2d->cur.xmin -= v2d->cur.xmax;
742                                 v2d->cur.xmax = 0.0f;
743                         }
744                 }
745                 else if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) {
746                         /* width is in positive-x half */
747                         if (v2d->cur.xmin < 0) {
748                                 v2d->cur.xmax -= v2d->cur.xmin;
749                                 v2d->cur.xmin = 0.0f;
750                         }
751                 }
752                 
753                 /* handle height - posx and negx flags are mutually exclusive, so watch out */
754                 if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
755                         /* height is in negative-y half */
756                         if (v2d->cur.ymax > 0) {
757                                 v2d->cur.ymin -= v2d->cur.ymax;
758                                 v2d->cur.ymax = 0.0f;
759                         }
760                 }
761                 else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) {
762                         /* height is in positive-y half */
763                         if (v2d->cur.ymin < 0) {
764                                 v2d->cur.ymax -= v2d->cur.ymin;
765                                 v2d->cur.ymin = 0.0f;
766                         }
767                 }
768         }
769         
770         /* set masks */
771         view2d_masks(v2d, mask_scrollers);
772 }
773
774 void UI_view2d_curRect_validate(View2D *v2d)
775 {
776         ui_view2d_curRect_validate_resize(v2d, 0, 1);
777 }
778
779 /* ------------------ */
780
781 /* Called by menus to activate it, or by view2d operators to make sure 'related' views stay in synchrony */
782 void UI_view2d_sync(bScreen *screen, ScrArea *area, View2D *v2dcur, int flag)
783 {
784         ScrArea *sa;
785         ARegion *ar;
786         
787         /* don't continue if no view syncing to be done */
788         if ((v2dcur->flag & (V2D_VIEWSYNC_SCREEN_TIME | V2D_VIEWSYNC_AREA_VERTICAL)) == 0)
789                 return;
790                 
791         /* check if doing within area syncing (i.e. channels/vertical) */
792         if ((v2dcur->flag & V2D_VIEWSYNC_AREA_VERTICAL) && (area)) {
793                 for (ar = area->regionbase.first; ar; ar = ar->next) {
794                         /* don't operate on self */
795                         if (v2dcur != &ar->v2d) {
796                                 /* only if view has vertical locks enabled */
797                                 if (ar->v2d.flag & V2D_VIEWSYNC_AREA_VERTICAL) {
798                                         if (flag == V2D_LOCK_COPY) {
799                                                 /* other views with locks on must copy active */
800                                                 ar->v2d.cur.ymin = v2dcur->cur.ymin;
801                                                 ar->v2d.cur.ymax = v2dcur->cur.ymax;
802                                         }
803                                         else { /* V2D_LOCK_SET */
804                                                    /* active must copy others */
805                                                 v2dcur->cur.ymin = ar->v2d.cur.ymin;
806                                                 v2dcur->cur.ymax = ar->v2d.cur.ymax;
807                                         }
808                                         
809                                         /* region possibly changed, so refresh */
810                                         ED_region_tag_redraw(ar);
811                                 }
812                         }
813                 }
814         }
815         
816         /* check if doing whole screen syncing (i.e. time/horizontal) */
817         if ((v2dcur->flag & V2D_VIEWSYNC_SCREEN_TIME) && (screen)) {
818                 for (sa = screen->areabase.first; sa; sa = sa->next) {
819                         for (ar = sa->regionbase.first; ar; ar = ar->next) {
820                                 /* don't operate on self */
821                                 if (v2dcur != &ar->v2d) {
822                                         /* only if view has horizontal locks enabled */
823                                         if (ar->v2d.flag & V2D_VIEWSYNC_SCREEN_TIME) {
824                                                 if (flag == V2D_LOCK_COPY) {
825                                                         /* other views with locks on must copy active */
826                                                         ar->v2d.cur.xmin = v2dcur->cur.xmin;
827                                                         ar->v2d.cur.xmax = v2dcur->cur.xmax;
828                                                 }
829                                                 else { /* V2D_LOCK_SET */
830                                                            /* active must copy others */
831                                                         v2dcur->cur.xmin = ar->v2d.cur.xmin;
832                                                         v2dcur->cur.xmax = ar->v2d.cur.xmax;
833                                                 }
834                                                 
835                                                 /* region possibly changed, so refresh */
836                                                 ED_region_tag_redraw(ar);
837                                         }
838                                 }
839                         }
840                 }
841         }
842 }
843
844
845 /* Restore 'cur' rect to standard orientation (i.e. optimal maximum view of tot) 
846  * This does not take into account if zooming the view on an axis will improve the view (if allowed)
847  */
848 void UI_view2d_curRect_reset(View2D *v2d)
849 {
850         float width, height;
851         
852         /* assume width and height of 'cur' rect by default, should be same size as mask */
853         width  = (float)(BLI_rcti_size_x(&v2d->mask) + 1);
854         height = (float)(BLI_rcti_size_y(&v2d->mask) + 1);
855         
856         /* handle width - posx and negx flags are mutually exclusive, so watch out */
857         if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
858                 /* width is in negative-x half */
859                 v2d->cur.xmin = -width;
860                 v2d->cur.xmax = 0.0f;
861         }
862         else if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) {
863                 /* width is in positive-x half */
864                 v2d->cur.xmin = 0.0f;
865                 v2d->cur.xmax = width;
866         }
867         else {
868                 /* width is centered around (x == 0) */
869                 const float dx = width / 2.0f;
870                 
871                 v2d->cur.xmin = -dx;
872                 v2d->cur.xmax = dx;
873         }
874         
875         /* handle height - posx and negx flags are mutually exclusive, so watch out */
876         if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
877                 /* height is in negative-y half */
878                 v2d->cur.ymin = -height;
879                 v2d->cur.ymax = 0.0f;
880         }
881         else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) {
882                 /* height is in positive-y half */
883                 v2d->cur.ymin = 0.0f;
884                 v2d->cur.ymax = height;
885         }
886         else {
887                 /* height is centered around (y == 0) */
888                 const float dy = height / 2.0f;
889                 
890                 v2d->cur.ymin = -dy;
891                 v2d->cur.ymax = dy;
892         }
893 }
894
895 /* ------------------ */
896
897 /* Change the size of the maximum viewable area (i.e. 'tot' rect) */
898 void UI_view2d_totRect_set_resize(View2D *v2d, int width, int height, int resize)
899 {
900 //      int scroll = view2d_scroll_mapped(v2d->scroll);
901         
902         /* don't do anything if either value is 0 */
903         width = abs(width);
904         height = abs(height);
905         
906         /* hrumf! */
907         /* XXX: there are work arounds for this in the panel and file browse code. */
908         /* round to int, because this is called with width + V2D_SCROLL_WIDTH */
909 //      if (scroll & V2D_SCROLL_HORIZONTAL)
910 //              width -= (int)V2D_SCROLL_WIDTH;
911 //      if (scroll & V2D_SCROLL_VERTICAL)
912 //              height -= (int)V2D_SCROLL_HEIGHT;
913         
914         if (ELEM(0, width, height)) {
915                 if (G.debug & G_DEBUG)
916                         printf("Error: View2D totRect set exiting: v2d=%p width=%d height=%d\n", (void *)v2d, width, height);  // XXX temp debug info
917                 return;
918         }
919         
920         /* handle width - posx and negx flags are mutually exclusive, so watch out */
921         if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
922                 /* width is in negative-x half */
923                 v2d->tot.xmin = (float)-width;
924                 v2d->tot.xmax = 0.0f;
925         }
926         else if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) {
927                 /* width is in positive-x half */
928                 v2d->tot.xmin = 0.0f;
929                 v2d->tot.xmax = (float)width;
930         }
931         else {
932                 /* width is centered around (x == 0) */
933                 const float dx = (float)width / 2.0f;
934                 
935                 v2d->tot.xmin = -dx;
936                 v2d->tot.xmax = dx;
937         }
938         
939         /* handle height - posx and negx flags are mutually exclusive, so watch out */
940         if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
941                 /* height is in negative-y half */
942                 v2d->tot.ymin = (float)-height;
943                 v2d->tot.ymax = 0.0f;
944         }
945         else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) {
946                 /* height is in positive-y half */
947                 v2d->tot.ymin = 0.0f;
948                 v2d->tot.ymax = (float)height;
949         }
950         else {
951                 /* height is centered around (y == 0) */
952                 const float dy = (float)height / 2.0f;
953                 
954                 v2d->tot.ymin = -dy;
955                 v2d->tot.ymax = dy;
956         }
957         
958         /* make sure that 'cur' rect is in a valid state as a result of these changes */
959         ui_view2d_curRect_validate_resize(v2d, resize, 1);
960         
961 }
962
963 void UI_view2d_totRect_set(View2D *v2d, int width, int height)
964 {
965         int scroll = view2d_scroll_mapped(v2d->scroll);
966
967         UI_view2d_totRect_set_resize(v2d, width, height, 0);
968         
969         /* solve bad recursion... if scroller state changed, mask is different, so you get different rects */
970         if (scroll != view2d_scroll_mapped(v2d->scroll)) {
971                 UI_view2d_totRect_set_resize(v2d, width, height, 0);
972         }
973         
974 }
975
976 bool UI_view2d_tab_set(View2D *v2d, int tab)
977 {
978         float default_offset[2] = {0.0f, 0.0f};
979         float *offset, *new_offset;
980         bool changed = false;
981
982         /* if tab changed, change offset */
983         if (tab != v2d->tab_cur && v2d->tab_offset) {
984                 if (tab < v2d->tab_num)
985                         offset = &v2d->tab_offset[tab * 2];
986                 else
987                         offset = default_offset;
988
989                 v2d->cur.xmax += offset[0] - v2d->cur.xmin;
990                 v2d->cur.xmin = offset[0];
991
992                 v2d->cur.ymin += offset[1] - v2d->cur.ymax;
993                 v2d->cur.ymax = offset[1];
994
995                 /* validation should happen in subsequent totRect_set */
996
997                 changed = true;
998         }
999
1000         /* resize array if needed */
1001         if (tab >= v2d->tab_num) {
1002                 new_offset = MEM_callocN(sizeof(float) * (tab + 1) * 2, "view2d tab offset");
1003
1004                 if (v2d->tab_offset) {
1005                         memcpy(new_offset, v2d->tab_offset, sizeof(float) * v2d->tab_num * 2);
1006                         MEM_freeN(v2d->tab_offset);
1007                 }
1008
1009                 v2d->tab_offset = new_offset;
1010                 v2d->tab_num = tab + 1;
1011         }
1012
1013         /* set current tab and offset */
1014         v2d->tab_cur = tab;
1015         v2d->tab_offset[2 * tab + 0] = v2d->cur.xmin;
1016         v2d->tab_offset[2 * tab + 1] = v2d->cur.ymax;
1017
1018         return changed;
1019 }
1020
1021 void UI_view2d_zoom_cache_reset(void)
1022 {
1023         /* While scaling we can accumulate fonts at many sizes (~20 or so).
1024          * Not an issue with embedded font, but can use over 500Mb with i18n ones! See [#38244]. */
1025
1026         /* note: only some views draw text, we could check for this case to avoid clearning cache */
1027         BLF_cache_clear();
1028 }
1029
1030 /* *********************************************************************** */
1031 /* View Matrix Setup */
1032
1033 /* mapping function to ensure 'cur' draws extended over the area where sliders are */
1034 static void view2d_map_cur_using_mask(View2D *v2d, rctf *curmasked)
1035 {
1036         *curmasked = v2d->cur;
1037         
1038         if (view2d_scroll_mapped(v2d->scroll)) {
1039                 float sizex = BLI_rcti_size_x(&v2d->mask);
1040                 float sizey = BLI_rcti_size_y(&v2d->mask);
1041                 
1042                 /* prevent tiny or narrow regions to get invalid coordinates - mask can get negative even... */
1043                 if (sizex > 0.0f && sizey > 0.0f) {
1044                         float dx = BLI_rctf_size_x(&v2d->cur) / (sizex + 1);
1045                         float dy = BLI_rctf_size_y(&v2d->cur) / (sizey + 1);
1046                         
1047                         if (v2d->mask.xmin != 0)
1048                                 curmasked->xmin -= dx * (float)v2d->mask.xmin;
1049                         if (v2d->mask.xmax + 1 != v2d->winx)
1050                                 curmasked->xmax += dx * (float)(v2d->winx - v2d->mask.xmax - 1);
1051                         
1052                         if (v2d->mask.ymin != 0)
1053                                 curmasked->ymin -= dy * (float)v2d->mask.ymin;
1054                         if (v2d->mask.ymax + 1 != v2d->winy)
1055                                 curmasked->ymax += dy * (float)(v2d->winy - v2d->mask.ymax - 1);
1056                 }
1057         }
1058 }
1059
1060 /* Set view matrices to use 'cur' rect as viewing frame for View2D drawing */
1061 void UI_view2d_view_ortho(View2D *v2d)
1062 {
1063         rctf curmasked;
1064         const int sizex = BLI_rcti_size_x(&v2d->mask);
1065         const int sizey = BLI_rcti_size_y(&v2d->mask);
1066         const float eps = 0.001f;
1067         float xofs = 0.0f, yofs = 0.0f;
1068         
1069         /* pixel offsets (-GLA_PIXEL_OFS) are needed to get 1:1 correspondence with pixels for smooth UI drawing,
1070          * but only applied where requested
1071          */
1072         /* XXX brecht: instead of zero at least use a tiny offset, otherwise
1073          * pixel rounding is effectively random due to float inaccuracy */
1074         if (sizex > 0)
1075                 xofs = eps * BLI_rctf_size_x(&v2d->cur) / sizex;
1076         if (sizey > 0)
1077                 yofs = eps * BLI_rctf_size_y(&v2d->cur) / sizey;
1078         
1079         /* apply mask-based adjustments to cur rect (due to scrollers), to eliminate scaling artifacts */
1080         view2d_map_cur_using_mask(v2d, &curmasked);
1081
1082         BLI_rctf_translate(&curmasked, -xofs, -yofs);
1083         
1084         /* XXX ton: this flag set by outliner, for icons */
1085         if (v2d->flag & V2D_PIXELOFS_X) {
1086                 curmasked.xmin = floorf(curmasked.xmin) - (eps + xofs);
1087                 curmasked.xmax = floorf(curmasked.xmax) - (eps + xofs);
1088         }
1089         if (v2d->flag & V2D_PIXELOFS_Y) {
1090                 curmasked.ymin = floorf(curmasked.ymin) - (eps + yofs);
1091                 curmasked.ymax = floorf(curmasked.ymax) - (eps + yofs);
1092         }
1093         
1094         /* set matrix on all appropriate axes */
1095         wmOrtho2(curmasked.xmin, curmasked.xmax, curmasked.ymin, curmasked.ymax);
1096
1097         /* XXX is this necessary? */
1098         glLoadIdentity();
1099 }
1100
1101 /* Set view matrices to only use one axis of 'cur' only
1102  *      - xaxis     = if non-zero, only use cur x-axis, otherwise use cur-yaxis (mostly this will be used for x)
1103  */
1104 void UI_view2d_view_orthoSpecial(ARegion *ar, View2D *v2d, const bool xaxis)
1105 {
1106         rctf curmasked;
1107         float xofs, yofs;
1108         
1109         /* pixel offsets (-GLA_PIXEL_OFS) are needed to get 1:1 correspondence with pixels for smooth UI drawing,
1110          * but only applied where requested
1111          */
1112         /* XXX temp (ton) */
1113         xofs = 0.0f; // (v2d->flag & V2D_PIXELOFS_X) ? GLA_PIXEL_OFS : 0.0f;
1114         yofs = 0.0f; // (v2d->flag & V2D_PIXELOFS_Y) ? GLA_PIXEL_OFS : 0.0f;
1115         
1116         /* apply mask-based adjustments to cur rect (due to scrollers), to eliminate scaling artifacts */
1117         view2d_map_cur_using_mask(v2d, &curmasked);
1118         
1119         /* only set matrix with 'cur' coordinates on relevant axes */
1120         if (xaxis)
1121                 wmOrtho2(curmasked.xmin - xofs, curmasked.xmax - xofs, -yofs, ar->winy - yofs);
1122         else
1123                 wmOrtho2(-xofs, ar->winx - xofs, curmasked.ymin - yofs, curmasked.ymax - yofs);
1124                 
1125         /* XXX is this necessary? */
1126         glLoadIdentity();
1127
1128
1129
1130 /* Restore view matrices after drawing */
1131 void UI_view2d_view_restore(const bContext *C)
1132 {
1133         ARegion *ar = CTX_wm_region(C);
1134         int width  = BLI_rcti_size_x(&ar->winrct) + 1;
1135         int height = BLI_rcti_size_y(&ar->winrct) + 1;
1136         
1137         wmOrtho2(0.0f, (float)width, 0.0f, (float)height);
1138         glLoadIdentity();
1139         
1140         //      ED_region_pixelspace(CTX_wm_region(C));
1141 }
1142
1143 /* *********************************************************************** */
1144 /* Gridlines */
1145
1146 /* View2DGrid is typedef'd in UI_view2d.h */
1147 struct View2DGrid {
1148         float dx, dy;           /* stepsize (in pixels) between gridlines */
1149         float startx, starty;   /* initial coordinates to start drawing grid from */
1150         int powerx, powery;     /* step as power of 10 */
1151 };
1152
1153 /* --------------- */
1154
1155 /* try to write step as a power of 10 */
1156 static void step_to_grid(float *step, int *power, int unit)
1157 {
1158         const float loga = (float)log10(*step);
1159         float rem;
1160         
1161         *power = (int)(loga);
1162         
1163         rem = loga - (*power);
1164         rem = (float)pow(10.0, rem);
1165         
1166         if (loga < 0.0f) {
1167                 if (rem < 0.2f) rem = 0.2f;
1168                 else if (rem < 0.5f) rem = 0.5f;
1169                 else rem = 1.0f;
1170                 
1171                 *step = rem * (float)pow(10.0, (*power));
1172                 
1173                 /* for frames, we want 1.0 frame intervals only */
1174                 if (unit == V2D_UNIT_FRAMES) {
1175                         rem = 1.0f;
1176                         *step = 2.0f; /* use 2 since there are grid lines drawn in between, this way to get 1 line per frane */
1177                 }
1178                 
1179                 /* prevents printing 1.0 2.0 3.0 etc */
1180                 if (rem == 1.0f) (*power)++;
1181         }
1182         else {
1183                 if (rem < 2.0f) rem = 2.0f;
1184                 else if (rem < 5.0f) rem = 5.0f;
1185                 else rem = 10.0f;
1186                 
1187                 *step = rem * (float)pow(10.0, (*power));
1188                 
1189                 (*power)++;
1190                 /* prevents printing 1.0, 2.0, 3.0, etc. */
1191                 if (rem == 10.0f) (*power)++;
1192         }
1193 }
1194
1195 /* Initialize settings necessary for drawing gridlines in a 2d-view
1196  *      - Currently, will return pointer to View2DGrid struct that needs to 
1197  *        be freed with UI_view2d_grid_free()
1198  *      - Is used for scrollbar drawing too (for units drawing)
1199  *      - Units + clamping args will be checked, to make sure they are valid values that can be used
1200  *        so it is very possible that we won't return grid at all!
1201  *      
1202  *      - xunits,yunits = V2D_UNIT_*  grid steps in seconds or frames 
1203  *      - xclamp,yclamp = V2D_CLAMP_* only show whole-number intervals
1204  *      - winx                  = width of region we're drawing to, note: not used but keeping for completeness.
1205  *      - winy                  = height of region we're drawing into
1206  */
1207 View2DGrid *UI_view2d_grid_calc(Scene *scene, View2D *v2d,
1208                                 short xunits, short xclamp, short yunits, short yclamp, int UNUSED(winx), int winy)
1209 {
1210
1211         View2DGrid *grid;
1212         float space, pixels, seconddiv;
1213         
1214         /* check that there are at least some workable args */
1215         if (ELEM(V2D_ARG_DUMMY, xunits, xclamp) && ELEM(V2D_ARG_DUMMY, yunits, yclamp))
1216                 return NULL;
1217         
1218         /* grid here is allocated... */
1219         grid = MEM_callocN(sizeof(View2DGrid), "View2DGrid");
1220         
1221         /* rule: gridstep is minimal GRIDSTEP pixels */
1222         if (xunits == V2D_UNIT_SECONDS) {
1223                 seconddiv = (float)(0.01 * FPS);
1224         }
1225         else {
1226                 seconddiv = 1.0f;
1227         }
1228         
1229         /* calculate x-axis grid scale (only if both args are valid) */
1230         if (ELEM(V2D_ARG_DUMMY, xunits, xclamp) == 0) {
1231                 space = BLI_rctf_size_x(&v2d->cur);
1232                 pixels = (float)BLI_rcti_size_x(&v2d->mask);
1233                 
1234                 if (pixels != 0.0f) {
1235                         grid->dx = (U.v2d_min_gridsize * UI_DPI_FAC * space) / (seconddiv * pixels);
1236                         step_to_grid(&grid->dx, &grid->powerx, xunits);
1237                         grid->dx *= seconddiv;
1238                 }
1239                 
1240                 if (xclamp == V2D_GRID_CLAMP) {
1241                         if (grid->dx < 0.1f) grid->dx = 0.1f;
1242                         grid->powerx -= 2;
1243                         if (grid->powerx < -2) grid->powerx = -2;
1244                 }
1245         }
1246         
1247         /* calculate y-axis grid scale (only if both args are valid) */
1248         if (ELEM(V2D_ARG_DUMMY, yunits, yclamp) == 0) {
1249                 space = BLI_rctf_size_y(&v2d->cur);
1250                 pixels = (float)winy;
1251                 
1252                 grid->dy = U.v2d_min_gridsize * UI_DPI_FAC * space / pixels;
1253                 step_to_grid(&grid->dy, &grid->powery, yunits);
1254                 
1255                 if (yclamp == V2D_GRID_CLAMP) {
1256                         if (grid->dy < 1.0f) grid->dy = 1.0f;
1257                         if (grid->powery < 1) grid->powery = 1;
1258                 }
1259         }
1260         
1261         /* calculate start position */
1262         if (ELEM(V2D_ARG_DUMMY, xunits, xclamp) == 0) {
1263                 grid->startx = seconddiv * (v2d->cur.xmin / seconddiv - (float)fmod(v2d->cur.xmin / seconddiv, grid->dx / seconddiv));
1264                 if (v2d->cur.xmin < 0.0f) grid->startx -= grid->dx;
1265         }
1266         else
1267                 grid->startx = v2d->cur.xmin;
1268                 
1269         if (ELEM(V2D_ARG_DUMMY, yunits, yclamp) == 0) {
1270                 grid->starty = (v2d->cur.ymin - (float)fmod(v2d->cur.ymin, grid->dy));
1271                 if (v2d->cur.ymin < 0.0f) grid->starty -= grid->dy;
1272         }
1273         else
1274                 grid->starty = v2d->cur.ymin;
1275         
1276         return grid;
1277 }
1278
1279 /* Draw gridlines in the given 2d-region */
1280 void UI_view2d_grid_draw(View2D *v2d, View2DGrid *grid, int flag)
1281 {
1282         float vec1[2], vec2[2];
1283         int a, step;
1284         
1285         /* check for grid first, as it may not exist */
1286         if (grid == NULL)
1287                 return;
1288         
1289         /* vertical lines */
1290         if (flag & V2D_VERTICAL_LINES) {
1291                 /* initialize initial settings */
1292                 vec1[0] = vec2[0] = grid->startx;
1293                 vec1[1] = grid->starty;
1294                 vec2[1] = v2d->cur.ymax;
1295                 
1296                 /* minor gridlines */
1297                 step = (BLI_rcti_size_x(&v2d->mask) + 1) / (U.v2d_min_gridsize * UI_DPI_FAC);
1298                 UI_ThemeColor(TH_GRID);
1299                 
1300                 for (a = 0; a < step; a++) {
1301                         glBegin(GL_LINE_STRIP);
1302                         glVertex2fv(vec1);
1303                         glVertex2fv(vec2);
1304                         glEnd();
1305                         
1306                         vec2[0] = vec1[0] += grid->dx;
1307                 }
1308                 
1309                 /* major gridlines */
1310                 vec2[0] = vec1[0] -= 0.5f * grid->dx;
1311                 UI_ThemeColorShade(TH_GRID, 16);
1312                 
1313                 step++;
1314                 for (a = 0; a <= step; a++) {
1315                         glBegin(GL_LINE_STRIP);
1316                         glVertex2fv(vec1);
1317                         glVertex2fv(vec2);
1318                         glEnd();
1319                         
1320                         vec2[0] = vec1[0] -= grid->dx;
1321                 }
1322         }
1323         
1324         /* horizontal lines */
1325         if (flag & V2D_HORIZONTAL_LINES) {
1326                 /* only major gridlines */
1327                 vec1[1] = vec2[1] = grid->starty;
1328                 vec1[0] = grid->startx;
1329                 vec2[0] = v2d->cur.xmax;
1330                 
1331                 step = (BLI_rcti_size_y(&v2d->mask) + 1) / (U.v2d_min_gridsize * UI_DPI_FAC);
1332                 
1333                 UI_ThemeColor(TH_GRID);
1334                 for (a = 0; a <= step; a++) {
1335                         glBegin(GL_LINE_STRIP);
1336                         glVertex2fv(vec1);
1337                         glVertex2fv(vec2);
1338                         glEnd();
1339                         
1340                         vec2[1] = vec1[1] += grid->dy;
1341                 }
1342                 
1343                 /* fine grid lines */
1344                 vec2[1] = vec1[1] -= 0.5f * grid->dy;
1345                 step++;
1346                 
1347                 if (flag & V2D_HORIZONTAL_FINELINES) {
1348                         UI_ThemeColorShade(TH_GRID, 16);
1349                         for (a = 0; a < step; a++) {
1350                                 glBegin(GL_LINE_STRIP);
1351                                 glVertex2fv(vec1);
1352                                 glVertex2fv(vec2);
1353                                 glEnd();
1354                                 
1355                                 vec2[1] = vec1[1] -= grid->dy;
1356                         }
1357                 }
1358         }
1359         
1360         /* Axes are drawn as darker lines */
1361         UI_ThemeColorShade(TH_GRID, -50);
1362         
1363         /* horizontal axis */
1364         if (flag & V2D_HORIZONTAL_AXIS) {
1365                 vec1[0] = v2d->cur.xmin;
1366                 vec2[0] = v2d->cur.xmax;
1367                 vec1[1] = vec2[1] = 0.0f;
1368                 
1369                 glBegin(GL_LINE_STRIP);
1370                 glVertex2fv(vec1);
1371                 glVertex2fv(vec2);
1372                 glEnd();
1373         }
1374         
1375         /* vertical axis */
1376         if (flag & V2D_VERTICAL_AXIS) {
1377                 vec1[1] = v2d->cur.ymin;
1378                 vec2[1] = v2d->cur.ymax;
1379                 vec1[0] = vec2[0] = 0.0f;
1380                 
1381                 glBegin(GL_LINE_STRIP);
1382                 glVertex2fv(vec1);
1383                 glVertex2fv(vec2);
1384                 glEnd();
1385         }
1386 }
1387
1388 /* Draw a constant grid in given 2d-region */
1389 void UI_view2d_constant_grid_draw(View2D *v2d)
1390 {
1391         float start, step = 25.0f;
1392
1393         UI_ThemeColorShade(TH_BACK, -10);
1394         
1395         start = v2d->cur.xmin - (float)fmod(v2d->cur.xmin, step);
1396         
1397         glBegin(GL_LINES);
1398         for (; start < v2d->cur.xmax; start += step) {
1399                 glVertex2f(start, v2d->cur.ymin);
1400                 glVertex2f(start, v2d->cur.ymax);
1401         }
1402
1403         start = v2d->cur.ymin - (float)fmod(v2d->cur.ymin, step);
1404         for (; start < v2d->cur.ymax; start += step) {
1405                 glVertex2f(v2d->cur.xmin, start);
1406                 glVertex2f(v2d->cur.xmax, start);
1407         }
1408         
1409         /* X and Y axis */
1410         UI_ThemeColorShade(TH_BACK, -18);
1411         glVertex2f(0.0f, v2d->cur.ymin);
1412         glVertex2f(0.0f, v2d->cur.ymax);
1413         glVertex2f(v2d->cur.xmin, 0.0f);
1414         glVertex2f(v2d->cur.xmax, 0.0f);
1415         
1416         glEnd();
1417 }
1418
1419 /* Draw a multi-level grid in given 2d-region */
1420 void UI_view2d_multi_grid_draw(View2D *v2d, int colorid, float step, int level_size, int totlevels)
1421 {
1422         int offset = -10;
1423         float lstep = step;
1424         int level;
1425         
1426         for (level = 0; level < totlevels; ++level) {
1427                 int i;
1428                 float start;
1429                 
1430                 UI_ThemeColorShade(colorid, offset);
1431                 
1432                 i = (v2d->cur.xmin >= 0.0f ? -(int)(-v2d->cur.xmin / lstep) : (int)(v2d->cur.xmin / lstep));
1433                 start = i * lstep;
1434                 
1435                 glBegin(GL_LINES);
1436                 for (; start < v2d->cur.xmax; start += lstep, ++i) {
1437                         if (i == 0 || (level < totlevels - 1 && i % level_size == 0))
1438                                 continue;
1439                         glVertex2f(start, v2d->cur.ymin);
1440                         glVertex2f(start, v2d->cur.ymax);
1441                 }
1442                 
1443                 i = (v2d->cur.ymin >= 0.0f ? -(int)(-v2d->cur.ymin / lstep) : (int)(v2d->cur.ymin / lstep));
1444                 start = i * lstep;
1445                 
1446                 for (; start < v2d->cur.ymax; start += lstep, ++i) {
1447                         if (i == 0 || (level < totlevels - 1 && i % level_size == 0))
1448                                 continue;
1449                         glVertex2f(v2d->cur.xmin, start);
1450                         glVertex2f(v2d->cur.xmax, start);
1451                 }
1452                 
1453                 /* X and Y axis */
1454                 UI_ThemeColorShade(colorid, offset - 8);
1455                 glVertex2f(0.0f, v2d->cur.ymin);
1456                 glVertex2f(0.0f, v2d->cur.ymax);
1457                 glVertex2f(v2d->cur.xmin, 0.0f);
1458                 glVertex2f(v2d->cur.xmax, 0.0f);
1459                 
1460                 glEnd();
1461                 
1462                 lstep *= level_size;
1463                 offset -= 6;
1464         }
1465 }
1466
1467 /* the price we pay for not exposting structs :( */
1468 void UI_view2d_grid_size(View2DGrid *grid, float *r_dx, float *r_dy)
1469 {
1470         *r_dx = grid->dx;
1471         *r_dy = grid->dy;
1472 }
1473
1474 /* free temporary memory used for drawing grid */
1475 void UI_view2d_grid_free(View2DGrid *grid)
1476 {
1477         /* only free if there's a grid */
1478         if (grid) {
1479                 MEM_freeN(grid);
1480         }
1481 }
1482
1483 /* *********************************************************************** */
1484 /* Scrollers */
1485
1486 /* View2DScrollers is typedef'd in UI_view2d.h 
1487  * WARNING: the start of this struct must not change, as view2d_ops.c uses this too. 
1488  *         For now, we don't need to have a separate (internal) header for structs like this...
1489  */
1490 struct View2DScrollers {
1491         /* focus bubbles */
1492         int vert_min, vert_max; /* vertical scrollbar */
1493         int hor_min, hor_max;   /* horizontal scrollbar */
1494         
1495         rcti hor, vert;         /* exact size of slider backdrop */
1496         int horfull, vertfull;  /* set if sliders are full, we don't draw them */
1497         
1498         /* scales */
1499         View2DGrid *grid;       /* grid for coordinate drawing */
1500         short xunits, xclamp;   /* units and clamping options for x-axis */
1501         short yunits, yclamp;   /* units and clamping options for y-axis */
1502 };
1503
1504 /* Calculate relevant scroller properties */
1505 View2DScrollers *UI_view2d_scrollers_calc(const bContext *C, View2D *v2d,
1506                                           short xunits, short xclamp, short yunits, short yclamp)
1507 {
1508         View2DScrollers *scrollers;
1509         rcti vert, hor;
1510         float fac1, fac2, totsize, scrollsize;
1511         int scroll = view2d_scroll_mapped(v2d->scroll);
1512         int smaller;
1513         
1514         /* scrollers is allocated here... */
1515         scrollers = MEM_callocN(sizeof(View2DScrollers), "View2DScrollers");
1516         
1517         vert = v2d->vert;
1518         hor = v2d->hor;
1519         
1520         /* slider rects need to be smaller than region */
1521         smaller = (int)(0.2f * U.widget_unit);
1522         hor.xmin += smaller;
1523         hor.xmax -= smaller;
1524         if (scroll & V2D_SCROLL_BOTTOM)
1525                 hor.ymin += smaller;
1526         else
1527                 hor.ymax -= smaller;
1528         
1529         if (scroll & V2D_SCROLL_LEFT)
1530                 vert.xmin += smaller;
1531         else
1532                 vert.xmax -= smaller;
1533         vert.ymin += smaller;
1534         vert.ymax -= smaller;
1535         
1536         CLAMP(vert.ymin, vert.ymin, vert.ymax - V2D_SCROLLER_HANDLE_SIZE);
1537         CLAMP(hor.xmin, hor.xmin, hor.xmax - V2D_SCROLLER_HANDLE_SIZE);
1538         
1539         /* store in scrollers, used for drawing */
1540         scrollers->vert = vert;
1541         scrollers->hor = hor;
1542         
1543         /* scroller 'buttons':
1544          *      - These should always remain within the visible region of the scrollbar
1545          *      - They represent the region of 'tot' that is visible in 'cur'
1546          */
1547         
1548         /* horizontal scrollers */
1549         if (scroll & V2D_SCROLL_HORIZONTAL) {
1550                 /* scroller 'button' extents */
1551                 totsize = BLI_rctf_size_x(&v2d->tot);
1552                 scrollsize = (float)BLI_rcti_size_x(&hor);
1553                 if (totsize == 0.0f) totsize = 1.0f;  /* avoid divide by zero */
1554
1555                 fac1 = (v2d->cur.xmin - v2d->tot.xmin) / totsize;
1556                 if (fac1 <= 0.0f)
1557                         scrollers->hor_min = hor.xmin;
1558                 else
1559                         scrollers->hor_min = (int)(hor.xmin + (fac1 * scrollsize));
1560                 
1561                 fac2 = (v2d->cur.xmax - v2d->tot.xmin) / totsize;
1562                 if (fac2 >= 1.0f)
1563                         scrollers->hor_max = hor.xmax;
1564                 else
1565                         scrollers->hor_max = (int)(hor.xmin + (fac2 * scrollsize));
1566                 
1567                 /* prevent inverted sliders */
1568                 if (scrollers->hor_min > scrollers->hor_max) 
1569                         scrollers->hor_min = scrollers->hor_max;
1570                 /* prevent sliders from being too small, and disappearing */
1571                 if ((scrollers->hor_max - scrollers->hor_min) < V2D_SCROLLER_HANDLE_SIZE) {
1572                         scrollers->hor_max = scrollers->hor_min + V2D_SCROLLER_HANDLE_SIZE;
1573
1574                         CLAMP(scrollers->hor_max, hor.xmin + V2D_SCROLLER_HANDLE_SIZE, hor.xmax);
1575                         CLAMP(scrollers->hor_min, hor.xmin, hor.xmax - V2D_SCROLLER_HANDLE_SIZE);
1576                 }
1577                 
1578         }
1579         
1580         /* vertical scrollers */
1581         if (scroll & V2D_SCROLL_VERTICAL) {
1582                 /* scroller 'button' extents */
1583                 totsize    =        BLI_rctf_size_y(&v2d->tot);
1584                 scrollsize = (float)BLI_rcti_size_y(&vert);
1585                 if (totsize == 0.0f) totsize = 1.0f;  /* avoid divide by zero */
1586
1587                 fac1 = (v2d->cur.ymin - v2d->tot.ymin) / totsize;
1588                 if (fac1 <= 0.0f)
1589                         scrollers->vert_min = vert.ymin;
1590                 else
1591                         scrollers->vert_min = (int)(vert.ymin + (fac1 * scrollsize));
1592                 
1593                 fac2 = (v2d->cur.ymax - v2d->tot.ymin) / totsize;
1594                 if (fac2 >= 1.0f)
1595                         scrollers->vert_max = vert.ymax;
1596                 else
1597                         scrollers->vert_max = (int)(vert.ymin + (fac2 * scrollsize));
1598                 
1599                 /* prevent inverted sliders */
1600                 if (scrollers->vert_min > scrollers->vert_max) 
1601                         scrollers->vert_min = scrollers->vert_max;
1602                 /* prevent sliders from being too small, and disappearing */
1603                 if ((scrollers->vert_max - scrollers->vert_min) < V2D_SCROLLER_HANDLE_SIZE) {
1604                         
1605                         scrollers->vert_max = scrollers->vert_min + V2D_SCROLLER_HANDLE_SIZE;
1606                         
1607                         CLAMP(scrollers->vert_max, vert.ymin + V2D_SCROLLER_HANDLE_SIZE, vert.ymax);
1608                         CLAMP(scrollers->vert_min, vert.ymin, vert.ymax - V2D_SCROLLER_HANDLE_SIZE);
1609                 }
1610
1611         }
1612         
1613         /* grid markings on scrollbars */
1614         if (scroll & (V2D_SCROLL_SCALE_HORIZONTAL | V2D_SCROLL_SCALE_VERTICAL)) {
1615                 /* store clamping */
1616                 scrollers->xclamp = xclamp;
1617                 scrollers->xunits = xunits;
1618                 scrollers->yclamp = yclamp;
1619                 scrollers->yunits = yunits;
1620                 
1621                 scrollers->grid = UI_view2d_grid_calc(CTX_data_scene(C), v2d,
1622                                                       xunits, xclamp, yunits, yclamp,
1623                                                       BLI_rcti_size_x(&hor), BLI_rcti_size_y(&vert));
1624         }
1625         
1626         /* return scrollers */
1627         return scrollers;
1628 }
1629
1630 /* Print scale marking along a time scrollbar */
1631 static void scroll_printstr(Scene *scene, float x, float y, float val, int power, short unit, char dir)
1632 {
1633         int len;
1634         char timecode_str[32];
1635         
1636         /* adjust the scale unit to work ok */
1637         if (dir == 'v') {
1638                 /* here we bump up the power by factor of 10, as 
1639                  * rotation values (hence 'degrees') are divided by 10 to 
1640                  * be able to show the curves at the same time
1641                  */
1642                 if (ELEM(unit, V2D_UNIT_DEGREES, V2D_UNIT_TIME)) {
1643                         power += 1;
1644                         val *= 10;
1645                 }
1646         }
1647         
1648         /* get string to print */
1649         if (unit == V2D_UNIT_SECONDS) {
1650                 /* not neces*/
1651                 BLI_timecode_string_from_time(timecode_str, sizeof(timecode_str), power, val, FPS, U.timecode_style);
1652         }
1653         else {
1654                 BLI_timecode_string_from_time_simple(timecode_str, sizeof(timecode_str), power, val);
1655         }
1656         
1657         /* get length of string, and adjust printing location to fit it into the horizontal scrollbar */
1658         len = strlen(timecode_str);
1659         if (dir == 'h') {
1660                 /* seconds/timecode display has slightly longer strings... */
1661                 if (unit == V2D_UNIT_SECONDS)
1662                         x -= 3 * len;
1663                 else
1664                         x -= 4 * len;
1665         }
1666         
1667         /* Add degree sympbol to end of string for vertical scrollbar? */
1668         if ((dir == 'v') && (unit == V2D_UNIT_DEGREES)) {
1669                 timecode_str[len] = 186;
1670                 timecode_str[len + 1] = 0;
1671         }
1672         
1673         /* draw it */
1674         BLF_draw_default_ascii(x, y, 0.0f, timecode_str, sizeof(timecode_str));
1675 }
1676
1677 /* Draw scrollbars in the given 2d-region */
1678 void UI_view2d_scrollers_draw(const bContext *C, View2D *v2d, View2DScrollers *vs)
1679 {
1680         Scene *scene = CTX_data_scene(C);
1681         rcti vert, hor;
1682         int scroll = view2d_scroll_mapped(v2d->scroll);
1683         
1684         /* make copies of rects for less typing */
1685         vert = vs->vert;
1686         hor = vs->hor;
1687         
1688         /* horizontal scrollbar */
1689         if (scroll & V2D_SCROLL_HORIZONTAL) {
1690                 bTheme *btheme = UI_GetTheme();
1691                 uiWidgetColors wcol = btheme->tui.wcol_scroll;
1692                 rcti slider;
1693                 int state;
1694                 unsigned char col[4];
1695                 
1696                 slider.xmin = vs->hor_min;
1697                 slider.xmax = vs->hor_max;
1698                 slider.ymin = hor.ymin;
1699                 slider.ymax = hor.ymax;
1700                 
1701                 state = (v2d->scroll_ui & V2D_SCROLL_H_ACTIVE) ? UI_SCROLL_PRESSED : 0;
1702                 
1703                 /* show zoom handles if:
1704                  *      - zooming on x-axis is allowed (no scroll otherwise)
1705                  *      - slider bubble is large enough (no overdraw confusion)
1706                  *      - scale is shown on the scroller 
1707                  *        (workaround to make sure that button windows don't show these, 
1708                  *              and only the time-grids with their zoomability can do so)
1709                  */
1710                 if ((v2d->keepzoom & V2D_LOCKZOOM_X) == 0 &&
1711                     (v2d->scroll & V2D_SCROLL_SCALE_HORIZONTAL) &&
1712                     (BLI_rcti_size_x(&slider) > V2D_SCROLLER_HANDLE_SIZE))
1713                 {
1714                         state |= UI_SCROLL_ARROWS;
1715                 }
1716                 
1717                 /* clean rect behind slider, but not with transparent background */
1718                 UI_GetThemeColor4ubv(TH_BACK, col);
1719                 if (col[3] == 255) {
1720                         glColor3ub(col[0], col[1], col[2]);
1721                         glRecti(v2d->hor.xmin, v2d->hor.ymin, v2d->hor.xmax, v2d->hor.ymax);
1722                 }
1723                 
1724                 UI_draw_widget_scroll(&wcol, &hor, &slider, state);
1725                 
1726                 /* scale indicators */
1727                 if ((scroll & V2D_SCROLL_SCALE_HORIZONTAL) && (vs->grid)) {
1728                         View2DGrid *grid = vs->grid;
1729                         float fac, dfac, fac2, val;
1730                         
1731                         /* the numbers: convert grid->startx and -dx to scroll coordinates 
1732                          *      - fac is x-coordinate to draw to
1733                          *      - dfac is gap between scale markings
1734                          */
1735                         fac = (grid->startx - v2d->cur.xmin) / BLI_rctf_size_x(&v2d->cur);
1736                         fac = (float)hor.xmin + fac * BLI_rcti_size_x(&hor);
1737                         
1738                         dfac = grid->dx / BLI_rctf_size_x(&v2d->cur);
1739                         dfac = dfac * BLI_rcti_size_x(&hor);
1740                         
1741                         /* set starting value, and text color */
1742                         UI_ThemeColor(TH_TEXT);
1743                         val = grid->startx;
1744                         
1745                         /* if we're clamping to whole numbers only, make sure entries won't be repeated */
1746                         if (vs->xclamp == V2D_GRID_CLAMP) {
1747                                 while (grid->dx < 0.9999f) {
1748                                         grid->dx *= 2.0f;
1749                                         dfac *= 2.0f;
1750                                 }
1751                         }
1752                         if (vs->xunits == V2D_UNIT_FRAMES)
1753                                 grid->powerx = 1;
1754                         
1755                         /* draw numbers in the appropriate range */
1756                         if (dfac > 0.0f) {
1757                                 float h = 0.1f * UI_UNIT_Y + (float)(hor.ymin);
1758                                 
1759                                 for (; fac < hor.xmax - 0.5f * U.widget_unit; fac += dfac, val += grid->dx) {
1760                                         
1761                                         /* make prints look nicer for scrollers */
1762                                         if (fac < hor.xmin + 0.5f * U.widget_unit)
1763                                                 continue;
1764                                         
1765                                         switch (vs->xunits) {
1766                                                 case V2D_UNIT_FRAMES:       /* frames (as whole numbers)*/
1767                                                         scroll_printstr(scene, fac, h, val, grid->powerx, V2D_UNIT_FRAMES, 'h');
1768                                                         break;
1769                                                         
1770                                                 case V2D_UNIT_FRAMESCALE:   /* frames (not always as whole numbers) */
1771                                                         scroll_printstr(scene, fac, h, val, grid->powerx, V2D_UNIT_FRAMESCALE, 'h');
1772                                                         break;
1773                                                 
1774                                                 case V2D_UNIT_SECONDS:      /* seconds */
1775                                                         fac2 = val / (float)FPS;
1776                                                         scroll_printstr(scene, fac, h, fac2, grid->powerx, V2D_UNIT_SECONDS, 'h');
1777                                                         break;
1778                                                         
1779                                                 case V2D_UNIT_DEGREES:      /* Graph Editor for rotation Drivers */
1780                                                         /* HACK: although we're drawing horizontal, we make this draw as 'vertical', just to get degree signs */
1781                                                         scroll_printstr(scene, fac, h, val, grid->powerx, V2D_UNIT_DEGREES, 'v');
1782                                                         break;
1783                                         }
1784                                 }
1785                         }
1786                 }
1787         }
1788         
1789         /* vertical scrollbar */
1790         if (scroll & V2D_SCROLL_VERTICAL) {
1791                 bTheme *btheme = UI_GetTheme();
1792                 uiWidgetColors wcol = btheme->tui.wcol_scroll;
1793                 rcti slider;
1794                 int state;
1795                 unsigned char col[4];
1796                 
1797                 slider.xmin = vert.xmin;
1798                 slider.xmax = vert.xmax;
1799                 slider.ymin = vs->vert_min;
1800                 slider.ymax = vs->vert_max;
1801                 
1802                 state = (v2d->scroll_ui & V2D_SCROLL_V_ACTIVE) ? UI_SCROLL_PRESSED : 0;
1803                 
1804                 /* show zoom handles if:
1805                  *      - zooming on y-axis is allowed (no scroll otherwise)
1806                  *      - slider bubble is large enough (no overdraw confusion)
1807                  *      - scale is shown on the scroller 
1808                  *        (workaround to make sure that button windows don't show these, 
1809                  *              and only the time-grids with their zoomability can do so)
1810                  */
1811                 if ((v2d->keepzoom & V2D_LOCKZOOM_Y) == 0 &&
1812                     (v2d->scroll & V2D_SCROLL_SCALE_VERTICAL) &&
1813                     (BLI_rcti_size_y(&slider) > V2D_SCROLLER_HANDLE_SIZE))
1814                 {
1815                         state |= UI_SCROLL_ARROWS;
1816                 }
1817                 
1818                 /* clean rect behind slider, but not with transparent background */
1819                 UI_GetThemeColor4ubv(TH_BACK, col);
1820                 if (col[3] == 255) {
1821                         glColor3ub(col[0], col[1], col[2]);
1822                         glRecti(v2d->vert.xmin, v2d->vert.ymin, v2d->vert.xmax, v2d->vert.ymax);
1823                 }
1824                 
1825                 UI_draw_widget_scroll(&wcol, &vert, &slider, state);
1826                 
1827                 
1828                 /* scale indiators */
1829                 if ((scroll & V2D_SCROLL_SCALE_VERTICAL) && (vs->grid)) {
1830                         View2DGrid *grid = vs->grid;
1831                         float fac, dfac, val;
1832                         
1833                         /* the numbers: convert grid->starty and dy to scroll coordinates 
1834                          *      - fac is y-coordinate to draw to
1835                          *      - dfac is gap between scale markings
1836                          *      - these involve a correction for horizontal scrollbar
1837                          *        NOTE: it's assumed that that scrollbar is there if this is involved!
1838                          */
1839                         fac = (grid->starty - v2d->cur.ymin) / BLI_rctf_size_y(&v2d->cur);
1840                         fac = vert.ymin + fac * BLI_rcti_size_y(&vert);
1841                         
1842                         dfac = grid->dy / BLI_rctf_size_y(&v2d->cur);
1843                         dfac = dfac     * BLI_rcti_size_y(&vert);
1844                         
1845                         /* set starting value, and text color */
1846                         UI_ThemeColor(TH_TEXT);
1847                         val = grid->starty;
1848                         
1849                         /* if vertical clamping (to whole numbers) is used (i.e. in Sequencer), apply correction */
1850                         if (vs->yclamp == V2D_GRID_CLAMP)
1851                                 fac += 0.5f * dfac;
1852                                 
1853                         /* draw vertical steps */
1854                         if (dfac > 0.0f) {
1855                                 
1856                                 BLF_rotation_default(M_PI_2);
1857                                 BLF_enable_default(BLF_ROTATION);
1858
1859                                 for (; fac < vert.ymax - 10; fac += dfac, val += grid->dy) {
1860                                         
1861                                         /* make prints look nicer for scrollers */
1862                                         if (fac < vert.ymin + 10)
1863                                                 continue;
1864                                         
1865                                         scroll_printstr(scene, (float)(vert.xmax) - 2.0f, fac, val, grid->powery, vs->yunits, 'v');
1866                                 }
1867                                 
1868                                 BLF_disable_default(BLF_ROTATION);
1869                         }
1870                 }
1871         }
1872         
1873 }
1874
1875 /* free temporary memory used for drawing scrollers */
1876 void UI_view2d_scrollers_free(View2DScrollers *scrollers)
1877 {
1878         /* need to free grid as well... */
1879         if (scrollers->grid) {
1880                 MEM_freeN(scrollers->grid);
1881         }
1882         MEM_freeN(scrollers);
1883 }
1884
1885 /* *********************************************************************** */
1886 /* List View Utilities */
1887
1888 /* Get the view-coordinates of the nominated cell 
1889  *      - columnwidth, rowheight        = size of each 'cell'
1890  *      - startx, starty                        = coordinates (in 'tot' rect space) that the list starts from
1891  *                                                        This should be (0,0) for most views. However, for those where the starting row was offsetted
1892  *                                                        (like for Animation Editor channel lists, to make the first entry more visible), these will be 
1893  *                                                        the min-coordinates of the first item.
1894  *      - column, row                           = the 2d-coordinates (in 2D-view / 'tot' rect space) the cell exists at
1895  *      - rect                                  = coordinates of the cell (passed as single var instead of 4 separate, as it's more useful this way)
1896  */
1897 void UI_view2d_listview_cell_to_view(View2D *v2d, float columnwidth, float rowheight,
1898                                      float startx, float starty,
1899                                      int column, int row, rctf *rect)
1900 {
1901         /* sanity checks */
1902         if (ELEM(NULL, v2d, rect)) {
1903                 return;
1904         }
1905
1906         if ((columnwidth <= 0) && (rowheight <= 0)) {
1907                 rect->xmin = rect->xmax = 0.0f;
1908                 rect->ymin = rect->ymax = 0.0f;
1909                 return;
1910         }
1911         
1912         /* x-coordinates */
1913         rect->xmin = startx + (float)(columnwidth * column);
1914         rect->xmax = startx + (float)(columnwidth * (column + 1));
1915         
1916         if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
1917                 /* simply negate the values for the coordinates if in negative half */
1918                 rect->xmin = -rect->xmin;
1919                 rect->xmax = -rect->xmax;
1920         }
1921         
1922         /* y-coordinates */
1923         rect->ymin = starty + (float)(rowheight * row);
1924         rect->ymax = starty + (float)(rowheight * (row + 1));
1925         
1926         if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
1927                 /* simply negate the values for the coordinates if in negative half */
1928                 rect->ymin = -rect->ymin;
1929                 rect->ymax = -rect->ymax;
1930         }
1931 }
1932
1933 /* Get the 'cell' (row, column) that the given 2D-view coordinates (i.e. in 'tot' rect space) lie in.
1934  *      - columnwidth, rowheight        = size of each 'cell'
1935  *      - startx, starty                        = coordinates (in 'tot' rect space) that the list starts from
1936  *                                                        This should be (0,0) for most views. However, for those where the starting row was offsetted
1937  *                                                        (like for Animation Editor channel lists, to make the first entry more visible), these will be 
1938  *                                                        the min-coordinates of the first item.
1939  *      - viewx, viewy                  = 2D-coordinates (in 2D-view / 'tot' rect space) to get the cell for
1940  *      - column, row                           = the 'coordinates' of the relevant 'cell'
1941  */
1942 void UI_view2d_listview_view_to_cell(View2D *v2d, float columnwidth, float rowheight, float startx, float starty,
1943                                      float viewx, float viewy, int *column, int *row)
1944 {
1945         /* adjust view coordinates to be all positive ints, corrected for the start offset */
1946         const int x = (int)(floorf(fabsf(viewx) + 0.5f) - startx);
1947         const int y = (int)(floorf(fabsf(viewy) + 0.5f) - starty);
1948         
1949         /* sizes must not be negative */
1950         if ((v2d == NULL) || ((columnwidth <= 0) && (rowheight <= 0))) {
1951                 if (column) *column = 0;
1952                 if (row) *row = 0;
1953                 
1954                 return;
1955         }
1956         
1957         /* get column */
1958         if ((column) && (columnwidth > 0))
1959                 *column = x / columnwidth;
1960         else if (column)
1961                 *column = 0;
1962         
1963         /* get row */
1964         if ((row) && (rowheight > 0))
1965                 *row = y / rowheight;
1966         else if (row)
1967                 *row = 0;
1968 }
1969
1970 /* Get the 'extreme' (min/max) column and row indices which are visible within the 'cur' rect 
1971  *      - columnwidth, rowheight        = size of each 'cell'
1972  *      - startx, starty                        = coordinates that the list starts from, which should be (0,0) for most views
1973  *      - column/row_min/max            = the starting and ending column/row indices
1974  */
1975 void UI_view2d_listview_visible_cells(View2D *v2d, float columnwidth, float rowheight, float startx, float starty,
1976                                       int *column_min, int *column_max, int *row_min, int *row_max)
1977 {
1978         /* using 'cur' rect coordinates, call the cell-getting function to get the cells for this */
1979         if (v2d) {
1980                 /* min */
1981                 UI_view2d_listview_view_to_cell(v2d, columnwidth, rowheight, startx, starty, 
1982                                                 v2d->cur.xmin, v2d->cur.ymin, column_min, row_min);
1983
1984                 /* max*/
1985                 UI_view2d_listview_view_to_cell(v2d, columnwidth, rowheight, startx, starty, 
1986                                                 v2d->cur.xmax, v2d->cur.ymax, column_max, row_max);
1987         }
1988 }
1989
1990 /* *********************************************************************** */
1991 /* Coordinate Conversions */
1992
1993 float UI_view2d_region_to_view_x(struct View2D *v2d, float x)
1994 {
1995         return (v2d->cur.xmin + (BLI_rctf_size_x(&v2d->cur) * (x - v2d->mask.xmin) / BLI_rcti_size_x(&v2d->mask)));
1996 }
1997 float UI_view2d_region_to_view_y(struct View2D *v2d, float y)
1998 {
1999         return (v2d->cur.ymin + (BLI_rctf_size_y(&v2d->cur) * (y - v2d->mask.ymin) / BLI_rcti_size_y(&v2d->mask)));
2000 }
2001
2002 /* Convert from screen/region space to 2d-View space 
2003  *      
2004  *      - x,y           = coordinates to convert
2005  *      - viewx,viewy           = resultant coordinates
2006  */
2007 void UI_view2d_region_to_view(View2D *v2d, float x, float y, float *r_view_x, float *r_view_y)
2008 {
2009         *r_view_x = UI_view2d_region_to_view_x(v2d, x);
2010         *r_view_y = UI_view2d_region_to_view_y(v2d, y);
2011 }
2012
2013 void UI_view2d_region_to_view_rctf(View2D *v2d, const rctf *rect_src, rctf *rect_dst)
2014 {
2015         const float cur_size[2]  = {BLI_rctf_size_x(&v2d->cur),  BLI_rctf_size_y(&v2d->cur)};
2016         const float mask_size[2] = {BLI_rcti_size_x(&v2d->mask), BLI_rcti_size_y(&v2d->mask)};
2017
2018         rect_dst->xmin = (v2d->cur.xmin + (cur_size[0] * (rect_src->xmin - v2d->mask.xmin) / mask_size[0]));
2019         rect_dst->xmax = (v2d->cur.xmin + (cur_size[0] * (rect_src->xmax - v2d->mask.xmin) / mask_size[0]));
2020         rect_dst->ymin = (v2d->cur.ymin + (cur_size[1] * (rect_src->ymin - v2d->mask.ymin) / mask_size[1]));
2021         rect_dst->ymax = (v2d->cur.ymin + (cur_size[1] * (rect_src->ymax - v2d->mask.ymin) / mask_size[1]));
2022 }
2023
2024 float UI_view2d_view_to_region_x(View2D *v2d, float x)
2025 {
2026         return (v2d->mask.xmin + (((x - v2d->cur.xmin) / BLI_rctf_size_x(&v2d->cur)) * BLI_rcti_size_x(&v2d->mask)));
2027 }
2028 float UI_view2d_view_to_region_y(View2D *v2d, float y)
2029 {
2030         return (v2d->mask.ymin + (((y - v2d->cur.ymin) / BLI_rctf_size_y(&v2d->cur)) * BLI_rcti_size_y(&v2d->mask)));
2031 }
2032
2033 /* Convert from 2d-View space to screen/region space
2034  *      - Coordinates are clamped to lie within bounds of region
2035  *
2036  *      - x,y               = coordinates to convert
2037  *      - regionx,regiony   = resultant coordinates
2038  */
2039 bool UI_view2d_view_to_region_clip(View2D *v2d, float x, float y, int *r_region_x, int *r_region_y)
2040 {
2041         /* express given coordinates as proportional values */
2042         x = (x - v2d->cur.xmin) / BLI_rctf_size_x(&v2d->cur);
2043         y = (y - v2d->cur.ymin) / BLI_rctf_size_y(&v2d->cur);
2044         
2045         /* check if values are within bounds */
2046         if ((x >= 0.0f) && (x <= 1.0f) && (y >= 0.0f) && (y <= 1.0f)) {
2047                 *r_region_x = (int)(v2d->mask.xmin + (x * BLI_rcti_size_x(&v2d->mask)));
2048                 *r_region_y = (int)(v2d->mask.ymin + (y * BLI_rcti_size_y(&v2d->mask)));
2049
2050                 return true;
2051         }
2052         else {
2053                 /* set initial value in case coordinate lies outside of bounds */
2054                 *r_region_x = *r_region_y = V2D_IS_CLIPPED;
2055
2056                 return false;
2057         }
2058 }
2059
2060 /* Convert from 2d-view space to screen/region space
2061  *      - Coordinates are NOT clamped to lie within bounds of region
2062  *
2063  *      - x,y               = coordinates to convert
2064  *      - regionx,regiony   = resultant coordinates
2065  */
2066 void UI_view2d_view_to_region(View2D *v2d, float x, float y, int *r_region_x, int *r_region_y)
2067 {
2068         /* step 1: express given coordinates as proportional values */
2069         x = (x - v2d->cur.xmin) / BLI_rctf_size_x(&v2d->cur);
2070         y = (y - v2d->cur.ymin) / BLI_rctf_size_y(&v2d->cur);
2071
2072         /* step 2: convert proportional distances to screen coordinates  */
2073         x = v2d->mask.xmin + (x * BLI_rcti_size_x(&v2d->mask));
2074         y = v2d->mask.ymin + (y * BLI_rcti_size_y(&v2d->mask));
2075
2076         /* although we don't clamp to lie within region bounds, we must avoid exceeding size of ints */
2077         *r_region_x = clamp_float_to_int(x);
2078         *r_region_y = clamp_float_to_int(y);
2079 }
2080
2081 void UI_view2d_view_to_region_fl(View2D *v2d, float x, float y, float *r_region_x, float *r_region_y)
2082 {
2083         /* express given coordinates as proportional values */
2084         x = (x - v2d->cur.xmin) / BLI_rctf_size_x(&v2d->cur);
2085         y = (y - v2d->cur.ymin) / BLI_rctf_size_y(&v2d->cur);
2086
2087         /* convert proportional distances to screen coordinates */
2088         *r_region_x = v2d->mask.xmin + (x * BLI_rcti_size_x(&v2d->mask));
2089         *r_region_y = v2d->mask.ymin + (y * BLI_rcti_size_y(&v2d->mask));
2090 }
2091
2092 void UI_view2d_view_to_region_rcti(View2D *v2d, const rctf *rect_src, rcti *rect_dst)
2093 {
2094         const float cur_size[2]  = {BLI_rctf_size_x(&v2d->cur),  BLI_rctf_size_y(&v2d->cur)};
2095         const float mask_size[2] = {BLI_rcti_size_x(&v2d->mask), BLI_rcti_size_y(&v2d->mask)};
2096         rctf rect_tmp;
2097
2098         /* step 1: express given coordinates as proportional values */
2099         rect_tmp.xmin = (rect_src->xmin - v2d->cur.xmin) / cur_size[0];
2100         rect_tmp.xmax = (rect_src->xmax - v2d->cur.xmin) / cur_size[0];
2101         rect_tmp.ymin = (rect_src->ymin - v2d->cur.ymin) / cur_size[1];
2102         rect_tmp.ymax = (rect_src->ymax - v2d->cur.ymin) / cur_size[1];
2103
2104
2105         /* step 2: convert proportional distances to screen coordinates  */
2106         rect_tmp.xmin = v2d->mask.xmin + (rect_tmp.xmin * mask_size[0]);
2107         rect_tmp.xmax = v2d->mask.xmin + (rect_tmp.xmax * mask_size[0]);
2108         rect_tmp.ymin = v2d->mask.ymin + (rect_tmp.ymin * mask_size[1]);
2109         rect_tmp.ymax = v2d->mask.ymin + (rect_tmp.ymax * mask_size[1]);
2110
2111         clamp_rctf_to_rcti(rect_dst, &rect_tmp);
2112 }
2113
2114 bool UI_view2d_view_to_region_rcti_clip(View2D *v2d, const rctf *rect_src, rcti *rect_dst)
2115 {
2116         const float cur_size[2]  = {BLI_rctf_size_x(&v2d->cur),  BLI_rctf_size_y(&v2d->cur)};
2117         const float mask_size[2] = {BLI_rcti_size_x(&v2d->mask), BLI_rcti_size_y(&v2d->mask)};
2118         rctf rect_tmp;
2119
2120         BLI_assert(rect_src->xmin <= rect_src->xmax && rect_src->ymin <= rect_src->ymax);
2121
2122         /* step 1: express given coordinates as proportional values */
2123         rect_tmp.xmin = (rect_src->xmin - v2d->cur.xmin) / cur_size[0];
2124         rect_tmp.xmax = (rect_src->xmax - v2d->cur.xmin) / cur_size[0];
2125         rect_tmp.ymin = (rect_src->ymin - v2d->cur.ymin) / cur_size[1];
2126         rect_tmp.ymax = (rect_src->ymax - v2d->cur.ymin) / cur_size[1];
2127
2128         if (((rect_tmp.xmax < 0.0f) || (rect_tmp.xmin > 1.0f) ||
2129              (rect_tmp.ymax < 0.0f) || (rect_tmp.ymin > 1.0f)) == 0)
2130         {
2131                 /* step 2: convert proportional distances to screen coordinates  */
2132                 rect_tmp.xmin = v2d->mask.xmin + (rect_tmp.xmin * mask_size[0]);
2133                 rect_tmp.xmax = v2d->mask.ymin + (rect_tmp.xmax * mask_size[0]);
2134                 rect_tmp.ymin = v2d->mask.ymin + (rect_tmp.ymin * mask_size[1]);
2135                 rect_tmp.ymax = v2d->mask.ymin + (rect_tmp.ymax * mask_size[1]);
2136
2137                 clamp_rctf_to_rcti(rect_dst, &rect_tmp);
2138
2139                 return true;
2140         }
2141         else {
2142                 rect_dst->xmin = rect_dst->xmax = rect_dst->ymin = rect_dst->ymax = V2D_IS_CLIPPED;
2143
2144                 return false;
2145         }
2146 }
2147
2148 /* *********************************************************************** */
2149 /* Utilities */
2150
2151 /* View2D data by default resides in region, so get from region stored in context */
2152 View2D *UI_view2d_fromcontext(const bContext *C)
2153 {
2154         ScrArea *area = CTX_wm_area(C);
2155         ARegion *region = CTX_wm_region(C);
2156
2157         if (area == NULL) return NULL;
2158         if (region == NULL) return NULL;
2159         return &(region->v2d);
2160 }
2161
2162 /* same as above, but it returns regionwindow. Utility for pulldowns or buttons */
2163 View2D *UI_view2d_fromcontext_rwin(const bContext *C)
2164 {
2165         ScrArea *sa = CTX_wm_area(C);
2166         ARegion *region = CTX_wm_region(C);
2167
2168         if (sa == NULL) return NULL;
2169         if (region == NULL) return NULL;
2170         if (region->regiontype != RGN_TYPE_WINDOW) {
2171                 ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
2172                 return ar ? &(ar->v2d) : NULL;
2173         }
2174         return &(region->v2d);
2175 }
2176
2177
2178 /* Calculate the scale per-axis of the drawing-area
2179  *      - Is used to inverse correct drawing of icons, etc. that need to follow view 
2180  *        but not be affected by scale
2181  *
2182  *      - x,y   = scale on each axis
2183  */
2184 void UI_view2d_scale_get(View2D *v2d, float *x, float *y) 
2185 {
2186         if (x) *x = BLI_rcti_size_x(&v2d->mask) / BLI_rctf_size_x(&v2d->cur);
2187         if (y) *y = BLI_rcti_size_y(&v2d->mask) / BLI_rctf_size_y(&v2d->cur);
2188 }
2189 /* Same as UI_view2d_scale_get() - 1.0f / x, y */
2190 void UI_view2d_scale_get_inverse(View2D *v2d, float *x, float *y)
2191 {
2192         if (x) *x = BLI_rctf_size_x(&v2d->cur) / BLI_rcti_size_x(&v2d->mask);
2193         if (y) *y = BLI_rctf_size_y(&v2d->cur) / BLI_rcti_size_y(&v2d->mask);
2194 }
2195
2196 /* Simple functions for consistent center offset access.
2197  * Used by node editor to shift view center for each individual node tree.
2198  */
2199 void UI_view2d_center_get(struct View2D *v2d, float *x, float *y)
2200 {
2201         /* get center */
2202         if (x) *x = BLI_rctf_cent_x(&v2d->cur);
2203         if (y) *y = BLI_rctf_cent_y(&v2d->cur);
2204 }
2205 void UI_view2d_center_set(struct View2D *v2d, float x, float y)
2206 {
2207         BLI_rctf_recenter(&v2d->cur, x, y);
2208
2209         /* make sure that 'cur' rect is in a valid state as a result of these changes */
2210         UI_view2d_curRect_validate(v2d);
2211 }
2212
2213 /**
2214  * Simple pan function
2215  *  (0.0, 0.0) bottom left
2216  *  (0.5, 0.5) center
2217  *  (1.0, 1.0) top right.
2218  */
2219 void UI_view2d_offset(struct View2D *v2d, float xfac, float yfac)
2220 {
2221         if (xfac != -1.0f) {
2222                 const float xsize = BLI_rctf_size_x(&v2d->cur);
2223                 const float xmin = v2d->tot.xmin;
2224                 const float xmax = v2d->tot.xmax - xsize;
2225
2226                 v2d->cur.xmin = (xmin * (1.0f - xfac)) + (xmax * xfac);
2227                 v2d->cur.xmax = v2d->cur.xmin + xsize;
2228         }
2229
2230         if (yfac != -1.0f) {
2231                 const float ysize = BLI_rctf_size_y(&v2d->cur);
2232                 const float ymin = v2d->tot.ymin;
2233                 const float ymax = v2d->tot.ymax - ysize;
2234
2235                 v2d->cur.ymin = (ymin * (1.0f - yfac)) + (ymax * yfac);
2236                 v2d->cur.ymax = v2d->cur.ymin + ysize;
2237         }
2238
2239         UI_view2d_curRect_validate(v2d);
2240 }
2241
2242 /* Check if mouse is within scrollers
2243  *      - Returns appropriate code for match
2244  *              'h' = in horizontal scroller
2245  *              'v' = in vertical scroller
2246  *              0 = not in scroller
2247  *      
2248  *      - x,y   = mouse coordinates in screen (not region) space
2249  */
2250 short UI_view2d_mouse_in_scrollers(const bContext *C, View2D *v2d, int x, int y)
2251 {
2252         ARegion *ar = CTX_wm_region(C);
2253         int co[2];
2254         int scroll = view2d_scroll_mapped(v2d->scroll);
2255         
2256         /* clamp x,y to region-coordinates first */
2257         co[0] = x - ar->winrct.xmin;
2258         co[1] = y - ar->winrct.ymin;
2259         
2260         /* check if within scrollbars */
2261         if (scroll & V2D_SCROLL_HORIZONTAL) {
2262                 if (IN_2D_HORIZ_SCROLL(v2d, co)) return 'h';
2263         }
2264         if (scroll & V2D_SCROLL_VERTICAL) {
2265                 if (IN_2D_VERT_SCROLL(v2d, co)) return 'v';
2266         }
2267         
2268         /* not found */
2269         return 0;
2270 }
2271
2272 /* ******************* view2d text drawing cache ******************** */
2273
2274 typedef struct View2DString {
2275         struct View2DString *next;
2276         union {
2277                 unsigned char ub[4];
2278                 int pack;
2279         } col;
2280         rcti rect;
2281         int mval[2];
2282
2283         /* str is allocated past the end */
2284         char str[0];
2285 } View2DString;
2286
2287 /* assumes caches are used correctly, so for time being no local storage in v2d */
2288 static MemArena     *g_v2d_strings_arena = NULL;
2289 static View2DString *g_v2d_strings = NULL;
2290
2291 void UI_view2d_text_cache_add(View2D *v2d, float x, float y,
2292                               const char *str, size_t str_len, const char col[4])
2293 {
2294         int mval[2];
2295         
2296         BLI_assert(str_len == strlen(str));
2297
2298         if (UI_view2d_view_to_region_clip(v2d, x, y, &mval[0], &mval[1])) {
2299                 int alloc_len = str_len + 1;
2300                 View2DString *v2s;
2301
2302                 if (g_v2d_strings_arena == NULL) {
2303                         g_v2d_strings_arena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 14), __func__);
2304                 }
2305
2306                 v2s = BLI_memarena_alloc(g_v2d_strings_arena, sizeof(View2DString) + alloc_len);
2307
2308                 BLI_LINKS_PREPEND(g_v2d_strings, v2s);
2309
2310                 v2s->col.pack = *((const int *)col);
2311
2312                 memset(&v2s->rect, 0, sizeof(v2s->rect));
2313
2314                 v2s->mval[0] = mval[0];
2315                 v2s->mval[1] = mval[1];
2316
2317                 memcpy(v2s->str, str, alloc_len);
2318         }
2319 }
2320
2321 /* no clip (yet) */
2322 void UI_view2d_text_cache_add_rectf(View2D *v2d, const rctf *rect_view,
2323                                     const char *str, size_t str_len, const char col[4])
2324 {
2325         rcti rect;
2326
2327         BLI_assert(str_len == strlen(str));
2328
2329         if (UI_view2d_view_to_region_rcti_clip(v2d, rect_view, &rect)) {
2330                 int alloc_len = str_len + 1;
2331                 View2DString *v2s;
2332
2333                 if (g_v2d_strings_arena == NULL) {
2334                         g_v2d_strings_arena = BLI_memarena_new(MEM_SIZE_OPTIMAL(1 << 14), __func__);
2335                 }
2336
2337                 v2s = BLI_memarena_alloc(g_v2d_strings_arena, sizeof(View2DString) + alloc_len);
2338
2339                 BLI_LINKS_PREPEND(g_v2d_strings, v2s);
2340
2341                 v2s->col.pack = *((const int *)col);
2342
2343                 v2s->rect = rect;
2344
2345                 v2s->mval[0] = v2s->rect.xmin;
2346                 v2s->mval[1] = v2s->rect.ymin;
2347
2348                 memcpy(v2s->str, str, alloc_len);
2349         }
2350 }
2351
2352
2353 void UI_view2d_text_cache_draw(ARegion *ar)
2354 {
2355         View2DString *v2s;
2356         int col_pack_prev = 0;
2357
2358         /* investigate using BLF_ascender() */
2359         const float default_height = g_v2d_strings ? BLF_height_default("28", 3) : 0.0f;
2360
2361         wmOrtho2_region_ui(ar);
2362
2363         for (v2s = g_v2d_strings; v2s; v2s = v2s->next) {
2364                 int xofs = 0, yofs;
2365
2366                 yofs = ceil(0.5f * (BLI_rcti_size_y(&v2s->rect) - default_height));
2367                 if (yofs < 1) yofs = 1;
2368
2369                 if (col_pack_prev != v2s->col.pack) {
2370                         glColor3ubv(v2s->col.ub);
2371                         col_pack_prev = v2s->col.pack;
2372                 }
2373
2374                 if (v2s->rect.xmin >= v2s->rect.xmax)
2375                         BLF_draw_default((float)(v2s->mval[0] + xofs), (float)(v2s->mval[1] + yofs), 0.0,
2376                                           v2s->str, BLF_DRAW_STR_DUMMY_MAX);
2377                 else {
2378                         BLF_clipping_default(v2s->rect.xmin - 4, v2s->rect.ymin - 4, v2s->rect.xmax + 4, v2s->rect.ymax + 4);
2379                         BLF_enable_default(BLF_CLIPPING);
2380                         BLF_draw_default(v2s->rect.xmin + xofs, v2s->rect.ymin + yofs, 0.0f,
2381                                          v2s->str, BLF_DRAW_STR_DUMMY_MAX);
2382                         BLF_disable_default(BLF_CLIPPING);
2383                 }
2384         }
2385         g_v2d_strings = NULL;
2386
2387         if (g_v2d_strings_arena) {
2388                 BLI_memarena_free(g_v2d_strings_arena);
2389                 g_v2d_strings_arena = NULL;
2390         }
2391
2392         // glMatrixMode(GL_PROJECTION);
2393         // glPopMatrix();
2394         // glMatrixMode(GL_MODELVIEW);
2395         // glPopMatrix();
2396 }
2397
2398
2399 /* ******************************************************** */
2400
2401