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