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