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