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