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