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