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