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