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