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