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