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