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