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