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