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