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