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