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